710 likes | 866 Vues
Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst. 7 2 9 4 2 4 7 9. 7 2 2 7. 9 4 4 9. 7 7. 2 2. 9 9. 4 4. Divide-and-Conquer. Outline and Reading. Divide-and-conquer paradigm (§5.2) Recurrence Equations (§5.2.1)
E N D
Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conquer Divide-and-Conquer
Outline and Reading • Divide-and-conquer paradigm (§5.2) • Recurrence Equations (§5.2.1) • Iterative substitution • Recursion trees • Guess-and-test • The master method • Merge-sort (§4.1.1) • Algorithm • Merging two sorted sequences • Merge-sort tree • Execution example • Analysis • Generic merging and set operations (§4.2.1) • Quick-sort (§4.3) • Algorithm • Partition step • Quick-sort tree • Execution example • Analysis of quick-sort (4.3.1) • In-place quick-sort (§4.8) • Summary of sorting algorithms Divide-and-Conquer
Divide-and-Conquer • Divide-and conquer is a general algorithm design paradigm: • Divide the input data S in two or more disjoint subsets S1, S2, … • Conquer the subproblems by solving them recursively • base case for the recursion: If the subproblems are small enough just solve them • Combine the solutions for S1,S2, …, into a solution for S • Analysis can be done using recurrence equations Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Base case Subproblem size 1 # subproblems Work dividing and combining Example: Sum of Queue SumQueue(Q) if (Q.length == 0 ) return 0 else return Q.dequeue() + SumQueue(Q) One subproblem Linear reduction in size (decrease by 1) Combining: constant (cost of 1 add) T(0) b T(n) c + T(n – 1) for n>0 Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Iterative substitution method Sum of Queue Solution Equation: T(0) b T(n) c + T(n – 1) for n>0 Solution: (finding the closed form solution) T(n) c + c + T(n-2) c + c + c + T(n-3) kc + T(n-k) for all k nc + T(0) for k=n cn + b = O(n) Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Find: 9 3 5 7 8 9 12 15 Example: Binary Search Search a sorted array for a given item, x • If x == middle array element, return true • Else, BinarySearch lower (x<mid) or upper (x>mid) sub-array • 1 subproblem, half as large BinarySearch(A, x) if (A.size == 1) return (x == A[0]) mid = A.size / 2 if (x == A[mid]) return true else if (x < A[mid]) return BinarySearch( A_LowerHalf, x) else if (x > A[mid]) return BinarySearch( A_UpperHalf, x) Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Base case Subproblem size 1 Work dividing and combining # subproblems Example: Binary Search Search a sorted array for a given item, x • If x == middle array element, return true • Else, BinarySearch lower (x<mid) or upper (x>mid) sub-array • 1 subproblem, half as large BinarySearch(A, x) if (A.size == 1) return (x == A[0]) mid = A.size / 2 if (x == A[mid]) return true else if (x < A[mid]) return BinarySearch( A_LowerHalf, x) else if (x > A[mid]) return BinarySearch( A_UpperHalf, x) Equation: T(1) b T(n) T(n/2) + c for n>1 Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Iterative substitution method Binary Search Solution Equation: T(1) b T(n) T(n/2) + c for n>1 Solution: (finding the closed form solution) T(n) T(n/2) + c T(n/4) + c + c T(n/8) + c + c + c T(n/2k) + kc T(1) + c log n where k = log n b + c log n = O(log n) Divide-and-Conquer
Modified from: Mukund Narasimhan, University of Washington Recursion Tree for Binary Search Problem size Cost per stage n O(1) n/2 O(1) n/4 O(1) log n … … 1 O(1) Θ(log n) Divide-and-Conquer
Example: Recursion Tree • The recursion tree is of the form: • where d > 0 is constant. • And the base case has a running time b Divide-and-Conquer
bn b bn + dn logn Drawing the Recursion Tree With: Divide-and-Conquer
Iterative Substitution forThe recursion tree • In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: • Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. • So, • Thus, T(n) is O(n log n). Divide-and-Conquer
Guess-and-Test Method,Part 1 • In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: • Guess: T(n) < cn log n. • Wrong: we cannot make this last line be less than cn log n Divide-and-Conquer
Guess-and-Test Method, Part 2 • Recall the recurrence equation: • Guess #2: T(n) < cn log2 n. • if c > b. • So, T(n) is O(n log2 n). • In general, to use this method, you need to have a good guess and you need to be good at induction proofs. Divide-and-Conquer
Modified from: Carola Wenk, University of Texas at San Antonio The divide-and-conquerdesign paradigm • Dividethe problem (instance) into subproblems. • asubproblems, eachof size n/b • Conquerthe subproblems by solving them recursively. The base case has a runtime c • Combinesubproblem solutions. Runtime is f(n) Divide-and-Conquer
Master Method • Many divide-and-conquer recurrence equations have the form: • The Master Theorem: Divide-and-Conquer
Master Method, Example 1 • The form: • The Master Theorem: • Example: Solution: logba=2, so case 1 says T(n) is O(n2). Divide-and-Conquer
Master Method, Example 2 • The form: • The Master Theorem: • Example: Solution: logba=1, so case 2 says T(n) is O(n log2 n). Divide-and-Conquer
Master Method, Example 3 • The form: • The Master Theorem: • Example: Solution: logba=0, so case 3 says T(n) is O(n logn). Divide-and-Conquer
Master Method, Example 4 • The form: • The Master Theorem: • Example: Solution: logba=3, so case 1 says T(n) is O(n3). Divide-and-Conquer
Master Method, Example 5 • The form: • The Master Theorem: • Example: Solution: logba=2, so case 3 says T(n) is O(n3). Divide-and-Conquer
Master Method, Example 6 • The form: • The Master Theorem: • Example: (binary search) Solution: logba=0, so case 2 says T(n) is O(log n). Divide-and-Conquer
Master Method, Example 7 • The form: • The Master Theorem: • Example: (heap construction) Solution: logba=1, so case 1 says T(n) is O(n). Divide-and-Conquer
Iterative “Proof” of the Master Theorem • Using iterative substitution, let us see if we can find a pattern: • We then distinguish the three cases as • The first term is dominant • Each part of the summation is equally dominant • The summation is a geometric series Divide-and-Conquer
Divide-and-Conquer,Algorithm Examples: • Merge SortQuick Sort Divide-and-Conquer
7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort Divide-and-Conquer
Outline • Merge-sort (§4.1.1) • Algorithm • Merging two sorted sequences • Merge-sort tree • Execution example • Analysis • Generic merging and set operations (§4.2.1) Divide-and-Conquer
Merge-Sort • Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm • Like heap-sort • It uses a comparator • It has O(n log n) running time • Unlike heap-sort • It does not use an auxiliary priority queue • It accesses data in a sequential manner (suitable to sort data on a disk) Divide-and-Conquer
2T(n/2) f(n)=bn Merge-Sort AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted • according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2) • Merge-sort on an input sequence S with n elements consists of three steps: • Divide: partition S into two sequences S1and S2 of about n/2 elements each • Recur: recursively sort S1and S2 • Conquer: merge S1and S2 into a unique sorted sequence Base case: merge(elem1,elem2) Running time = b Divide-and-Conquer
Merging Two Sorted Sequences Algorithmmerge(A, B) Inputsequences A and B withn/2 elements each Outputsorted sequence of A B S empty sequence whileA.isEmpty() B.isEmpty() ifA.first().element()<B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) whileA.isEmpty() S.insertLast(A.remove(A.first())) whileB.isEmpty() S.insertLast(B.remove(B.first())) return S • The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B • Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time Divide-and-Conquer
Modified from: Monica Nicolescu, University of Nevada, Remo Example: MERGE(L,R) Divide-and-Conquer
Modified from: Monica Nicolescu, University of Nevada, Remo Example: MERGE(L,R) Divide-and-Conquer
Modified from: Monica Nicolescu, University of Nevada, Remo Example (cont.) Divide-and-Conquer
Modified from: Monica Nicolescu, University of Nevada, Remo Example (cont.) Divide-and-Conquer
Modified from: Monica Nicolescu, University of Nevada, Remo Example (cont.) Done! Divide-and-Conquer
Merge-Sort Tree • An execution of merge-sort is depicted by a binary tree • each node represents a recursive call of merge-sort and stores • unsorted sequence before the execution and its partition • sorted sequence at the end of the execution • the root is the initial call • the leaves are calls on subsequences of size 0 or 1 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conquer
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example • Partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 Divide-and-Conquer
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example (cont.) • Recursive call, partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 Divide-and-Conquer
7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example (cont.) • Recursive call, partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 Divide-and-Conquer
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 Execution Example (cont.) • Recursive call, base case 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 77 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Divide-and-Conquer
Execution Example (cont.) • Recursive call, base case 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Divide-and-Conquer
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Divide-and-Conquer
Execution Example (cont.) • Recursive call, …, base case, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Divide-and-Conquer
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Divide-and-Conquer
Execution Example (cont.) • Recursive call, …, merge, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11 Divide-and-Conquer
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11 Divide-and-Conquer
Analysis of Merge-Sort • The height h of the merge-sort tree is O(log n) • at each recursive call we divide in half the sequence, • The overall amount or work done at the nodes of depth i is O(n) • we partition and merge 2i sequences of size n/2i • we make 2i+1 recursive calls • Thus, the total running time of merge-sort is O(n log n) Divide-and-Conquer
Recurrence Equation Analysis • The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. • Likewise, the basis case (n< 2) will take at b most steps. • Therefore, if we let T(n) denote the running time of merge-sort: • We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. • That is, a solution that has T(n) only on the left-hand side. Divide-and-Conquer
Iterative Substitution • In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: • Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. • So, • Thus, T(n) is O(n log n). Divide-and-Conquer
7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 2. Quick-Sort Divide-and-Conquer