【问题标题】:How to use master theorem to calculate recursion, divide and conquer如何使用主定理计算递归,分而治之
【发布时间】:2018-03-25 03:37:05
【问题描述】:

我要计算 Θ()

Given T (n) = T (n − 1) + n^3

我不能直接应用主定理规则,因为我不知道 b 是什么,那么如何推导出 Θ() ?

【问题讨论】:

    标签: algorithm divide-and-conquer master-theorem


    【解决方案1】:

    为什么你必须在这里使用主定理?可以直接这样解决:

    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)

    【讨论】:

    • 聪明的想法。
    • 好的,我明白了,但是这样计算太复杂了,谢谢你的回答。我对你的答案投了赞成票,但由于我的声望少于 15 个,因此尚未显示。
    • 但就是这样。看看这个(brilliant.org/wiki/sum-of-n-n2-or-n3)。解决try yourself 挑战,然后您将获得更多关于如何解决序列的知识。
    【解决方案2】:
    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)
    

    【讨论】:

    • 好的解决方案谢谢,有没有办法直接计算1^3+3^3+...+(n+2)^3?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2021-03-14
    • 2020-09-21
    • 2019-05-16
    • 2020-04-11
    • 2016-05-01
    相关资源
    最近更新 更多