1 / 66

26. Divide and Conquer Algorithms

26. Divide and Conquer Algorithms. Binary Search Merge Sort Mesh Generation Recursion. An ordered (sorted) list. The Manhattan phone book has 1,000,000+ entries. How is it possible to locate a name by examining just a tiny , tiny fraction of those entries?.

lowri
Download Presentation

26. 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. 26. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion

  2. An ordered (sorted) list The Manhattan phone book has 1,000,000+ entries. How is it possible to locate a name by examining just a tiny, tiny fraction of those entries?

  3. Key idea of “phone book search”: repeated halving To find the page containing PatReed’s number… while (Phone book is longer than 1 page) Open to the middle page. if “Reed” comes before the first entry, Rip and throw away the 2nd half. else Rip and throw away the 1st half. end end

  4. What happens to the phone book length? Original: 3000 pages After 1 rip: 1500 pages After 2 rips: 750 pages After 3 rips: 375 pages After 4 rips: 188 pages After 5 rips: 94 pages : After 12 rips: 1 page

  5. Binary Search Repeatedly halving the size of the “search space” is the main idea behind the method of binary search. An item in a sorted array of length n can be located with just log2 n comparisons.

  6. n log2(n) 100 7 1000 10 10000 13 Binary Search Repeatedly halving the size of the “search space” is the main idea behind the method of binary search. An item in a sorted array of length n can be located with just log2 n comparisons. “Savings” is significant!

  7. 12 15 33 35 42 45 51 62 73 Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 75 86 98 L: 1 v(Mid) <= x So throw away the left half… Mid: 6 R: 12

  8. 12 15 33 35 42 45 51 62 73 Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 75 86 98 L: 6 x < v(Mid) So throw away the right half… Mid: 9 R: 12

  9. 12 15 33 35 42 45 51 62 73 Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 75 86 98 L: 6 v(Mid) <= x So throw away the left half… Mid: 7 R: 9

  10. 12 15 33 35 42 45 51 62 73 Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 75 86 98 L: 7 v(Mid) <= x So throw away the left half… Mid: 8 R: 9

  11. 12 15 33 35 42 45 51 62 73 Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 75 86 98 L: 8 Done because R-L = 1 Mid: 8 R: 9

  12. function L = BinarySearch(a,x) % x is a row n-vector with x(1) < ... < x(n) % where x(1) <= a <= x(n) % L is the index such that x(L) <= a <= x(L+1) L = 1; R = length(x); % x(L) <= a <= x(R) while R-L > 1 mid = floor((L+R)/2); % Note that mid does not equal L or R. if a < x(mid) % x(L) <= a <= x(mid) R = mid; else % x(mid) <= a <== x(R) L = mid; end end

  13. Binary search is efficient, but how do we sort a vector in the first place so that we can use binary search? • Many different algorithms out there... • Let’s look at merge sort • An example of the “divide and conquer” approach

  14. Merge sort: Motivation • If I have two helpers, I’d… • Give each helper half the array to sort • Then I get back the sorted subarrays and merge them. What if those two helpers each had two sub-helpers? And the sub-helpers each had two sub-sub-helpers? And…

  15. M H F M J A A R B H P P R B J F K N C D L Q G N L E E D C G K Q Subdivide the sorting task

  16. A F R P P M J F R B M J B H A H C Q K G E N D D C L K N L Q E G Subdivide again

  17. H E M G J R F P B A H M B C N G E K L D K Q And again A Q F L P D R C J N

  18. And one last time H E M G B K A Q F L P D R C J N

  19. E H G M B K Now merge A Q F L D P C R J N H E M G B K A Q F L P D R C J N

  20. E H G M N C D L A K E H B J R M G B F P K Q And merge again A Q F L D P C R J N

  21. K C C F L H P D L A E N H E M A N Q K G B R J P J D B R F Q G M And again

  22. L C A M N E G H E J L F A C P B D F Q K G R H P M J D B K N And one last time Q R

  23. E C A J G N L P H M F D K B Done! Q R

  24. function y = mergeSort(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL(x(1:m)); yR = mergeSortR(x(m+1:n)); y = merge(yL,yR); end

  25. 12 33 35 45 15 12 42 15 33 55 65 35 75 42 45 55 65 75 The central sub-problem is the merging of two sorted arrays into one single sorted array

  26. 12 33 35 45 15 42 55 65 75 Merge x: 1 ix: iy: 1 y: 1 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  27. 12 33 35 45 15 12 42 55 65 75 Merge x: 1 ix: iy: 1 y: 1 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) YES

  28. 12 33 35 45 15 12 42 55 65 75 Merge x: 2 ix: iy: 1 y: 2 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  29. 12 33 35 45 15 12 15 42 55 65 75 Merge x: 2 ix: iy: 1 y: 2 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) NO

  30. 12 33 35 45 15 12 15 42 55 65 75 Merge x: 2 ix: iy: 2 y: 3 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  31. 12 33 35 45 15 12 42 15 33 55 65 75 Merge x: 2 ix: iy: 2 y: 3 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) YES

  32. 12 33 35 45 15 12 42 15 33 55 65 75 Merge x: 3 ix: iy: 2 y: 4 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  33. 12 33 35 45 15 12 42 15 33 55 65 35 75 Merge x: 3 ix: iy: 2 y: 4 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) YES

  34. 12 33 35 45 15 12 42 15 33 55 65 35 75 Merge x: 4 ix: iy: 2 y: 5 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  35. 12 33 35 45 15 12 42 15 33 55 65 35 75 42 Merge x: 4 ix: iy: 2 y: 5 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) NO

  36. 12 33 35 45 15 12 42 15 33 55 65 35 75 42 Merge x: 4 ix: iy: 3 y: 5 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) ???

  37. 12 33 35 45 15 12 42 15 33 55 65 35 75 42 45 Merge x: 4 ix: iy: 3 y: 5 iz: z: ix<=4 and iy<=5: x(ix) <= y(iy) YES

  38. 12 33 35 45 15 12 42 15 33 55 65 35 75 42 45 Merge x: 5 ix: iy: 3 y: 6 iz: z: ix > 4

  39. 12 33 35 45 15 12 42 15 55 33 65 35 75 42 45 55 Merge x: 5 ix: iy: 3 y: 6 iz: z: ix > 4: take y(iy)

  40. 12 33 35 45 15 12 42 15 55 33 65 35 75 42 45 55 Merge x: 5 ix: iy: 4 y: 8 iz: z: iy <= 5

  41. 12 33 35 45 15 12 42 15 55 33 65 35 75 42 45 55 65 Merge x: 5 ix: iy: 4 y: 8 iz: z: iy <= 5

  42. 12 33 35 45 15 12 42 15 55 33 65 35 75 42 45 55 65 Merge x: 5 ix: iy: 5 y: 9 iz: z: iy <= 5

  43. 12 33 35 45 15 12 42 15 55 33 65 35 75 42 45 55 65 75 Merge x: 5 ix: iy: 5 y: 9 iz: z: iy <= 5

  44. function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1,nx+ny); ix = 1; iy = 1; iz = 1;

  45. function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny end % Deal with remaining values in x or y

  46. function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny ifx(ix) <= y(iy) z(iz)= x(ix); ix=ix+1; iz=iz+1; else z(iz)= y(iy); iy=iy+1; iz=iz+1; end end % Deal with remaining values in x or y

  47. function z = merge(x,y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; while ix<=nx && iy<=ny ifx(ix) <= y(iy) z(iz)= x(ix); ix=ix+1; iz=iz+1; else z(iz)= y(iy); iy=iy+1; iz=iz+1; end end while ix<=nx % copy remaining x-values z(iz)= x(ix); ix=ix+1; iz=iz+1; end while iy<=ny % copy remaining y-values z(iz)= y(iy); iy=iy+1; iz=iz+1; end

  48. function y = mergeSort(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL(x(1:m)); yR = mergeSortR(x(m+1:n)); y = merge(yL,yR); end

  49. function y = mergeSortL(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL_L(x(1:m)); yR = mergeSortL_R(x(m+1:n)); y = merge(yL,yR); end

  50. function y = mergeSortL_L(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); if n==1 y = x; else m = floor(n/2); yL = mergeSortL_L_L(x(1:m)); yR = mergeSortL_L_R(x(m+1:n)); y = merge(yL,yR); end There should be just one mergeSort function!

More Related