14

有人可以帮我吗?

使用迭代法求解。 T(n) = T(n-1) +n

步骤的解释将不胜感激。

4

5 回答 5

35
T(n) = T(n-1) + n

T(n-1) = T(n-2) + n-1

T(n-2) = T(n-3) + n-2

依此类推,您可以在 T(n) 中替换 T(n-1) 和 T(n-2) 的值,以大致了解该模式。

T(n) = T(n-2) + n-1 + n

T(n) = T(n-3) + n-2 + n-1 + n
.
.
.

T(n) = T(n-k) + kn - k(k-1)/2    ...(1)

对于基本情况:

n - k = 1 so we can get T(1)

=> k = n - 1
替换 (1)

  T(n) = T(1) + (n-1)n - (n-1)(n-2)/2

您可以看到它的阶数为 n 2 => O(n 2 )。

于 2012-12-02T22:47:00.643 回答
9

展开它!

T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n

以此类推,直到

T(n) = 1 + 2 + ... + n = n(n+1)/2   [= O(n^2)]

前提是T(1) = 1

于 2012-12-02T22:43:11.763 回答
1

在使用迭代的伪代码中:

function T(n) {
    int result = 0;

    for (i in 1 ... n) {
       result = result + i;
    }

    return result;
}    
于 2012-12-02T22:47:09.543 回答
1

另一种解决方案:

T(n) = T(n-1) + n
     = T(n-2) + n-1 + n
     = T(n-3) + n-2 + n-1 + n
     // we can now generalize to k
     = T(n-k) + n-k+1 + n-k+2 + ... + n-1 + n
     // since n-k = 1 so T(1) = 1
     = 1 + 2 + ... + n    //Here 
     = n(n-1)/2
     = n^2/2 - n/2
     // we take the dominating term which is n^2*1/2 therefor 1/2 = big O
     = big O(n^2)
于 2017-10-22T19:50:26.837 回答
-2

简易方法:

T (n) = T (n - 1) + (n )-----------(1)
 //now submit T(n-1)=t(n)

T(n-1)=T((n-1)-1)+((n-1))
T(n-1)=T(n-2)+n-1---------------(2)

now submit (2) in (1) you will get
i.e T(n)=[T(n-2)+n-1]+(n)
T(n)=T(n-2)+2n-1 //simplified--------------(3)

 now, T(n-2)=t(n)
T(n-2)=T((n-2)-2)+[2(n-2)-1]
  T(n-2)=T(n-4)+2n-5---------------(4)
  now submit (4) in (2) you will get
   i.e T(n)=[T(n-4)+2n-5]+(2n-1)
  T(n)=T(n-4)+4n-6 //simplified
    ............
 T(n)=T(n-k)+kn-6
  **Based on General form T(n)=T(n-k)+k, **
  now, assume n-k=1 we know T(1)=1
            k=n-1

    T(n)=T(n-(n-1))+(n-1)n-6
    T(n)=T(1)+n^2-n-10
   According to the complexity 6 is constant

         So , Finally O(n^2)
于 2016-12-01T14:11:27.603 回答