【发布时间】:2018-03-25 03:37:05
【问题描述】:
我要计算 Θ()
Given T (n) = T (n − 1) + n^3
我不能直接应用主定理规则,因为我不知道 b 是什么,那么如何推导出 Θ() ?
【问题讨论】:
标签: algorithm divide-and-conquer master-theorem
我要计算 Θ()
Given T (n) = T (n − 1) + n^3
我不能直接应用主定理规则,因为我不知道 b 是什么,那么如何推导出 Θ() ?
【问题讨论】:
标签: algorithm divide-and-conquer master-theorem
为什么你必须在这里使用主定理?可以直接这样解决:
T(n) = T(n-1) + n^3
T(n-1) = T(n-2) + (n-1)^3
T(n-2) = T(n-3) + (n-2)^3
. . .
. . .
. . .
T(1) = T(0) + 1^3
----------------------- (Add them all and cancel)
T(n) = T(0) + (n(n-1)/2)^2 (Sum of the cubes of the first n numbers)
因此是O(n^4)
【讨论】:
try yourself 挑战,然后您将获得更多关于如何解决序列的知识。
T(n) = T(n-1) + n^3
= T(n-2) + n^3 + (n-1)^3
= T(n-i+1) + (n-i)^3 + ... + (n-1)^3 + n^3
= 1^3 + 2^3 + ... + (n/2)^3 + (n/2+1)^3 + ... + (n-1)^3
Throw bottom half and decrease the half top to n/2
> ((n/2)^3)*(n/2)
Ω(n^4)
Increase all to (n-1)
= 1^3 + 2^3 + ... + (n/2)^3 + (n/2+1)^3 + ... + (n-1)^3 < (n-1)^3*n = O(n^4)
T(n) = θ(n^4)
【讨论】: