Download presentation
Presentation is loading. Please wait.
Published byIsaac Fox Modified over 9 years ago
1
1 1431227-3 File Organization and Processing Week 13 Divide and Conquer
2
2 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
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
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
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
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
7 Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: –Divide: partition S into two sequences S 1 and S 2 of about n 2 elements each –Recur: recursively sort S 1 and S 2 –Conquer: merge S 1 and S 2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S 1, S 2 ) partition(S, n/2) mergeSort(S 1, C) mergeSort(S 2, C) S merge(S 1, S 2 )
8
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
9 Merging two sorted arrays 842842 963963
10
10 Merging two sorted arrays 842842 963963 2
11
11 Merging two sorted arrays 842842 963963 2 8484 963963
12
12 3 Merging two sorted arrays 842842 963963 2 8484 963963
13
13 3 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696
14
14 4 3 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696
15
15 4 3 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696 89696
16
16 4 3 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696 89696 6
17
17 43 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696 89696 6 89
18
18 43 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696 89696 6 89 8
19
19 43 Merging two sorted arrays 842842 963963 2 8484 963963 8484 9696 89696 6 89 89 Time = (n) to merge a total of n elements (linear time).
20
20 Analyzing merge sort M ERGE -S ORT 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
21 Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n 2 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.
22
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 2 i =n. That is, i = log n. So, Thus, T(n) is O(n log n).
23
23 The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depthT’ssize 01n 12 n2n2 i2i2i n2in2i ……… time bn … Total time = bn + bn log n (last level plus all previous levels)
24
24 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
25
25 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n)T(n)
26
26 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n/2) cn
27
27 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn T(n/4) cn/2
28
28 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) …
29
29 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n
30
30 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n cn
31
31 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n cn
32
32 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n cn …
33
33 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n cn #leaves = n (n)(n) …
34
34 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2 (1) … h = lg n cn #leaves = n (n)(n) Total (n lg n) …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.