简体   繁体   English

求解递归 T(n) = 2T(n/2) + n^4

[英]Solving a recurrence T(n) = 2T(n/2) + n^4

I am studying using the MIT Courseware and the CLRS book Introduction to Algorithms.我正在使用 MIT 课件和 CLRS 书《算法导论》进行学习。

I am currently trying to solve the recurrence (from page 107)我目前正在尝试解决重复问题(从第 107 页开始)

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

If I make a recurrence tree, I get:如果我制作一个循环树,我会得到:

Level 0: n 4 0 级:n 4

Level 1 2(n/2) 4级别 1 2(n/2) 4

Level 2 4(n/4) 4级别 2 4(n/4) 4

Level 3 8(n/8) 4级别 3 8(n/8) 4

The tree has lg(n) levels.树有 lg(n) 层。 Therefore I think that the recurrence should be因此我认为复发应该是

T(n) = Θ(n 4 lg n) T(n) = Θ(n 4 lg n)

But, If I use the master theorem, I get that但是,如果我使用主定理,我会明白

T(n) = Θ(n 4 ) T(n) = Θ(n 4 )

Clearly both of these can't be right.显然这两种方法都不对。 Which one is correct?哪一个是正确的? And where did I go wrong with my reasoning?我的推理哪里出了问题?

The second one looks correct.第二个看起来是正确的。 Notice that your recurrence tree looks like请注意,您的重复树看起来像

n 4 + 2(n/2) 4 + 4(n/4) 4 + ... + 2 i (n / 2 i ) 4 n 4 + 2(n/2) 4 + 4(n/4) 4 + ... + 2 i (n / 2 i ) 4

But 2(n/2) 4 ≠ n 4 , because (n/2) 4 = n 4 / 16, and so 2(n/2) 4 = n 4 /8.但是 2(n/2) 4 ≠ n 4 ,因为 (n/2) 4 = n 4 / 16,所以 2(n/2) 4 = n 4 /8。 In fact, if you work out the math, you get that the work being done at level i is given by事实上,如果你计算出数学,你会得到在第 i 级完成的工作由下式给出

n 4 / (2 -3i ) n 4 / (2 -3i )

So we get (1 + 1/8 + 1/64 + 1/512 + ... ) n 4 , which can be shown to be less than 2n 4 .所以我们得到 (1 + 1/8 + 1/64 + 1/512 + ... ) n 4 ,可以证明它小于 2n 4 So your function is Θ(n 4 ).所以你的函数是 Θ(n 4 )。

With recursion it is Θ(n^4)递归是 Θ(n^4)

T(n) = 2*T(n/2) + n^4 
T(n) = 2( 2*T(n/4) + (n/2)^4) + n^4 = 4*T(n/4) + 2*(n/2)^4 + n^4
T(n) = 4(2*T(n/8) + (n/4)^4) + 2*(n/2)^4 + n^4 = 8*T(n/8) + 4*(n/4)^4 + 2(n/2)^4 + n^4

T(n) = 8*T(n/8) + n^4*(1 + 1/(2^3) + 1/(2^6))
...

T(n) = 2^k*T(n/(2^k)) + n^4*(1+ 1/(2^3) + 1/(2^6) + 1/(2^9)...+ 1/((2^(k-1))^3)

We know T(1) = 1

n = 2^k so k = log2(n) Then

T(n) = n*T(1) + n^4*( 1 - (1/(2^3))^k)/(1-1/8)

T(n) = n + (8/7)*n^4*(1 - n^(-3))

T(n) = n + (8/7)*(n^4 - n)

T(n) = (8/7)*n^4 - (1/7)*n


Θ(T(n)) = Θ((8/7)*n^4 - (1/7)*n)
Θ(T(n)) = Θ(n^4)

it is Θ(n^4)它是Θ(n^4)

You can use the master theorem here directly.你可以在这里直接使用主定理。

This equation fits in case 1 of master theorem where log (a) base b < f( n)这个方程适合主定理的情况 1,其中log (a) base b < f( n)

a : Number of recurrence b : Number of subparts a : 重复次数 b : 子部分数

log a base b = log 2 base 2 = 1 < n^4

Therefore by masters theorem, T(n) = theta(f(n)) = theta(n^4)因此根据主人定理, T(n) = theta(f(n)) = theta(n^4)

By Master Theorem由主定理

a=2, b=2, f(n)=n^4 a=2, b=2, f(n)=n^4

First step - Calculate n^(log a to base b) => n(log 2 to base 2) = n*1 = n Second Step - Is f(n)> First step result => n^4> n => YES This means use Case 3 of Master Theorem.第一步 - 计算 n^(log a to base b) => n(log 2 to base 2) = n*1 = n 第二步 - 是 f(n)> 第一步结果 => n^4> n =>是这意味着使用主定理的案例 3。

Third step - Check Regularity Condition第三步 - 检查规律性条件

a. f(n/b) <= c. f(n) where c>1 
a(n/b) . log(n/b) <= c. f(n)
2.(n/2) . log(n/2) <= c. n^4
n.log(n/2) <= c.n^4

Yes, Regularity condition is met, so our solution must be.是的,正则性条件满足,所以我们的解一定是。

T(n) =theta (f(n)) = theta(n^4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM