master theorem
play

Master Theorem Carola Wenk Slides courtesy of Charles Leiserson - PowerPoint PPT Presentation

CS 3343 Fall 2010 Master Theorem Carola Wenk Slides courtesy of Charles Leiserson with small Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/16/2010 CS 3343 Analysis of Algorithms 1 The divide-and-conquer


  1. CS 3343 – Fall 2010 Master Theorem Carola Wenk Slides courtesy of Charles Leiserson with small Slides courtesy of Charles Leiserson with small changes by Carola Wenk 9/16/2010 CS 3343 Analysis of Algorithms 1

  2. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. p a subproblems, each of size n/b 2 Conquer the subproblems by solving them 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. 3 C bi b bl l ti Runtime for divide and combine is f(n) 9/16/2010 CS 3343 Analysis of Algorithms 2

  3. Example: merge sort Example: merge sort 1. Divide: Trivial. 2. Conquer: Recursively sort a =2 subarrays of size n /2= n / b y 3. Combine: Linear-time merge, runtime f ( n ) ∈ O ( n ) f ( n ) ∈ O ( n ) T ( n ) = 2 T ( n /2) + O ( n ) subproblem size work dividing k di idi # subproblems and combining T ( n ) = aT ( n / b ) + f(n) 9/16/2010 CS 3343 Analysis of Algorithms 3

  4. The master method The master method The master method applies to recurrences of the form the form T ( n ) = aT ( n / b ) + f ( n ) , where a ≥ 1, b > 1, and f is asymptotically positive. 9/16/2010 CS 3343 Analysis of Algorithms 4

  5. Master theorem (summary) Master theorem (summary) T ( n ) = aT ( n / b ) + f ( n ) C ASE 1 : f ( n ) = O ( n log ba – ε ) ⇒ T ( n ) = Θ ( n log ba ) . ⇒ T ( n ) Θ ( n ) . C ASE 2 : f ( n ) = Θ ( n log ba log k n ) ⇒ T ( n ) = Θ ( n log ba log k +1 n ) . k +1 ) Θ ( l T ( ) l C ASE 3 : f ( n ) = Ω ( n log ba + ε ) and af ( n/b ) ≤ c f ( n ) f ( ) ( ) f ( ) f ( ) for some constant c < 1. ⇒ T ( n ) = Θ ( f ( n )) . Θ ( f ( )) T ( ) 9/16/2010 CS 3343 Analysis of Algorithms 5

  6. Three common cases Three common cases Compare f ( n ) with n log ba : Compare f ( n ) with n : 1. f ( n ) = O ( n log ba – ε ) for some constant ε > 0. • f ( ) gro s pol nomiall slo er than • f ( n ) grows polynomially slower than n log ba log ba (by an n ε factor). Solution: T ( n ) = Θ ( n log ba ) . Θ ( log ba ) S l ti T ( ) 2. f ( n ) = Θ ( n log ba log k n ) for some constant k ≥ 0. f ( ) ( g ) • f ( n ) and n log ba grow at similar rates. Solution: T ( n ) = Θ ( n log ba log k +1 n ) Solution: T ( n ) = Θ ( n log ba log k +1 n ) . 9/16/2010 CS 3343 Analysis of Algorithms 6

  7. Three common cases (cont ) Three common cases (cont.) Compare f ( n ) with n log ba : Compare f ( n ) with n : 3. f ( n ) = Ω ( n log ba + ε ) for some constant ε > 0. • f ( n ) grows polynomially faster than n log ba (by l an n ε factor), and f ( n ) satisfies the regularity condition that af ( n/b ) ≤ c f ( n ) for some constant c < 1. Solution: T ( n ) = Θ ( f ( n )) . 9/16/2010 CS 3343 Analysis of Algorithms 7

  8. Examples Examples E Ex. T ( n ) = 4 T ( n /2) + sqrt( n) T ( ) 4 T ( /2) ( ) a = 4, b = 2 ⇒ n log ba = n 2 ; f ( n ) = sqrt( n). C ASE 1 : f ( n ) = O ( n 2 – ε ) for ε = 1 5 ε ) for ε = 1.5. C ASE 1 : f ( n ) = O ( n 2 ∴ T ( n ) = Θ ( n 2 ). Ex. T ( n ) = 4 T ( n /2) + n 2 a = 4 b = 2 ⇒ n log ba = n 2 ; f ( n ) = n 2 2 ⇒ n a 4, b n ; f ( n ) n . C ASE 2 : f ( n ) = Θ ( n 2 log 0 n ), that is, k = 0. ∴ T ( n ) = Θ ( n 2 log n ). ( ) ( g ) 9/16/2010 CS 3343 Analysis of Algorithms 8

  9. Examples Examples Ex. T ( n ) = 4 T ( n /2) + n 3 Ex. T ( n ) 4 T ( n /2) + n a = 4, b = 2 ⇒ n log ba = n 2 ; f ( n ) = n 3 . C ASE 3 : f ( n ) = Ω ( n 2 + ε ) for ε = 1 f ( ) ( ) and 4( n /2) 3 ≤ cn 3 (reg. cond.) for c = 1/2. ∴ T ( n ) = Θ ( n 3 ). Ex. T ( n ) = 4 T ( n /2) + n 2 /log n a = 4, b = 2 ⇒ n g b = n ; f ( n ) = n /log n. a = 4 b = 2 ⇒ n log ba = n 2 ; f ( n ) = n 2 /log n Master method does not apply. In particular, for every constant ε > 0, we have log n ∈ o ( n ε ). for every constant ε 0, we have log n ∈ o ( n ). 9/16/2010 CS 3343 Analysis of Algorithms 9

  10. Example: merge sort Example: merge sort 1 Divide: Trivial 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear-time merge. T ( n ) = 2 T ( n /2) + O ( n ) T ( n ) = 2 T ( n /2) + O ( n ) work dividing # subproblems subproblem size and combining and combining n log ba = n log 2 2 = n 1 = n ⇒ C ASE 2 ( k = 0) ⇒ T ( n ) = Θ ( n log n ) . ⇒ T ( n ) Θ ( n log n ) . 9/16/2010 CS 3343 Analysis of Algorithms 10

  11. Recurrence for binary search Recurrence for binary search T ( n ) = 1 T ( n /2) + Θ (1) 1 T ( /2) + Θ (1) T ( ) work dividing k di idi # subproblems # b bl and combining subproblem size n log ba = n log 2 1 = n 0 = 1 ⇒ C ASE 2 ( k = 0) n log ba = n log 2 1 = n 0 = 1 ⇒ C ASE 2 ( k = 0) ⇒ T ( n ) = Θ (log n ) . 9/16/2010 CS 3343 Analysis of Algorithms 11

Recommend


More recommend