Presentation is loading. Please wait.

Presentation is loading. Please wait.

Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.

Similar presentations


Presentation on theme: "Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill."— Presentation transcript:

1 Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill

2 COT5407 The Master Method  Based on the Master theorem.  “Cookbook” approach for solving recurrences of the form T(n) = aT(n/b) + f(n) a  1, b > 1 are constants. f(n) is asymptotically positive. n/b may not be an integer, but we ignore floors and ceilings.  Requires memorization of three cases.

3 COT5407 The Master Theorem Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by  n/b  or  n/b . T(n) can be bounded asymptotically in three cases: 1.If f(n) = O(n log b a–  ) for some constant  > 0, then T(n) =  ( n log b a ). 2.If f(n) =  (n log b a ), then T(n) =  ( n log b a lg n). 3.If f(n) =  (n log b a+  ) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) =  (f(n)). Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by  n/b  or  n/b . T(n) can be bounded asymptotically in three cases: 1.If f(n) = O(n log b a–  ) for some constant  > 0, then T(n) =  ( n log b a ). 2.If f(n) =  (n log b a ), then T(n) =  ( n log b a lg n). 3.If f(n) =  (n log b a+  ) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) =  (f(n)).

4 COT5407 Recursion tree view f(n)f(n) af(n/b) a 2 f(n/b 2 )  (n log b a ) Total:

5 COT5407 The Master Theorem Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by  n/b  or  n/b . T(n) can be bounded asymptotically in three cases: 1.If f(n) = O(n log b a–  ) for some constant  > 0, then T(n) =  ( n log b a ). 2.If f(n) =  (n log b a ), then T(n) =  ( n log b a lg n). 3.If f(n) =  (n log b a+  ) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) =  (f(n)). Theorem 4.1 Let a  1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by  n/b  or  n/b . T(n) can be bounded asymptotically in three cases: 1.If f(n) = O(n log b a–  ) for some constant  > 0, then T(n) =  ( n log b a ). 2.If f(n) =  (n log b a ), then T(n) =  ( n log b a lg n). 3.If f(n) =  (n log b a+  ) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) =  (f(n)).

6 COT5407 Master Method – Examples  T(n) = 16T(n/4)+n  a = 16, b = 4, n log b a = n log 4 16 = n 2.  f(n) = n = O(n log b a-  ) = O(n 2-  ), where  = 1  Case 1.  Hence, T(n) =  (n log b a ) =  (n 2 ).  T(n) = T(3n/7) + 1  a = 1, b=7/3, and n log b a = n log 7/3 1 = n 0 = 1  f(n) = 1 =  (n log b a )  Case 2.  Therefore, T(n) =  (n log b a lg n) =  (lg n)

7 COT5407 Master Method – Examples  T(n) = 3T(n/4) + n lg n  a = 3, b=4, thus n log b a = n log 4 3 = O(n 0.793 )  f(n) = n lg n =  (n log 4 3 +  ) where   0.2  Case 3.  Therefore, T(n) =  (f(n)) =  (n lg n).  T(n) = 2T(n/2) + n lg n  a = 2, b=2, f(n) = n lg n, and n log b a = n log 2 2 = n  f(n) is asymptotically larger than n log b a, but not polynomially larger. The ratio lg n is asymptotically less than n  for any positive . Thus, the Master Theorem doesn’t apply here.

8 COT5407 Master Theorem – What it means?  Case 1: If f(n) = O(n log b a–  ) for some constant  > 0, then T(n) =  ( n log b a ).  n log b a = a log b n : Number of leaves in the recursion tree.  f(n) = O(n log b a–  )  Sum of the cost of the nodes at each internal level asymptotically smaller than the cost of leaves by a polynomial factor.  Cost of the problem dominated by leaves, hence cost is  ( n log b a ).

9 COT5407 Master Theorem – What it means?  Case 2: If f(n) =  (n log b a ), then T(n) =  ( n log b a lg n).  n log b a = a log b n : Number of leaves in the recursion tree.  f(n) =  (n log b a )  Sum of the cost of the nodes at each level asymptotically the same as the cost of leaves.  There are  (lg n) levels.  Hence, total cost is  ( n log b a lg n).

10 COT5407 Master Theorem – What it means?  Case 3: If f(n) =  (n log b a+  ) for some constant  > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b)  c f(n), then T(n) =  (f(n)).  n log b a = a log b n : Number of leaves in the recursion tree.  f(n) =  (n log b a+  )  Cost is dominated by the root. Cost of the root is asymptotically larger than the sum of the cost of the leaves by a polynomial factor.  Hence, cost is  ( f(n)).

11 9/9/08 11

12 COT5407 Master Theorem – Proof for exact powers  Proof when n is an exact power of b.  Three steps. 1.Reduce the problem of solving the recurrence to the problem of evaluating an expression that contains a summation. 2.Determine bounds on the summation. 3.Combine 1 and 2.

13 13 Iterative “Proof” of the Master Theorem  Using iterative substitution, let us see if we can find a pattern:  We then distinguish the three cases as  The first term is dominant  Each part of the summation is equally dominant  The summation is a geometric series

14 COT5407 f(n)f(n) af(n/b) a 2 f(n/b 2 )  (n log b a ) Total:

15 Integer Multiplication  Algorithm: Multiply two n-bit integers I and J.  Divide step: Split I and J into high-order and low-order bits  We can then define I*J by multiplying the parts and adding:  So, T(n) = 4T(n/2) + n, which implies T(n) is O(n 2 ).  But that is no better than the algorithm we learned in grade school.

16 An Improved Integer Multiplication Algorithm  Algorithm: Multiply two n-bit integers I and J.  Divide step: Split I and J into high-order and low-order bits  Observe that there is a different way to multiply parts:  So, T(n) = 3T(n/2) + n, which implies T(n) is O(n log 2 3 ), by the Master Theorem.  Thus, T(n) is O(n 1.585 ).


Download ppt "Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill."

Similar presentations


Ads by Google