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

发布于 2024-10-10 09:03:51 字数 517 浏览 12 评论 0原文

我正在使用 MIT 课件和 CLRS 书籍算法简介进行学习。

我目前正在尝试解决递归问题(来自第 107 页)

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

如果我制作一个递归树,我得到:

0级:n4

级别 1 2(n/2)4

2级4(n/4)4

3级8(n/8)4

树有 lg(n) 个级别。因此我认为复发应该是

T(n) = θ(n4 lg n)

但是,如果我使用主定理,我会得到

T(n) = θ(n4)

显然这两个都不正确。哪一个是正确的?我的推理哪里出了问题?

I am studying using the MIT Courseware and the CLRS book Introduction to Algorithms.

I am currently trying to solve the recurrence (from page 107)

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

If I make a recurrence tree, I get:

Level 0: n4

Level 1 2(n/2)4

Level 2 4(n/4)4

Level 3 8(n/8)4

The tree has lg(n) levels. Therefore I think that the recurrence should be

T(n) = Θ(n4 lg n)

But, If I use the master theorem, I get that

T(n) = Θ(n4)

Clearly both of these can't be right. Which one is correct? And where did I go wrong with my reasoning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静赏你的温柔 2024-10-17 09:03:51

第二个看起来是正确的。请注意,您的递归树看起来像

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

但 2(n/2)4 ≠ n4,因为 (n/2)4 = n4 / 16,所以 2(n/2)4 = n4< /sup>/8。事实上,如果你计算一下数学,你就会发现在级别 i 所做的工作由下式给出

n4 / (2-3i)

所以我们得到 (1 + 1/8 + 1/64 + 1/512 + ... ) n4< /sup>,可以显示小于 2n4。所以你的函数是 θ(n4)。

The second one looks correct. Notice that your recurrence tree looks like

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

But 2(n/2)4 ≠ n4, because (n/2)4 = n4 / 16, and so 2(n/2)4 = n4/8. In fact, if you work out the math, you get that the work being done at level i is given by

n4 / (2-3i)

So we get (1 + 1/8 + 1/64 + 1/512 + ... ) n4, which can be shown to be less than 2n4. So your function is Θ(n4).

中二柚 2024-10-17 09:03:51

通过递归,它是 θ(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)

它是 θ(n^4)

With recursion it is Θ(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)

萝莉病 2024-10-17 09:03:51

这里可以直接使用主定理。

该方程适合主定理的情况 1,其中 log (a) 底 b f(n)

a:重复次数
b : 子部分的数量

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

因此根据马斯特斯定理,T(n) = theta(f(n)) = theta(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)

a : Number of recurrence
b : Number of subparts

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

Therefore by masters theorem, T(n) = theta(f(n)) = theta(n^4)

雪花飘飘的天空 2024-10-17 09:03:51

根据主定理

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

第一步 - 计算 n^(log a to base b) => n(以 2 为底的 log 2) = n*1 = n
第二步-是否f(n)>?第一步结果=> n^4> n =>是的
这意味着使用主定理的情况 3。

第三步 - 检查正则条件

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

是的,正则条件满足,所以我们的解决方案必须满足。

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

By Master Theorem

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.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文