26 Divide and Conquer Algorithms Binary Search Merge
26. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion Insight Through Computing
An ordered (sorted) list The Manhattan phone book has 1, 000+ entries. How is it possible to locate a name by examining just a tiny, tiny fraction of those entries? 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 Insight Through Computing
What happens to the phone book length? Original: 3000 After 1 rip: 1500 After 2 rips: 750 After 3 rips: 375 After 4 rips: 188 After 5 rips: 94 : After 12 rips: 1 Insight Through Computing pages pages page
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. 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! Insight Through Computing n 10000 log 2(n) 7 10 13
Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 12 15 33 35 42 45 51 62 73 75 86 98 L: 1 Mid: 6 R: 12 Insight Through Computing v(Mid) <= x So throw away the left half…
Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 12 15 33 35 42 45 51 62 73 75 86 98 L: 6 Mid: 9 R: 12 Insight Through Computing x < v(Mid) So throw away the right half…
Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 12 15 33 35 42 45 51 62 73 75 86 98 L: 6 Mid: 7 R: 9 Insight Through Computing v(Mid) <= x So throw away the left half…
Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 12 15 33 35 42 45 51 62 73 75 86 98 L: 7 Mid: 8 R: 9 Insight Through Computing v(Mid) <= x So throw away the left half…
Binary search: target x = 70 1 2 3 4 5 6 7 8 9 10 11 12 v 12 15 33 35 42 45 51 62 73 75 86 98 L: 8 Done because Mid: 8 R-L = 1 R: 9 Insight Through Computing
function L = Binary. Search(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 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? n n n Many different algorithms out there. . . Let’s look at merge sort An example of the “divide and conquer” approach Insight Through Computing
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? Insight Through Computing And the sub-helpers each had two sub-helpers? And…
Subdivide the sorting task H E M G B K A Q F L P D R C J N H E M G B K A Q Insight Through Computing F L P D R C J N
Subdivide again H E M G B K A Q H E M G Insight Through Computing B K A Q F L P D R C J N
And again H E M G Insight Through Computing B K A Q F L P D R C J N
And one last time H E M G B Insight Through Computing K A Q F L P D R C J N
Now merge E H H E G M M G B K A Q F L D P C R B A F P R Insight Through Computing K Q L D C J N
And merge again E G H M E H G M Insight Through Computing A B K Q B K A Q D F L P F L D P C J N R C R J N
And again A B E G H K M Q E G H M Insight Through Computing A B K Q C D F J L N P R D F L P C J N R
And one last time A B C D E F G H J K L M N P Q R A B E G H K M Q Insight Through Computing C D F J L N P R
Done! A B C D E F G H J K L M N P Q R Insight Through Computing
function y = merge. Sort(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); y. L = merge. Sort. L(x(1: m)); y. R = merge. Sort. R(x(m+1: n)); y = merge(y. L, y. R); end Insight Through Computing
The central sub-problem is the merging of two sorted arrays into one single sorted array 12 33 35 45 15 42 55 65 75 12 15 33 35 42 45 55 65 75 Insight Through Computing
Merge x: 12 33 35 45 ix: 1 y: 15 42 55 65 75 iy: 1 z: iz: 1 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 1 y: 15 42 55 65 75 iy: 1 z: 12 iz: 1 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) YES
Merge x: 12 33 35 45 ix: 2 y: 15 42 55 65 75 iy: 1 z: 12 iz: 2 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 2 y: 15 42 55 65 75 iy: 1 z: 12 15 iz: 2 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) NO
Merge x: 12 33 35 45 ix: 2 y: 15 42 55 65 75 iy: 2 z: 12 15 iz: 3 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 2 y: 15 42 55 65 75 iy: 2 z: 12 15 33 iz: 3 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) YES
Merge x: 12 33 35 45 ix: 3 y: 15 42 55 65 75 iy: 2 z: 12 15 33 iz: 4 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 3 y: 15 42 55 65 75 iy: 2 z: 12 15 33 35 iz: 4 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) YES
Merge x: 12 33 35 45 ix: 4 y: 15 42 55 65 75 iy: 2 z: 12 15 33 35 iz: 5 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 4 y: 15 42 55 65 75 iy: 2 z: 12 15 33 35 42 iz: 5 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) NO
Merge x: 12 33 35 45 ix: 4 y: 15 42 55 65 75 iy: 3 z: 12 15 33 35 42 iz: 5 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) ? ? ?
Merge x: 12 33 35 45 ix: 4 y: 15 42 55 65 75 iy: 3 z: 12 15 33 35 42 45 iz: 5 ix<=4 and iy<=5: Insight Through Computing x(ix) <= y(iy) YES
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 3 z: 12 15 33 35 42 45 iz: 6 ix > 4 Insight Through Computing
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 3 z: 12 15 33 35 42 45 55 iz: 6 ix > 4: Insight Through Computing take y(iy)
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 4 z: 12 15 33 35 42 45 55 iz: 8 iy <= 5 Insight Through Computing
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 4 z: 12 15 33 35 42 45 55 65 iz: 8 iy <= 5 Insight Through Computing
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 5 z: 12 15 33 35 42 45 55 65 iz: 9 iy <= 5 Insight Through Computing
Merge x: 12 33 35 45 ix: 5 y: 15 42 55 65 75 iy: 5 z: 12 15 33 35 42 45 55 65 75 iz: 9 iy <= 5 Insight Through Computing
function z = merge(x, y) nx = length(x); ny = length(y); z = zeros(1, nx+ny); ix = 1; iy = 1; iz = 1; Insight Through Computing
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 Insight Through Computing
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 Insight Through Computing
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 Insight Through Computing
function y = merge. Sort(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); y. L = merge. Sort. L(x(1: m)); y. R = merge. Sort. R(x(m+1: n)); y = merge(y. L, y. R); end Insight Through Computing
function y = merge. Sort. 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); y. L = merge. Sort. L_L(x(1: m)); y. R = merge. Sort. L_R(x(m+1: n)); y = merge(y. L, y. R); end Insight Through Computing
function y = merge. Sort. L_L(x) % x is a vector. y is a vector % consisting of the values in x % sorted from smallest to largest. n = length(x); There should be just one if n==1 merge. Sort function! y = x; else m = floor(n/2); y. L = merge. Sort. L_L_L(x(1: m)); y. R = merge. Sort. L_L_R(x(m+1: n)); y = merge(y. L, y. R); end Insight Through Computing
function y = merge. Sort(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); y. L = merge. Sort(x(1: m)); y. R = merge. Sort(x(m+1: n)); y = merge(y. L, y. R); end Insight Through Computing
function y=merge. Sort(x) n=length(x); if n==1 y=x; else m=floor(n/2); y. L=merge. Sort(x(1: m)); y. R=merge. Sort(x(m+1: n)); y=merge(y. L, y. R); end Insight Through Computing
function y=merge. Sort(x) n=length(x); if n==1 y=x; else m=floor(n/2); y. L=merge. Sort(x(1: m)); y. R=merge. Sort(x(m+1: n)); y=merge(y. L, y. R); end 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 Insight Through Computing
Mesh Generation An area of interest Step one in simulating flow around an airfoil is to generate a mesh and (say) estimate velocity at each mesh point. Insight Through Computing
Mesh Generation in 3 D Insight Through Computing
Why is mesh generation a divide & conquer process? Let’s draw this graphic Insight Through Computing
The basic operation the triangle is big enough if Connect the midpoints. Color the interior triangle mauve. else Color the whole triangle yellow. end Insight Through Computing
At the Start… Insight Through Computing
Recur on this idea: Apply same idea to the lower left triangle Insight Through Computing
Recur again Insight Through Computing
… and again Insight Through Computing
… and again Insight Through Computing
Now, climb your way out Insight Through Computing
…, etc. Insight Through Computing
function draw. Triangle(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. . . draw. Triangle([x(1) a(3)], [y(1) b(3)], level+1) draw. Triangle([x(2) a(1)], [y(2) b(1)], level+1) draw. Triangle([x(3) a(2)], [y(3) b(2)], level+1) end Insight Through Computing
- Slides: 66