1 / 34

1431227-3 File Organization and Processing Week 13

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

pshaddix
Download Presentation

1431227-3 File Organization and Processing Week 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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) Smerge(S1, S2)

  8. 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”

  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 8 4 9 6 3 2

  12. 3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 2

  13. 3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2

  14. 3 Merging two sorted arrays 8 4 2 9 6 3 8 4 9 6 3 8 4 9 6 2 4

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

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

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

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

  19. 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).

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

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

  23. 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)

  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. cn T(n/2) T(n/2) Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

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

  28. 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)

  29. 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)

  30. 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)

  31. 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)

  32. 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)

  33. 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)

  34. 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)

More Related