Master Theorem
- T(n) = aT(n/b) + f(n), a ≥ 1; b > 1.
- T(n) can be bounded asymptotically as follows:
- If f(n) = O(nlogba/nε)
for som constant ε > 0, then
T(n) = Θ(nlogba).
- If f(n) = Θ(nlogba), then
T(n) = Θ(nlogbalogn).
- If f(n) = Ω(nlogbanε)
for som constant ε > 0,
and if af(n/b) ≤ cf(n) for some constant c < 1
and for all sufficiently large n, then
T(n) = Θ(f(n)).
General Idea
- f(n) is compared with nlogba:
- if f(n) is (polynomially) smaller then nlogba, then T(n) = Θ(nlogba).
- if f(n) is (polynomially) greater then nlogba, then T(n) = Θ(f(n)).
- if f(n) is Θ(nlogba),
then T(n) = Θ(f(n)logn).
Examples
- T(n) = 9T(n/3) + n:
- a = 9.
- b = 3.
- f(n) = n.
- nlogba = nlog39 = n2.
- Since f(n) = O(nlog39-ε) = O(n) for ε=1, case 1 implies that T(n)=Θ(n2).
- T(n) = T(2n/3) + 1:
- a = 1.
- b = 3/2.
- f(n) = 1.
- nlogba = nlog3/21 = n0 = 1.
- Since f(n) = Θ(1), case 2 implies that T(n)=Θ(logn).
- 3T(n/4) + nlogn:
- a = 3.
- b = 4.
- f(n) = nlogn.
- nlogba = O(n0.793).
- Since f(n) = Ω(nlog43+ε), where ε ≅ 0.2, and af(n/b) ≤ cf(n) for c=a/b, case 3 implies that T(n)=Θ(nlogn).