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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值