Download presentation
Presentation is loading. Please wait.
Published byRoland White Modified over 9 years ago
1
Insight Through Computing 26. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion
2
Insight Through Computing 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
Insight Through Computing Key idea of “phone book search”: repeated halving To find the page containing Pat Reed’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 2 nd half. else Rip and throw away the 1 st half. end
4
Insight Through Computing 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
Insight Through Computing 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 log 2 n comparisons.
6
Insight Through Computing 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 log 2 n comparisons. “Savings” is significant! n log2(n) 100 7 100010 1000013
7
Insight Through Computing 121535334245517362 758698 Binary search: target x = 70 v L: Mid: R: 1 6 12 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x So throw away the left half…
8
Insight Through Computing 121535334245517362 758698 v L: Mid: R: 6 9 12 1 2 3 4 5 6 7 8 9 10 11 12 x < v(Mid) So throw away the right half… Binary search: target x = 70
9
Insight Through Computing 121535334245517362 758698 v L: Mid: R: 6 7 9 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x So throw away the left half… Binary search: target x = 70
10
Insight Through Computing 121535334245517362 758698 v L: Mid: R: 7 8 9 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x So throw away the left half… Binary search: target x = 70
11
Insight Through Computing 121535334245517362 758698 v L: Mid: R: 8 8 9 1 2 3 4 5 6 7 8 9 10 11 12 Done because R-L = 1 Binary search: target x = 70
12
Insight Through Computing 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
13
Insight Through Computing 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 What if those two helpers each had two sub-helpers? 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. And the sub-helpers each had two sub-sub-helpers? And…
15
Insight Through Computing Subdivide the sorting task JNRCPDFLAQBKMGHEAQBKMGHEJNRCPDFL
16
Insight Through Computing Subdivide again AQBKMGHEJNRCPDFLMGHEAQBKPDFLJNRC
17
Insight Through Computing And again MGHEAQBKPDFLJNRCMGHE AQ BK PDFLJNRC
18
Insight Through Computing And one last time JNRCPDFLAQBKMGHE
19
Insight Through Computing Now merge GMEH AQ BK DPFLJNCR JNRCPDFLAQBKMGHE
20
Insight Through Computing And merge again HMEGKQABLPDFNRCJGMEH AQ BK DPFLJNCR
21
Insight Through Computing And again MQHKEGABPRLNFJCDHMEGKQABLPDFNRCJ
22
Insight Through Computing And one last time MQHKEGABPRLNFJCDEFCDABJKGHNPLM QR
23
Insight Through Computing Done! EFCDABJKGHNPLM QR
24
Insight Through Computing 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
Insight Through Computing The central sub-problem is the merging of two sorted arrays into one single sorted array 123345351542655575121535334245557565
26
123345351542655575 x: y: z: 1 1 1 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
27
12334535154265557512 x: y: z: 1 1 1 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) YES
28
12334535154265557512 x: y: z: 2 1 2 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
29
1233453515426555751215 x: y: z: 2 1 2 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) NO
30
1233453515426555751215 x: y: z: 2 2 3 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
31
123345351542655575121533 x: y: z: 2 2 3 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) YES
32
123345351542655575121533 x: y: z: 3 2 4 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
33
12334535154265557512153533 x: y: z: 3 2 4 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) YES
34
12334535154265557512153533 x: y: z: 4 2 5 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
35
1233453515426555751215353342 x: y: z: 4 2 5 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) NO
36
1233453515426555751215353342 x: y: z: 4 3 5 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) ???
37
123345351542655575121535334245 x: y: z: 4 3 5 ix: iy: iz: Merge ix<=4 and iy<=5: x(ix) <= y(iy) YES
38
123345351542655575121535334245 x: y: z: 5 3 6 ix: iy: iz: Merge ix > 4
39
12334535154265557512153533424555 x: y: z: 5 3 6 ix: iy: iz: Merge ix > 4: take y(iy)
40
12334535154265557512153533424555 x: y: z: 5 4 8 ix: iy: iz: Merge iy <= 5
41
1233453515426555751215353342455565 x: y: z: 5 4 8 ix: iy: iz: Merge iy <= 5
42
1233453515426555751215353342455565 x: y: z: 5 5 9 ix: iy: iz: Merge iy <= 5
43
123345351542655575121535334245557565 x: y: z: 5 5 9 ix: iy: iz: Merge 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 if x(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 % 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 if x(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 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
Insight Through Computing 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
Insight Through Computing 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
Insight Through Computing 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!
51
Insight Through Computing 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 = mergeSort(x(1:m)); yR = mergeSort(x(m+1:n)); y = merge(yL,yR); end
52
Insight Through Computing function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end
53
Insight Through Computing function y=mergeSort(x) n=length(x); if n==1 y=x; else m=floor(n/2); yL=mergeSort(x(1:m)); yR=mergeSort(x(m+1:n)); y=merge(yL,yR); end
54
Insight Through Computing Divide-and-conquer methods also show up in geometric situations Chop a region up into triangles with smaller triangles in “areas of interest” Recursive mesh generation
55
Insight Through Computing Mesh Generation Step one in simulating flow around an airfoil is to generate a mesh and (say) estimate velocity at each mesh point. An area of interest
56
Insight Through Computing Mesh Generation in 3D
57
Insight Through Computing Why is mesh generation a divide & conquer process? Let’s draw this graphic
58
Insight Through Computing The basic operation if the triangle is big enough Connect the midpoints. Color the interior triangle mauve. else Color the whole triangle yellow. end
59
Insight Through Computing At the Start…
60
Insight Through Computing Recur on this idea: Apply same idea to the lower left triangle
61
Insight Through Computing Recur again
62
Insight Through Computing … and again
63
Insight Through Computing … and again
64
Insight Through Computing Now, climb your way out
65
Insight Through Computing …, etc.
66
Insight Through Computing function drawTriangle(x,y,level) % Draw recursively colored triangles. % x,y are 3-vectors that define the vertices of a triangle. if level==5 % Recursion limit (depth) reached fill(x,y,'y') % Color whole triangle yellow else % Draw the triangle... plot([x x(1)],[y y(1)],'k') % Determine the midpoints... a = [(x(1)+x(2))/2 (x(2)+x(3))/2 (x(3)+x(1))/2]; b = [(y(1)+y(2))/2 (y(2)+y(3))/2 (y(3)+y(1))/2]; % Draw and color the interior triangle mauve pause fill(a,b,'m') pause % Apply the process to the three "corner" triangles... drawTriangle([x(1) a(1) a(3)],[y(1) b(1) b(3)],level+1) drawTriangle([x(2) a(2) a(1)],[y(2) b(2) b(1)],level+1) drawTriangle([x(3) a(3) a(2)],[y(3) b(3) b(2)],level+1) end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.