26. Divide and Conquer Algorithms - PowerPoint PPT Presentation

1 / 66
About This Presentation
Title:

26. Divide and Conquer Algorithms

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. – PowerPoint PPT presentation

Number of Views:296
Avg rating:3.0/5.0
Slides: 67
Provided by: VanL68
Category:

less

Transcript and Presenter's Notes

Title: 26. Divide and Conquer Algorithms


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

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
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
Binary 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
8
Binary 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
9
Binary 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
10
Binary 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
11
Binary 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

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...
  • Lets look at merge sort
  • An example of the divide and conquer approach

14
Merge 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
15
Subdivide the sorting task
16
Subdivide 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

25
The central sub-problem is the merging of two
sorted arrays into one single sorted array
26
Merge
x
1
ix
iy
1
y
1
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
27
Merge
x
1
ix
iy
1
y
1
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
28
Merge
x
2
ix
iy
1
y
2
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
29
Merge
x
2
ix
iy
1
y
2
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) NO
30
Merge
x
2
ix
iy
2
y
3
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
31
Merge
x
2
ix
iy
2
y
3
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
32
Merge
x
3
ix
iy
2
y
4
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
33
Merge
x
3
ix
iy
2
y
4
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
34
Merge
x
4
ix
iy
2
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
35
Merge
x
4
ix
iy
2
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) NO
36
Merge
x
4
ix
iy
3
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) ???
37
Merge
x
4
ix
iy
3
y
5
iz
z
ixlt4 and iylt5 x(ix) lt y(iy) YES
38
Merge
x
5
ix
iy
3
y
6
iz
z
ix gt 4
39
Merge
x
5
ix
iy
3
y
6
iz
z
ix gt 4 take y(iy)
40
Merge
x
5
ix
iy
4
y
8
iz
z
iy lt 5
41
Merge
x
5
ix
iy
4
y
8
iz
z
iy lt 5
42
Merge
x
5
ix
iy
5
y
9
iz
z
iy lt 5
43
Merge
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

54
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
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.
56
Mesh Generation in 3D
57
Why is mesh generation a divide conquer process?
  • Lets draw this graphic

58
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
At the Start
60
Recur on this idea Apply same idea to the
lower left triangle
61
Recur again
62
and again
63
and again
64
Now, climb your way out
65
, etc.
66
function 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
Write a Comment
User Comments (0)
About PowerShow.com