Chapter 4: Divide-and-Conquer

Chapter 4: Divide-and-Conquer

Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide: divide instance of problem into two or more smaller instances, ideally of about equal size 2. Conquer: Solve smaller instances recursively 3. Combine: Obtain solution to original (larger) instance by combining these solutions A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Divide-and-Conquer Technique (cont.) a problem of size n subproblem 1 of size n/2 subproblem 2 of size n/2

a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2 Divide-and-Conquer Examples Sorting: mergesort and quicksort Binary tree traversals Multiplication of large integers

Matrix multiplication: Strassens algorithm Closest-pair and convex-hull algorithms Binary search: decrease-by-half (or degenerate divide&conq.) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 General Divide-and-Conquer Recurrence T(n) = aT(n/b) + f (n) where f(n) (nd), d 0 Master Theorem: If a < bd, T(n) (nd) If a = bd, T(n) (nd log n) If a > bd, T(n) (nlog b a ) Examples: T(n) = 4T(n/2) + n T(n) = 4T(n/2) + n2 T(n) = 4T(n/2) + n3

T(n) ? T(n) ? T(n) ? A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 Mergesort Divide into two equal parts Sort each part using merge-sort (recursion!!!) Merge two sorted subsequences A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5 Pseudocode of Mergesort

A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6 Mergesort Example 8 3 2 9 7 1 5 4 8 3 2 9 8 3 8 7 1 5 4 2 9 3 2 3 8 71

9 2 9 7 5 4 1 5 1 7 2 3 8 9 4 4 5 1 4 5 7 1 2 3 4 5 7 8 9 A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7

Merge Merge sorted arrays B and C into array A as follows: Repeat the following until no elements remain in one of the arrays: compare the first elements in the remaining unprocessed portions of the arrays copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A. A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8 Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9

Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10 Pseudocode of Merge A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11 Analysis of Mergesort Recurrence relation: C(n) = 2C(n/2) + Cmerge(n) for n > 1, C(1) = 0 All cases have same efficiency: (n log n) Space requirement: (n) (not in-place)

Can be implemented without recursion (bottom-up) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12 Quick Sort Divide: divide into two parts such that elements of left part < elements of right part Conquer: recursively solve for each part separately Combine: trivial - do not do anything Quicksort(A[lr]) if l

//conquer left Quicksort(A[s+1r]) //conquer right A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Quicksort Select a pivot (partitioning element) here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an algorithm) p A[i]p A[i]p Exchange the pivot with the last element in the first (i.e., )

subarray the pivot is now in its final position Sort the two subarrays recursively A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 Hoares Partitioning Algorithm A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 Quicksort Example l,i 5 5 r 3 1 9 i

i i 3 1 9 8 8 2 2 i 5 5 5 3 3 3 1

1 1 4 4 4 4 7 j j 4 7 j j 8 2

i j 8 2 i j 2 8 j i 9 7 9

7 9 7 5 3 1 4 2 8 9 7 2 3

1 4 5 8 9 7 A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 4 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 Analysis of Quicksort Best case: split in the middle Cbest (n) = 2Cbest (n/2) + n (n log n) Worst case: sorted array! (n2)

Average case: random arrays (n log n) Improvements: better pivot selection: random or median-of-three switch to insertion sort on small subarrays elimination of recursion Considered the method of choice for internal sorting of large files (n 10000) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17 Binary Tree Algorithms Binary tree is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) a if T Inorder(Tleft)

print(root of T) Inorder(Tright) b a c d b e c d e Efficiency: (n) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree

TL TR h(T) = max{h(TL), h(TR)} + 1 if T and h() = -1 Efficiency: (n) A. Levitin Introduction to the Design & Analysis of Algorithms, 3rd ed., Ch. 5 2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19

Recently Viewed Presentations

  • English I Review for Benchmark Test Night- Characters

    English I Review for Benchmark Test Night- Characters

    The Secret Life of Bees-Characters. Lilly Owens. T. Ray Owens. Deborah Owens. August Boatwright. June Boatwright. May Boatwright . Rosaleen. Zachary Taylor . Neil ...
  • The (in)justice of the cross

    The (in)justice of the cross

    "He is the Rock, his works are perfect, and all his ways are just. A faithful God who does no wrong, upright and just is he." We also know that God in his perfect nature is a God of justice....
  • Substituições e Fideicomisso - WordPress.com

    Substituições e Fideicomisso - WordPress.com

    Substituição. Substituição. Disposição testamentária na qual o testador chama uma pessoa para receber,n o todo ou em parte, a herança ou legado, na falta ou após o herdeiro/legatário nomeado em primieiro lugar, ou seja, quando a vocação deste ou daquele...
  • What is gin.o.mai? gin.o.mai is a college age

    What is gin.o.mai? gin.o.mai is a college age

    The righteousness imputed to believing man is God's type of righteousness."dikaiosunhQeou" =righteousness God "Qeou" is in the Genitive Case, which describes possession. In our text, the grammar indicates the genitive case to stress the quality or type of the righteousness...
  • Where&#x27;s the evidence? - University of Edinburgh

    Where's the evidence? - University of Edinburgh

    Baseline < 3 hours Early left basal ganglia hypodensity Follow-up Haemorrhagic transformation Is this too much hypodensity? 5 hrs: swelling + density change 4 days - infarct in density change and swelling areas IST3 00188 5 hrs - then swelling...
  • Center for Sport, Character &amp; Culture

    Center for Sport, Character & Culture

    Are universal (universalizable)—we would will all persons act according to them. Are reversible—we would want to be treated this way. E.g., Respect, responsibility, justice, kindness. Values and the Sun Like the sun, we can't grasp values in their entirety.
  • Infection Prevention and Control the Argentine Experience

    Infection Prevention and Control the Argentine Experience

    * * * * 27 August (South Pacific Teleclass) DIAGNOSIS, TREATMENT AND PREVENTION OF PROSTHETIC JOINT INFECTIONS - A SURGEON'S PERSPECTIVE Prof. Gary Hooper, University of Otago, New Zealand 03 September (FREE WHO Teleclass - Europe) NEW WHO GLOBAL CAMPAIGN...
  • Ceramics and Glass - Materials Research Society

    Ceramics and Glass - Materials Research Society

    Ceramics. Ceramic comes from Keramos (Greek for pottery) Early forms of ceramics were formed by the heat treatment of Clay. E.g. Kaolinite clay after firing above 1000°C forms Porcelain a ceramic