1 / 35

Master theorem Design divide-and-conquer algorithms

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.

minda
Download Presentation

Master theorem Design divide-and-conquer algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 Divide-and-Conquer • Master theorem • Design divide-and-conquer algorithms Xiaojuan Cai

  2. Overview • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Master theorem Xiaojuan Cai

  10. 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)

  11. Where are we? • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai

  12. 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

  13. 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

  14. 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

  15. Multiplication Problem: Multiplication Input: two n-bits binary integers x and y Output:x ∗ y Xiaojuan Cai

  16. Multiplication Problem: Multiplication Input: two n-bits binary integers x and y Output:x ∗ y Xiaojuan Cai

  17. Matrix multiplication Xiaojuan Cai

  18. Matrix multiplication Xiaojuan Cai

  19. Strassen’s algorithm Xiaojuan Cai

  20. 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

  21. 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

  22. 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

  23. Selection in linear time [by M. Blum, R. Floyd, V. Pratt and R. Rivest] Xiaojuan Cai

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. Θ(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

  31. Selection: algorithm Xiaojuan Cai

  32. 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

  33. Conclusion • Master theorem • Design divide-and-conquer algorithms • Min-Max • Multiplication • Matrix multiplication • Selection Xiaojuan Cai

  34. Exercise • Download hw6.pdf from our course homepage • Due on Oct. 21 Xiaojuan Cai

  35. 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

More Related