Outline and Reading Divide-and-conquer paradigm (§5.2) Divide-and-Conquer Review Merge-sort (§4.1.1) Recurrence Equations (§5.2.1) � Iterative substitution 7 2 9 4 → 2 4 7 9 � Recursion trees 7 2 → 2 7 9 4 → 4 9 � Guess-and-test � The master method 7 → 7 2 → 2 9 → 9 4 → 4 Integer Multiplication (§5.2.2) Divide-and-Conquer 1 Divide-and-Conquer 2 Divide-and-Conquer Merge-Sort Review Divide-and conquer is a Merge-sort on an input general algorithm design sequence S with n paradigm: Algorithm mergeSort ( S, C ) elements consists of Input sequence S with n � Divide: divide the input data S in three steps: elements, comparator C two or more disjoint subsets S 1 , � Divide: partition S into Output sequence S sorted S 2 , … two sequences S 1 and S 2 according to C � Recur: solve the subproblems of about n / 2 elements recursively if S.size () > 1 each � Conquer: combine the solutions ( S 1 , S 2 ) ← partition ( S , n /2) � Recur: recursively sort S 1 for S 1 , S 2 , …, into a solution for S mergeSort ( S 1 , C ) and S 2 The base case for the mergeSort ( S 2 , C ) � Conquer: merge S 1 and recursion are subproblems of S ← merge ( S 1 , S 2 ) S 2 into a unique sorted constant size sequence Analysis can be done using recurrence equations Divide-and-Conquer 3 Divide-and-Conquer 4 Recurrence Equation Analysis Iterative Substitution The conquer step of merge-sort consists of merging two sorted In the iterative substitution, or “plug-and-chug,” technique, we sequences, each with n / 2 elements and implemented by means of iteratively apply the recurrence equation to itself and see if we can a doubly linked list, takes at most bn steps, for some constant b . find a pattern: = + T ( n ) 2 T ( n / 2 ) bn Likewise, the basis case ( n < 2) will take at b most steps. = 2 + + 2 ( 2 T ( n / 2 )) b ( n / 2 )) bn Therefore, if we let T ( n ) denote the running time of merge-sort: = 2 2 + 2 T ( n / 2 ) 2 bn < b if n 2 = 3 3 + 2 T ( n / 2 ) 3 bn = T ( n ) = + 2 4 T ( n / 2 4 ) 4 bn + ≥ 2 T ( n / 2 ) bn if n 2 = ... = 2 i T ( n / 2 i ) + ibn We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. Note that base, T(n)=b, case occurs when 2 i =n. That is, i = log n. � That is, a solution that has T ( n ) only on the left-hand side. So, = + T ( n ) bn bn log n Thus, T(n) is O(n log n). Divide-and-Conquer 5 Divide-and-Conquer 6
The Recursion Tree Guess-and-Test Method In the guess-and-test method, we guess a closed form solution Draw the recursion tree for the recurrence relation and look for a and then try to prove it is true by induction: pattern: < < b if n 2 b if n 2 T ( n ) = T ( n ) = + ≥ + ≥ 2 T ( n / 2 ) bn if n 2 2 T ( n / 2 ) bn log n if n 2 Guess: T(n) < cn log n. time depth T’s size T ( n ) = 2 T ( n / 2 ) + bn log n bn 0 1 n = + 2 ( c ( n / 2 ) log( n / 2 )) bn log n bn n / 2 1 2 = − + cn (log n log 2 ) bn log n = − + bn cn log n cn bn log n i 2 i n / 2 i … … … … Wrong: we cannot make this last line be less than cn log n Total time = bn + bn log n (last level plus all previous levels) Divide-and-Conquer 7 Divide-and-Conquer 8 Guess-and-Test Method, Part 2 Master Method Many divide-and-conquer recurrence equations have Recall the recurrence equation: b if n < 2 the form: = T ( n ) < + ≥ c if n d 2 T ( n / 2 ) bn log n if n 2 = T ( n ) Guess #2: T(n) < cn log 2 n. + ≥ aT ( n / b ) f ( n ) if n d = + T ( n ) 2 T ( n / 2 ) bn log n = + 2 ( c ( n / 2 ) log 2 ( n / 2 )) bn log n The Master Theorem: = − + cn (log n log 2 ) 2 bn log n − ε log a Θ log a 1. if f ( n ) is O ( n ), then T ( n ) is ( n ) b b = cn log 2 n − 2 cn log n + cn + bn log n Θ Θ + 2. if f ( n ) is ( n log a log k n ), then T ( n ) is ( n log a log k 1 n ) b b ≤ cn log 2 n � if c > b. Ω log a + ε Θ 3. if f ( n ) is ( n ), then T ( n ) is ( f ( n )), b So, T(n) is O(n log 2 n). ≤ δ δ < provided af ( n / b ) f ( n ) for some 1 . In general, to use this method, you need to have a good guess and you need to be good at induction proofs. Divide-and-Conquer 9 Divide-and-Conquer 10 Master Method, Example 1 Master Method, Example 2 < < c if n d c if n d The form: The form: T ( n ) = T ( n ) = + ≥ + ≥ aT ( n / b ) f ( n ) if n d aT ( n / b ) f ( n ) if n d The Master Theorem: The Master Theorem: log a − ε Θ log a log a − ε Θ log a 1. if f ( n ) is O ( n ), then T ( n ) is ( n ) 1. if f ( n ) is O ( n ), then T ( n ) is ( n ) b b b b Θ log a k Θ log a k + 1 Θ log a k Θ log a k + 1 2. if f ( n ) is ( n log n ), then T ( n ) is ( n log n ) 2. if f ( n ) is ( n log n ), then T ( n ) is ( n log n ) b b b b + ε + ε Ω log a Θ Ω log a Θ 3. if f ( n ) is ( n ), then T ( n ) is ( f ( n )), 3. if f ( n ) is ( n ), then T ( n ) is ( f ( n )), b b provided af ( n / b ) ≤ δ f ( n ) for some δ < 1 . provided af ( n / b ) ≤ δ f ( n ) for some δ < 1 . Example: Example: = + T ( n ) 4 T ( n / 2 ) n = + T ( n ) 2 T ( n / 2 ) n log n Solution: log b a=2, so case 1 says T(n) is O(n 2 ). Solution: log b a=1, so case 2 says T(n) is O(n log 2 n). Divide-and-Conquer 11 Divide-and-Conquer 12
Recommend
More recommend