340 likes | 352 Views
1431227-3 File Organization and Processing Week 13. Divide and Conquer. Divide and Conquer. A Technique for designing algorithm that decompose instance into smaller subinstances of the same problem Solving the sub-instances independently
E N D
1431227-3File Organization and ProcessingWeek 13 Divide and Conquer
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
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.
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.
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.
Applications • Non-obvious applications • Data compression. • Computer graphics. • Computational biology. • Supply chain management. • Book recommendations on Amazon. • Load balancing on a parallel computer. • ....
Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1and S2 of about n/2 elements each Recur: recursively sort S1and S2 Conquer: merge S1and S2 into a unique sorted sequence Merge-Sort Review AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted • according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2)
MERGE-SORTA[1 . . n] • If n = 1, done. • Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ]. • “Merge” the 2 sorted lists. Merge Sort • John von Neumann 1945. Divide Conquer Combine Key subroutine:“Merge”
Merging two sorted arrays 8 4 2 9 6 3
Merging two sorted arrays 8 4 2 9 6 3 2
Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 2
3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 2
3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2
3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2 4
3 Merging two sorted arrays 8 9 6 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2 4
3 Merging two sorted arrays 8 9 6 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2 4 6
Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6
Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6 8
Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 8 9 6 8 9 2 3 4 6 8 9 Time =Q(n)to merge a total of n elements (linear time).
Analyzing merge sort MERGE-SORTA[1 . . n] T(n) Q(1) 2T(n/2) Q(n) • If n = 1, done. • Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ]. • “Merge” the 2 sorted lists
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.
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).
The Recursion Tree • Draw the recursion tree for the recurrence relation and look for a pattern: Total time = bn + bn log n (last level plus all previous levels)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n)
cn T(n/2) T(n/2) Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4) Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 … Q(1)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/2 cn/2 h = lg n cn/4 cn/4 cn/4 cn/4 … Q(1)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 h = lg n cn/4 cn/4 cn/4 cn/4 … Q(1)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn cn/2 h = lg n cn/4 cn/4 cn/4 cn/4 … Q(1)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn cn/2 h = lg n cn/4 cn/4 cn cn/4 cn/4 … … Q(1)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn cn/2 h = lg n cn/4 cn/4 cn cn/4 cn/4 … … Q(1) #leaves = n Q(n)
Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn cn/2 h = lg n cn/4 cn/4 cn cn/4 cn/4 … … Q(1) #leaves = n Q(n) Total = Q(n lg n)