2_Divide and Conquer
-
Upload
abeera-gohar -
Category
Documents
-
view
220 -
download
0
Transcript of 2_Divide and Conquer
-
7/30/2019 2_Divide and Conquer
1/54
Divide and Conquer
-
7/30/2019 2_Divide and Conquer
2/54
Recommended Reading
Chapter 2 from the textbookIntroduction to Algorithms
-
7/30/2019 2_Divide and Conquer
3/54
Divide and Conquer
Divide
Break the problem into smaller sub problems
Conquer
Solve the sub problems
Generally, this involves waiting for the problem to be
small enough that it is trivial to solve
Combine
Given the results of the solved sub problems, combinethem to generate a solution for the complete problem
-
7/30/2019 2_Divide and Conquer
4/54
Divide and Conquer: some thoughts
Often, the sub-problem is the same as the original problem
Dividing the problem in half frequently does the job
May have to get creative about how the data is split
Splitting tends to generate run times with log n in them
-
7/30/2019 2_Divide and Conquer
5/54
Divide and Conquer: Sorting
How should we split the data?
What are the sub problems we need to solve?
How do we combine the results from these sub problems?
-
7/30/2019 2_Divide and Conquer
6/54
MergeSort
-
7/30/2019 2_Divide and Conquer
7/54
MergeSort: Merge
Assuming L and R are sorted already, merge
the two to create a single sorted array
-
7/30/2019 2_Divide and Conquer
8/54
Merge
R: 2 4 6 7L: 1 3 5 8
-
7/30/2019 2_Divide and Conquer
9/54
Merge
R: 2 4 6 7L: 1 3 5 8
B:
-
7/30/2019 2_Divide and Conquer
10/54
Merge
R: 2 4 6 7L: 1 3 5 8
B:
-
7/30/2019 2_Divide and Conquer
11/54
Merge
R: 2 4 6 7L: 1 3 5 8
B:
-
7/30/2019 2_Divide and Conquer
12/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1
-
7/30/2019 2_Divide and Conquer
13/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1
-
7/30/2019 2_Divide and Conquer
14/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2
-
7/30/2019 2_Divide and Conquer
15/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2
-
7/30/2019 2_Divide and Conquer
16/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 3
-
7/30/2019 2_Divide and Conquer
17/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 3
-
7/30/2019 2_Divide and Conquer
18/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 3 4
-
7/30/2019 2_Divide and Conquer
19/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 12 3 4
-
7/30/2019 2_Divide and Conquer
20/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 12 3 4 5
-
7/30/2019 2_Divide and Conquer
21/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 3 4 5
-
7/30/2019 2_Divide and Conquer
22/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 3 4 5 6
-
7/30/2019 2_Divide and Conquer
23/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 34 5 6
-
7/30/2019 2_Divide and Conquer
24/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 34 5 6 7
-
7/30/2019 2_Divide and Conquer
25/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 34 5 6 7
-
7/30/2019 2_Divide and Conquer
26/54
Merge
R: 2 4 6 7L: 1 3 5 8
B: 1 2 34 5 6 7 8
-
7/30/2019 2_Divide and Conquer
27/54
Merge
Does the algorithm terminate?
-
7/30/2019 2_Divide and Conquer
28/54
Merge
Is it correct?
Loop invariant: At the end of each iteration of the for
loop of lines 4-10 the subarray B[1..k] contains thesmallestkelements from L and R in sorted order.
-
7/30/2019 2_Divide and Conquer
29/54
Merge
Is it correct?
Loop invariant: At the end of each iteration of thefor loop of lines 4-10 the subarray B[1..k]contains the smallestkelements from L and R insorted order.
-
7/30/2019 2_Divide and Conquer
30/54
Merge
Running time?
-
7/30/2019 2_Divide and Conquer
31/54
Merge
Running time? (n) - linear
-
7/30/2019 2_Divide and Conquer
32/54
Merge-Sort: Divide
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
-
7/30/2019 2_Divide and Conquer
33/54
Merge-Sort: Divide
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 38 5
-
7/30/2019 2_Divide and Conquer
34/54
Merge-Sort: Divide
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 38 5
8 5
-
7/30/2019 2_Divide and Conquer
35/54
Merge-Sort: Conquer
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 38 5
8 5
Sorting a list of
one element is
easy
-
7/30/2019 2_Divide and Conquer
36/54
Merge-Sort: Combine
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 35 8
8 5
-
7/30/2019 2_Divide and Conquer
37/54
Merge-Sort: Divide
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 35 8
1 3
-
7/30/2019 2_Divide and Conquer
38/54
Merge-Sort: Divide
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 35 8
1 3
-
7/30/2019 2_Divide and Conquer
39/54
Merge-Sort: Merge
8 5 1 3 6 2 7 4
6 2 7 48 5 1 3
1 35 8
1 3
-
7/30/2019 2_Divide and Conquer
40/54
Merge-Sort: Merge
8 5 1 3 6 2 7 4
6 2 7 41 3 5 8
1 35 8
-
7/30/2019 2_Divide and Conquer
41/54
Merge-Sort
8 5 1 3 6 2 7 4
6 2 7 41 3 5 8
-
7/30/2019 2_Divide and Conquer
42/54
Merge-Sort
8 5 1 3 6 2 7 4
2 4 6 71 3 5 8
-
7/30/2019 2_Divide and Conquer
43/54
Merge-Sort: Merge
1 2 3 4 5 6 7 8
2 4 6 71 3 5 8
Done!
-
7/30/2019 2_Divide and Conquer
44/54
MergeSort
Running time?
otherwise)()()2/(2
smallisif)(
nCnDnT
ncnT
D(n): cost of splitting (dividing) the dataC(n): cost of merging/combining the data
-
7/30/2019 2_Divide and Conquer
45/54
MergeSort
Running time?
otherwise)()()2/(2
smallisif)(
nCnDnT
ncnT
D(n): cost of splitting (dividing) the data - linear (n)C(n): cost of merging/combining the data linear (n)
-
7/30/2019 2_Divide and Conquer
46/54
MergeSort
Running time?
otherwise)2/(2
smallisif)(
cnnT
ncnT
Which is?
M S t
-
7/30/2019 2_Divide and Conquer
47/54
MergeSort
cn
T(n/2) T(n/2)
otherwise)2/(2
smallisif)(
cnnT
ncnT
M S t
-
7/30/2019 2_Divide and Conquer
48/54
MergeSort
cn
T(n/4) T(n/4) T(n/4) T(n/4)
cn/2 cn/2
otherwise)2/(2
smallisif)(
cnnT
ncnT
M S t
-
7/30/2019 2_Divide and Conquer
49/54
MergeSort
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)
otherwise)2/(2
smallisif)(
cnnT
ncnT
M S t
-
7/30/2019 2_Divide and Conquer
50/54
MergeSort
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8
c c c c c c c c c c c
otherwise)2/(2
smallisif)(
cnnT
ncnT
M S t
-
7/30/2019 2_Divide and Conquer
51/54
MergeSort
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8
c c c c c c c c c c c
cn
cn
cn
cn
cn
otherwise)2/(2
smallisif)(
cnnT
ncnT
M S t
-
7/30/2019 2_Divide and Conquer
52/54
MergeSort
otherwise)2/(2
smallisif)(
cnnT
ncnT
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8
c c c c c c c c c c c
cn
cn
cn
cn
cn
Depth? M S t
-
7/30/2019 2_Divide and Conquer
53/54
MergeSort
We can calculate the depth, by determining when
the recursion is c At each level, we divide by 2
12
d
n
nd 2
nd
log2log
nd log2log
nd 2log
-
7/30/2019 2_Divide and Conquer
54/54
MergeSort
Running time?
Each level costs cn
log n levels
cn log n = (n log n )
otherwise)2/(2
smallisif)(
cnnT
ncnT