n Tn a Tnb cn aa Tnbb cn
n T(n) = = = = = a. T(n/b) + cn a(a. T(n/b/b) + cn a 2 T(n/b 2) + cna/b + cn a 2 T(n /b 2) + cn(a/b + 1) a 2(a. T(n/b 2/b) + cn/b 2) + cn(a/b + 1) a 3 T(n /b 3) + cn(a 2/b 2) + cn(a/b + 1) a 3 T(n/b 3) + cn(a 2/b 2 + a/b + 1) … ak. T(n/bk) + cn(ak-1/bk-1 + ak-2/bk-2 + … + a 2/b 2 + a/b + 1) 1 1 2 3 k
n So we have q n T(n) = ak. T(n/bk) + cn(ak-1/bk-1 +. . . + a 2/b 2 +a/b+1) To stop the recursion, we should have q q n/bk = 1 n = bk k = logb n T(n)= ak. T(1) + cn(ak-1/bk-1 +. . . + a 2/b 2 + a/b+1) = akc + cn(ak-1/bk-1 +. . . + a 2/b 2 + a/b + 1) = cak + cn(ak-1/bk-1 +. . . + a 2/b 2 + a/b + 1) = cnak/bk + cn(ak-1/bk-1 +. . . +a 2/b 2 +a/b+1) = cn(ak/bk +. . . + a 2/b 2 + a/b + 1) 2
n So with k = logb n q n T(n) = cn(ak/bk +. . . + a 2/b 2 + a/b + 1) What if a = b? q T(n) = cn(1+…+1+1+1) //k+1 times = cn(k + 1) = cn(logb n + 1) = (n logb n) 3
n So with k = logb n q n T(n) = cn(ak/bk +. . . + a 2/b 2 + a/b + 1) What if a < b? q Recall that n (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1) q So: q T(n) = cn · (1) = (n) 4
n So with k = logb n q n T(n) = cn(ak/bk +. . . + a 2/b 2 + a/b + 1) What if a b? q T(n) = cn · (ak / bk) = cn · (alogbn / blogbn) = cn · (alogbn / n) recall logarithm fact: alogbn = nlogba = cn · (nlogba / n) = (cn · nlogba / n) = (nlogba) 5
n So… 6
The Master Method n Based on the Master theorem. n “Cookbook” approach for solving recurrences of the form T(n) = a. T(n/b) + f(n) n n n a 1, b > 1 are constants. f(n) is asymptotically positive. Requires memorization of three cases. 7
The Master Theorem 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) = a. T(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(nlogba– ) for some constant > 0, then T(n) = (nlogba). 2. If f(n) = (nlogba), then T(n) = (nlogbalg n). 3. If f(n) = (nlogba+ ) 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)). 8
The Master Theorem n Given: a divide and conquer algorithm q q n An algorithm that divides the problem of size n into a subproblems, each of size n/b Let the cost of each stage (i. e. , the work to divide the problem + combine solved subproblems) be described by the function f(n) Then, the Master Theorem gives us a cookbook for the algorithm’s running time: 9
The Master Theorem n n if T(n) = a. T(n/b) + f(n) then where a ≥ 1 & b > 1 10
Understanding Master Theorem n n In each of the three cases, we are comparing f(n) with nlogba , the solution to the recurrence is determined by the larger of the two functions. In case 1, if the function nlogba is the larger, then the solution T(n) = Θ(nlogba). n In case 3, if the function f(n) is the larger, then the solution is T(n) = (f(n)). n In case 2, if the two functions are the same size, then the solution is T(n) = Θ(nlogba lg n) = Θ(f(n) lg n). 11
Understanding Master Theorem n In case 1, not only must f(n) be smaller than nlogba, it must be polynomially smaller. That is f(n) must be asymptotically smaller than nlogba by a factor of nε for some constant ε > 0. n In case 3, not only must f(n) be larger than nlogba , it must be polynomially larger and in addition satisfy the “regularity” condition that: a f(n/b) ≤ c f(n). 12
Understanding Master Theorem n It is important to realize that the three cases do not cover all the possibilities for f(n). n There is a gap between cases 1 and 2 when f(n) is smaller than nlogba but not polynomially smaller. n There is a gap between cases 2 and 3 when f(n) is larger than nlogba but not polynomially larger. n If f(n) falls into one of these gaps, or if the regularity condition in case 3 fails to hold, the master method cannot be used to solve the recurrence. 13
- Slides: 13