Title: 26. Divide and Conquer Algorithms
126. 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?
3Key idea of phone book search repeated halving
- To find the page containing Pat Reeds 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
4What 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
5Binary 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.
6Binary 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!
7Binary search target x 70
1 2 3 4 5 6 7 8 9 10 11 12
75
86
98
v
L
1
v(Mid) lt x So throw away the left half
Mid
6
R
12
8Binary search target x 70
1 2 3 4 5 6 7 8 9 10 11 12
75
86
98
v
L
6
x lt v(Mid) So throw away the right half
Mid
9
R
12
9Binary search target x 70
1 2 3 4 5 6 7 8 9 10 11 12
75
86
98
v
L
6
v(Mid) lt x So throw away the left half
Mid
7
R
9
10Binary search target x 70
1 2 3 4 5 6 7 8 9 10 11 12
75
86
98
v
L
7
v(Mid) lt x So throw away the left half
Mid
8
R
9
11Binary search target x 70
1 2 3 4 5 6 7 8 9 10 11 12
75
86
98
v
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) lt ... lt x(n)
- where x(1) lt a lt x(n)
- L is the index such that x(L) lt a lt x(L1)
-
- L 1 R length(x)
- x(L) lt a lt x(R)
- while R-L gt 1
- mid floor((LR)/2)
- Note that mid does not equal L or R.
- if a lt x(mid)
- x(L) lt a lt x(mid)
- R mid
- else
- x(mid) lt a lt x(R)
- L mid
- end
- end
13Binary 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...
- Lets look at merge sort
- An example of the divide and conquer approach
14Merge sort Motivation
- If I have two helpers, Id
- 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
15Subdivide the sorting task
16Subdivide again
17 And again
A
Q
P
D
F
L
J
N
R
C
18 And one last time
J
N
R
C
P
D
F
L
A
Q
B
K
M
G
H
E
19 Now merge
A
Q
D
P
F
L
J
N
C
R
J
N
R
C
P
D
F
L
A
Q
B
K
M
G
H
E
20 And merge again
A
Q
D
P
F
L
J
N
C
R
21 And again
22 And one last time
Q
R
23 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 n1
- y x
- else
- m floor(n/2)
- yL mergeSortL(x(1m))
- yR mergeSortR(x(m1n))
- y merge(yL,yR)
- end
25The central sub-problem is the merging of two
sorted arrays into one single sorted array
26Merge
x
1
ix
iy
1
y
1
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
27Merge
x
1
ix
iy
1
y
1
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
28Merge
x
2
ix
iy
1
y
2
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
29Merge
x
2
ix
iy
1
y
2
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) NO
30Merge
x
2
ix
iy
2
y
3
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
31Merge
x
2
ix
iy
2
y
3
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
32Merge
x
3
ix
iy
2
y
4
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
33Merge
x
3
ix
iy
2
y
4
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
34Merge
x
4
ix
iy
2
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
35Merge
x
4
ix
iy
2
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) NO
36Merge
x
4
ix
iy
3
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
37Merge
x
4
ix
iy
3
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
38Merge
x
5
ix
iy
3
y
6
iz
z
ix gt 4
39Merge
x
5
ix
iy
3
y
6
iz
z
ix gt 4 take y(iy)
40Merge
x
5
ix
iy
4
y
8
iz
z
iy lt 5
41Merge
x
5
ix
iy
4
y
8
iz
z
iy lt 5
42Merge
x
5
ix
iy
5
y
9
iz
z
iy lt 5
43Merge
x
5
ix
iy
5
y
9
iz
z
iy lt 5
44- function z merge(x,y)
- nx length(x) ny length(y)
- z zeros(1,nxny)
- ix 1 iy 1 iz 1
-
-
-
-
-
-
-
45- function z merge(x,y)
- nx length(x) ny length(y)
- z zeros(1, nxny)
- ix 1 iy 1 iz 1
- while ixltnx iyltny
-
-
-
-
-
- end
- Deal with remaining values in x or y
-
-
46- function z merge(x,y)
- nx length(x) ny length(y)
- z zeros(1, nxny)
- ix 1 iy 1 iz 1
- while ixltnx iyltny
- if x(ix) lt y(iy)
- z(iz) x(ix) ixix1 iziz1
- else
- z(iz) y(iy) iyiy1 iziz1
- 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, nxny)
- ix 1 iy 1 iz 1
- while ixltnx iyltny
- if x(ix) lt y(iy)
- z(iz) x(ix) ixix1 iziz1
- else
- z(iz) y(iy) iyiy1 iziz1
- end
- end
- while ixltnx copy remaining x-values
- z(iz) x(ix) ixix1 iziz1
- end
- while iyltny copy remaining y-values
- z(iz) y(iy) iyiy1 iziz1
- 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 n1
- y x
- else
- m floor(n/2)
- yL mergeSortL(x(1m))
- yR mergeSortR(x(m1n))
- 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 n1
- y x
- else
- m floor(n/2)
- yL mergeSortL_L(x(1m))
- yR mergeSortL_R(x(m1n))
- 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 n1
- y x
- else
- m floor(n/2)
- yL mergeSortL_L_L(x(1m))
- yR mergeSortL_L_R(x(m1n))
- y merge(yL,yR)
- end
There should be just one mergeSort function!
51- 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 n1
- y x
- else
- m floor(n/2)
- yL mergeSort(x(1m))
- yR mergeSort(x(m1n))
- y merge(yL,yR)
- end
52- function ymergeSort(x)
- nlength(x)
- if n1
- yx
- else
- mfloor(n/2)
- yLmergeSort(x(1m))
- yRmergeSort(x(m1n))
- ymerge(yL,yR)
- end
53- function ymergeSort(x)
- nlength(x)
- if n1
- yx
- else
- mfloor(n/2)
- yLmergeSort(x(1m))
- yRmergeSort(x(m1n))
- ymerge(yL,yR)
- end
54Divide-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
55Mesh 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.
56Mesh Generation in 3D
57Why is mesh generation a divide conquer process?
58The basic operation
- if the triangle is big enough
- Connect the midpoints.
- Color the interior triangle mauve.
- else
- Color the whole triangle yellow.
- end
59At the Start
60Recur on this idea Apply same idea to the
lower left triangle
61Recur again
62 and again
63 and again
64Now, climb your way out
65, etc.
66function drawTriangle(x,y,level) Draw
recursively colored triangles. x,y are
3-vectors that define the vertices of a
triangle. if level5 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),level1) drawTriangle(x(2) a(2)
a(1),y(2) b(2) b(1),level1)
drawTriangle(x(3) a(3) a(2),y(3) b(3)
b(2),level1) end