码迷,mamicode.com
首页 > 其他好文 > 详细

Master Theorem

时间:2015-10-07 06:18:52      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

Master Theorem

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!