26. Divide and Conquer Algorithms

66
Insight Through Computing 26. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion

description

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?. - PowerPoint PPT Presentation

Transcript of 26. Divide and Conquer Algorithms

Page 1: 26. Divide and Conquer Algorithms

Insight Through Computing

26. Divide and Conquer Algorithms

Binary SearchMerge SortMesh GenerationRecursion

Page 2: 26. Divide and Conquer Algorithms

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?

Page 3: 26. Divide and Conquer Algorithms

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 2nd half. else Rip and throw away the 1st half. end

end

Page 4: 26. Divide and Conquer Algorithms

Insight Through Computing

What happens to the phone book length?

Original: 3000 pagesAfter 1 rip: 1500 pagesAfter 2 rips: 750 pagesAfter 3 rips: 375 pagesAfter 4 rips: 188 pagesAfter 5 rips: 94 pages :After 12 rips: 1 page

Page 5: 26. Divide and Conquer Algorithms

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 log2 n comparisons.

Page 6: 26. Divide and Conquer Algorithms

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 log2 n comparisons.

“Savings” is significant! n log2(n) 100 7 1000 1010000 13

Page 7: 26. Divide and Conquer Algorithms

Insight Through Computing

12 15 3533 42 45 51 7362 75 86 98

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…

Page 8: 26. Divide and Conquer Algorithms

Insight Through Computing

12 15 3533 42 45 51 7362 75 86 98v

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

Page 9: 26. Divide and Conquer Algorithms

Insight Through Computing

12 15 3533 42 45 51 7362 75 86 98v

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

Page 10: 26. Divide and Conquer Algorithms

Insight Through Computing

12 15 3533 42 45 51 7362 75 86 98v

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

Page 11: 26. Divide and Conquer Algorithms

Insight Through Computing

12 15 3533 42 45 51 7362 75 86 98v

L:

Mid:

R:

8

8

9

1 2 3 4 5 6 7 8 9 10 11 12

Done becauseR-L = 1

Binary search: target x = 70

Page 12: 26. Divide and Conquer Algorithms

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; endend

Page 13: 26. Divide and Conquer Algorithms

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

Page 14: 26. Divide and Conquer Algorithms

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…

Page 15: 26. Divide and Conquer Algorithms

Insight Through Computing

Subdivide the sorting task

J NR CP DF LA QB KM GH E

A QB KM GH E J NR CP DF L

Page 16: 26. Divide and Conquer Algorithms

Insight Through Computing

Subdivide again

A QB KM GH E J NR CP DF L

M GH E A QB K P DF L J NR C

Page 17: 26. Divide and Conquer Algorithms

Insight Through Computing

And again

M GH E A QB K P DF L J NR C

M GH E A QB K P DF L J NR C

Page 18: 26. Divide and Conquer Algorithms

Insight Through Computing

And one last time

J NR CP DF LA QB KM GH E

Page 19: 26. Divide and Conquer Algorithms

Insight Through Computing

Now merge

G ME H A QB K D PF L J NC R

J NR CP DF LA QB KM GH E

Page 20: 26. Divide and Conquer Algorithms

Insight Through Computing

And merge again

H ME G K QA B L PD F N RC J

G ME H A QB K D PF L J NC R

Page 21: 26. Divide and Conquer Algorithms

Insight Through Computing

And again

M QH KE GA B P RL NF JC D

H ME G K QA B L PD F N RC J

Page 22: 26. Divide and Conquer Algorithms

Insight Through Computing

And one last time

M QH KE GA B P RL NF JC D

E FC DA B J KG H N PL M Q R

Page 23: 26. Divide and Conquer Algorithms

Insight Through Computing

Done!

E FC DA B J KG H N PL M Q R

Page 24: 26. Divide and Conquer Algorithms

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

Page 25: 26. Divide and Conquer Algorithms

Insight Through Computing

The central sub-problem is the merging of two sorted arrays into one single sorted array

12 33 4535

15 42 6555 75

12 15 3533 42 45 55 7565

Page 26: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

x:

y:

z:

1

1

1

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 27: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12

x:

y:

z:

1

1

1

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

Page 28: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12

x:

y:

z:

2

1

2

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 29: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15

x:

y:

z:

2

1

2

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) NO

Page 30: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15

x:

y:

z:

2

2

3

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 31: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 33

x:

y:

z:

2

2

3

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

Page 32: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 33

x:

y:

z:

3

2

4

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 33: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533

x:

y:

z:

3

2

4

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

Page 34: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533

x:

y:

z:

4

2

5

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 35: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42

x:

y:

z:

4

2

5

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) NO

Page 36: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42

x:

y:

z:

4

3

5

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) ???

Page 37: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45

x:

y:

z:

4

3

5

ix:

iy:

iz:

Merge

ix<=4 and iy<=5: x(ix) <= y(iy) YES

Page 38: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45

x:

y:

z:

5

3

6

ix:

iy:

iz:

Merge

ix > 4

Page 39: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45 55

x:

y:

z:

5

3

6

ix:

iy:

iz:

Merge

ix > 4: take y(iy)

Page 40: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45 55

x:

y:

z:

5

4

8

ix:

iy:

iz:

Merge

iy <= 5

Page 41: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45 55 65

x:

y:

z:

5

4

8

ix:

iy:

iz:

Merge

iy <= 5

Page 42: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45 55 65

x:

y:

z:

5

5

9

ix:

iy:

iz:

Merge

iy <= 5

Page 43: 26. Divide and Conquer Algorithms

12 33 4535

15 42 6555 75

12 15 3533 42 45 55 7565

x:

y:

z:

5

5

9

ix:

iy:

iz:

Merge

iy <= 5

Page 44: 26. Divide and Conquer Algorithms

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

Page 45: 26. Divide and Conquer Algorithms

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

Page 46: 26. Divide and Conquer Algorithms

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; endend% Deal with remaining values in x or y

Page 47: 26. Divide and Conquer Algorithms

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; endendwhile ix<=nx % copy remaining x-values z(iz)= x(ix); ix=ix+1; iz=iz+1;endwhile iy<=ny % copy remaining y-values z(iz)= y(iy); iy=iy+1; iz=iz+1;end

Page 48: 26. Divide and Conquer Algorithms

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

Page 49: 26. Divide and Conquer Algorithms

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

Page 50: 26. Divide and Conquer Algorithms

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!

Page 51: 26. Divide and Conquer Algorithms

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

Page 52: 26. Divide and Conquer Algorithms

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

Page 53: 26. Divide and Conquer Algorithms

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

Page 54: 26. Divide and Conquer Algorithms

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

Page 55: 26. Divide and Conquer Algorithms

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

Page 56: 26. Divide and Conquer Algorithms

Insight Through Computing

Mesh Generation in 3D

Page 57: 26. Divide and Conquer Algorithms

Insight Through Computing

Why is mesh generation a divide & conquer process?

Let’s draw this graphic

Page 58: 26. Divide and Conquer Algorithms

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

Page 59: 26. Divide and Conquer Algorithms

Insight Through Computing

At the Start…

Page 60: 26. Divide and Conquer Algorithms

Insight Through Computing

Recur on this idea: Apply same idea to the lower left triangle

Page 61: 26. Divide and Conquer Algorithms

Insight Through Computing

Recur again

Page 62: 26. Divide and Conquer Algorithms

Insight Through Computing

… and again

Page 63: 26. Divide and Conquer Algorithms

Insight Through Computing

… and again

Page 64: 26. Divide and Conquer Algorithms

Insight Through Computing

Now, climb your way out

Page 65: 26. Divide and Conquer Algorithms

Insight Through Computing

…, etc.

Page 66: 26. Divide and Conquer Algorithms

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 yellowelse % 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