Master Theorem

Master theorem provides a solution in asymptotic terms to solve time complexity problem of most divide and conquer algorithms.

Recurrence relations of the form:

T(n) = a T(n/b) + f(n) where a >= 1 and b > 1

Case 1:

f(n) = O(nc) where c < loga

Then: T(n) = Θ(nlogb a)

Case 2:

f(n) = Θ(nlogkn) where c = logb a
Then: T(n) = Θ(nc logk+1n)

Case 3:

f(n) = Ω(nc) where c > logb a

a f(n/b) <= k f(n) for some constant k < 1 and sufficiently large n

Then: T(n) = Θ(f(n)) 

Examples


 1. T(n) = 2 T(n/2) + n2

 a = 2, b = 2, f(n) = n -> c = 2 > logb a

And 2 (n2 / 4) <= k n2, choosing k = 1/2

2. Binary Search

T(n) = T(n/2) + O(1)

T(n) = O(log n)

3. Merge Sort

T(n) = 2 T(n/2) + O(n)

T(n) = O(n log n)

Notes


There are some conditions where we cannot apply master theorem.

1. a < 1 or b < 1

2. f(n) is not positive

3. f(n) = n / (log n)

Because 1/(log n) < nε for any constant ε > 0.

原文地址:https://www.cnblogs.com/ireneyanglan/p/4858153.html