divide and conquer
play

Divide-and-Conquer Divide-and-conquer. Break up problem into - PowerPoint PPT Presentation

Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage. Break up problem of size n into two equal parts of


  1. Divide-and-Conquer Divide-and-conquer.  Break up problem into several parts.  Solve each part recursively.  Combine solutions to sub-problems into overall solution. Most common usage.  Break up problem of size n into two equal parts of size ½ n.  Solve two parts recursively.  Combine two solutions into overall solution in linear time. Consequence.  Brute force: n 2 .  Divide-and-conquer: n log n. Divide et impera. Veni, vidi, vici. - Julius Caesar 2

  2. 5.1 Mergesort

  3. Sorting Sorting. Given n elements, rearrange in ascending order. Obvious sorting applications. Non-obvious sorting applications. List files in a directory. Data compression. Organize an MP3 library. Computer graphics. List names in a phone book. Interval scheduling. Display Google PageRank Computational biology. results. Minimum spanning tree. Supply chain management. Problems become easier once Simulate a system of sorted. particles. Find the median. Book recommendations on Find the closest pair. Amazon. Binary search in a Load balancing on a parallel database. computer. Identify statistical . . . outliers. Find duplicates in a mailing list. 4

  4. Mergesort Mergesort.  Divide array into two halves.  Recursively sort each half.  Merge two halves to make sorted whole. Jon von Neumann (1945) A L G O R I T H M S divide O(1) A L G O R I T H M S sort 2T(n/2) A G L O R H I M S T merge O(n) A G H I L M O R S T 5

  5. Merging Merging. Combine two pre-sorted lists into a sorted whole. How to merge efficiently?  Linear number of comparisons.  Use temporary array. A G L O R H I M S T A G H I Challenge for the bored. In-place merge. [Kronrud, 1969] using only a constant amount of extra storage 6

  6. A Useful Recurrence Relation Def. T(n) = number of comparisons to mergesort an input of size n. Mergesort recurrence. if n = 1  0  T( n ) T ( n /2 ) T n /2 n ( )   otherwise ≤  +   + { 1 2 4 4 3 1 2 4 4 3  merging  solve left half solve right half Solution. T(n) = O(n log 2 n). Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace ≤ with =. 7

  7. Proof by Recursion Tree if n = 1  0  T( n ) = 2 T ( n /2) n  otherwise + { 1 2 4 4 3   merging sorting both halves T(n) n 2(n/2) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) 4(n/4) T(n/4) log 2 n . . . 2 k (n / 2 k ) T(n / 2 k ) . . . T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) n/2 (2) n log 2 n 8

  8. Proof by Telescoping Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. assumes n is a power of 2 if n = 1  0  T( n ) = 2 T ( n /2) n  otherwise + { 1 2 4 4 3   merging sorting both halves Pf. For n > 1: T ( n ) 2 T ( n /2) + 1 = n n T ( n /2) = + 1 n /2 T ( n /4) + 1 + 1 = n /4 L T ( n / n ) + 1 + L + 1 = 1 2 4 4 3 n / n log 2 n log 2 n = 9

  9. Proof by Induction Claim. If T(n) satisfies this recurrence, then T(n) = n log 2 n. assumes n is a power of 2 if n = 1  0  T( n ) = 2 T ( n /2) n  otherwise + { 1 2 4 4 3   merging sorting both halves Pf. (by induction on n)  Base case: n = 1.  Inductive hypothesis: T(n) = n log 2 n.  Goal: show that T(2n) = 2n log 2 (2n). T (2 n ) 2 T ( n ) + 2 n = 2 n log 2 n + 2 n = 2 n log 2 (2 n ) − 1 ) + 2 n ( = 2 n log 2 (2 n ) = 10

  10. Analysis of Mergesort Recurrence Claim. If T(n) satisfies the following recurrence, then T(n) ≤ n  lg n  . log 2 n if n = 1  0  T( n ) T n /2 + T n /2 n ( ) ( )   otherwise ≤  4 +   { 1 2 4 4 3 1 2 4 3  merging  solve left half solve right half Pf. (by induction on n)  Base case: n = 1.  Define n 1 =  n / 2  , n 2 =  n / 2  .  Induction step: assume true for 1, 2, ... , n–1. T ( n ) T ( n 1 ) + T ( n 2 ) + n ≤ n 2 n /2   = n 1 lg n 1  + n 2 lg n 2  + n   ≤  / 2 lg n    2 ≤ n 1 lg n 2  + n 2 lg n 2  + n   ≤  / 2 lg n  2 = n lg n 2  + n  = ⇒ lg n 2 ≤ lg n   − 1 n ( lg n  − 1 ) + n  ≤ n lg n   = 11

Recommend


More recommend