360 likes | 612 Views
Lecture 6 Divide-and-Conquer. Master theorem Design divide-and-conquer algorithms. Overview. Master theorem Design divide-and-conquer algorithms Min-Max Multiplication Matrix multiplication Selection. Divide-and-conquer.
E N D
Lecture 6 Divide-and-Conquer • Master theorem • Design divide-and-conquer algorithms Xiaojuan Cai
Overview • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai
Divide-and-conquer • Divide Step: the input is partitioned into p ≥ 1 parts, each of which is strictly less than n. • Conquer Step: p recursive call(s) of the smaller parts. • Combine Step: combine the solved parts to obtain the desired output. Xiaojuan Cai
Which step is more difficult? Divide-and-conquer • Divide Step: the input is partitioned into p ≥ 1 parts, each of which is strictly less than n. • Conquer Step: p recursive call(s) of the smaller parts. • Combine Step: combine the solved parts to obtain the desired output. Xiaojuan Cai
Mergesort 13 6 24 43 1 51 9 2 Divide 6 13 24 1 9 43 51 2 Conquer Combine 2 6 9 13 24 43 51 1 D(N) = 2 D(N/2) + N Xiaojuan Cai
Conquer 2 6 9 13 24 43 51 1 Quicksort 2 6 24 43 13 51 9 1 Divide Combine 2 6 9 13 24 43 51 1 D(N) = 2 D(N/2) + N Xiaojuan Cai
Divide-and-conquer • Divide Step: the input is partitioned into p ≥ 1 parts, each of which is strictly less than n. • Conquer Step: p recursive call(s) of the smaller parts. • Combine Step: combine the solved parts to obtain the desired output. f(n) = af(n/b) + cnd Xiaojuan Cai
Simplified Master Theorem Let a ≥ 1, b > 1 and c,d,w ≥ 0 be constants, and let T(n) be defined on the nonnegative integers by the recurrence Then Master theorem Xiaojuan Cai
Master theorem Xiaojuan Cai
Quiz Give the growth order of following functions: f(n) = f(n/2) + 1 f(n) = 2f(n/2) + n2 f(n) = 9f(n/3) + n f(n) = 5f(n/2) + n3 A. Θ(logn) B. Θ(nlogn) C. Θ(n2) D. Θ(n3)
Where are we? • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai
MinMax: Which one is better? Problem: MinMax Input: a sequence of integers A[1...n] Output: the maximum element and the minimum element in A[1...n] Xiaojuan Cai
MinMax: Which one is better? Problem: MinMax Input: a sequence of integers A[1...n] Output: the maximum element and the minimum element in A[1...n] #comp = 2n - 2 #comp = 3n/2 - 2 Cost more spaces Xiaojuan Cai
Multiplication Problem: Multiplication Input: two n-bits binary integers x and y Output:x ∗ y O(n) + O(n) + ... + O(n) = (n-1) O(n) = O(n2) Xiaojuan Cai
Multiplication Problem: Multiplication Input: two n-bits binary integers x and y Output:x ∗ y Xiaojuan Cai
Multiplication Problem: Multiplication Input: two n-bits binary integers x and y Output:x ∗ y Xiaojuan Cai
Matrix multiplication Xiaojuan Cai
Matrix multiplication Xiaojuan Cai
Strassen’s algorithm Xiaojuan Cai
Medians and order statistic Problem: Medians Input: An array A[1..n] of n elements Output: The ⎡n/2⎤-th smallest element in A[1..n]. Problem: OrderStatistic (Selection) Input: An array A[1..n] of n numbers, and a positive integer k Output: The k-th smallest element in A[1..n]. • O(nlogn) Xiaojuan Cai
Selection 5th smallest element? 1 13 6 24 43 51 9 2 3rd smallest element? 13 6 24 43 1 2 51 9 24 43 6 9 13 51 Xiaojuan Cai
Worst case: O(N2) • Lower bound: O(N) Quick-select Partition array so that: • Entry a[j] is in place. • No larger entry to the left of j. • No smaller entry to the right of j. Repeat in one subarray, depending on j; finished when j equals k. Xiaojuan Cai 23
Selection in linear time [by M. Blum, R. Floyd, V. Pratt and R. Rivest] Xiaojuan Cai
Selection How to guarantee a good partition? 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 328 3 4 183 92 95 9 25 2 2 74 38 29 27 727 45 4 84 83 17 451 7 41 79 99 84 45 37 5 7 1 26 92 21 75 38 3 91 42 Xiaojuan Cai
Selection Step1. Sort every group of 5 elements. 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 45 2 2 17 3 7 1 25 3 4 74 37 5 7 9 26 4 21 75 38 29 27 41 45 92 84 83 38 92 91 42 79 99 84 328 183 451 95 727 Xiaojuan Cai
Selection Step2. find the median of the medians. 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 Recursively 45 2 2 17 3 7 1 25 3 4 74 37 5 7 9 26 4 21 75 38 29 27 41 45 92 84 83 38 92 91 42 79 99 84 328 183 451 95 727 Xiaojuan Cai
Selection Step3. Use the median as pivot. 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 Recursively 45 2 2 17 3 7 1 25 3 4 74 37 5 7 9 26 29 4 21 75 38 27 41 45 92 84 83 38 92 91 42 79 99 84 328 183 451 95 727 Xiaojuan Cai
Selection Why the median of the medians can guarantee a good partition? 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 45 2 2 17 3 7 1 25 3 4 74 37 5 7 9 26 29 4 21 75 38 27 41 45 92 84 83 38 92 91 42 79 99 84 328 183 451 95 727 Xiaojuan Cai
Selection Why the median of the medians can guarantee a good partition? 3 2 4 99 92 4 2 84 84 21 328 74 83 45 75 183 38 17 37 38 92 29 451 5 3 95 27 7 7 91 9 727 41 1 42 25 45 79 26 45 2 2 17 3 7 1 25 3 4 37 5 7 9 26 74 29 21 75 38 27 45 4 41 92 83 38 92 91 42 79 84 99 84 328 183 451 95 727 Xiaojuan Cai
Θ(n) Selection 45 2 2 17 3 7 1 25 3 4 37 5 7 9 26 74 29 21 75 38 27 45 4 41 92 83 38 92 91 42 79 84 99 84 328 183 451 95 727 Xiaojuan Cai
Selection: algorithm Xiaojuan Cai
Closest pair problem Problem: ClosestPair Input: A set S of n points in the plane Output: The pair p1 = (x1, y1)and p2 = (x2, y2) such that the Euclidean distance between p1 and p2 is minimal. 2δr Divide δr Conquer δl δr Combine Xiaojuan Cai
Conclusion • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai
Exercise • Download hw6.pdf from our course homepage • Due on Oct. 21 Xiaojuan Cai
Groups in Selection algorithm If each group consists of other than 5 elements, which of the following will not keep Θ(n) complexity? A. 3 B. 7 C. 9 D. all of above Xiaojuan Cai