1 1431227-3 File Organization and Processing Week 13 Divide and Conquer.
Transcript of 1 1431227-3 File Organization and Processing Week 13 Divide and Conquer.
1
1431227-3File Organization and
ProcessingWeek 13
Divide and Conquer
2
Divide and Conquer• A Technique for designing algorithm that
decompose instance into smaller subinstances of the same problem
– Solving the sub-instances independently
– Combining the sub-solutions to obtain the solution of the original instance
3
Divide and Conquer
• Basic Steps:– Divide: the problem into a number of subproblems
that are themselves smaller instances of the same type of problem.
– Conquer: Recursively solving these subproblems. If the subproblems are small enough, solve them straightforward.
– Combine: the solutions to the subproblems into the solution of original problem.
4
Most common usage
• Break up problem of size n into two equal parts of size n/2.
• Solve two parts recursively
• Combine two solutions into overall solution in linear time.
5
Sort• Obviously application
– Sort a list of names.– Organize an MP3 library.– Display Google PageRank results.– List RSS news items in reverse chronological order.
• Problems become easy once items are in sorted order– Find the median.– Find the closest pair.– Binary search in a database.– Identify statistical outliers.– Find duplicates in a mailing list.
6
Applications
• Non-obvious applications– Data compression.– Computer graphics.– Computational biology.– Supply chain management.– Book recommendations on Amazon.– Load balancing on a parallel computer.– ....
7
Merge-Sort Review
• Merge-sort on an input sequence S with n elements consists of three steps:– Divide: partition S into
two sequences S1 and S2 of about n2 elements each
– Recur: recursively sort S1 and S2
– Conquer: merge S1 and S2 into a unique sorted sequence
Algorithm mergeSort(S, C)Input sequence S with n
elements, comparator C Output sequence S sorted
according to Cif S.size() > 1
(S1, S2) partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S merge(S1, S2)
8
Merge Sort
• John von Neumann 1945.MERGE-SORT A[1 . . n]
1. If n = 1, done.2. Recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .3. “Merge” the 2 sorted lists.
Key subroutine: “Merge”
Divide
Conquer Combine
9
Merging two sorted arrays
8
4
2
9
6
3
10
Merging two sorted arrays
8
4
2
9
6
3
2
11
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
12
3
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
13
3
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
14
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
15
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
8 9
6
16
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
8 9
6
6
17
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
89
6
6
8 9
18
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
89
6
6
8 9
8
19
43
Merging two sorted arrays
8
4
2
9
6
3
2
8
4
9
6
3
8
4
9
6
89
6
6
8 9
8 9
Time = (n) to merge a total of n elements (linear time).
20
Analyzing merge sort
MERGE-SORT A[1 . . n]1. If n = 1, done.2. Recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .3. “Merge” the 2 sorted lists
T(n)(1)2T(n/2)
(n)
21
Recurrence Equation Analysis
• The conquer step of merge-sort consists of merging two sorted sequences, each with n2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b.
• Likewise, the basis case (n < 2) will take at b most steps.• Therefore, if we let T(n) denote the running time of merge-sort:
• We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.– That is, a solution that has T(n) only on the left-hand side.
2if)2/(2
2if )(
nbnnT
nbnT
22
Iterative Substitution
• In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern:
• Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n. • So,
• Thus, T(n) is O(n log n).
ibnnT
bnnT
bnnT
bnnT
bnnbnT
bnnTnT
ii
)2/(2
...
4)2/(2
3)2/(2
2)2/(2
))2/())2/(2(2
)2/(2)(
44
33
22
2
nbnbnnT log)(
23
The Recursion Tree
• Draw the recursion tree for the recurrence relation and look for a pattern:
depth T’s size
0 1 n
1 2 n2
i 2i n2i
… … …
2if)2/(2
2if )(
nbnnT
nbnT
time
bn
bn
bn
…
Total time = bn + bn log n(last level plus all previous levels)
24
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
25
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)
26
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n/2) T(n/2)
cn
27
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/4) T(n/4) T(n/4) T(n/4)
cn/2 cn/2
28
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
29
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
30
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
31
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
cn
32
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
cn
cn
…
33
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
cn
cn
#leaves = n (n)
…
34
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
(1)
…
h = lg n
cn
cn
cn
#leaves = n (n)
Total(n lg n)
…