是否有人可以帮助我吗?
使用迭代法来解决这个问题。 T(N)= T(N-1)+ N
的步骤说明将不胜感激。
是否有人可以帮助我吗?
使用迭代法来解决这个问题。 T(N)= T(N-1)+ N
的步骤说明将不胜感激。
T(n) = T(n-1) + n
T(n-1) =T(n-2) + n-1
T(n-2) = T(n-3) + n-2
等可以替代T(N-1)和T(N-2)的T(n)的数值来获取模式的总体思路。
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
对于基本情况:
n - k =1 so we can get T(1)
K = N - 在上述1所代替
T(n) = T(1) + (n-1)n - (n-1)(n-2)/2
你可以看到的是n阶^ 2
展开吧!
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
在伪代码中使用迭代:
function T(n) {
int result = 0;
for (i in 1 ... n) {
result = result + i;
}
return result;
}
另一种简单的解决方案
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 k = n-1 and T(1) = 1
= 1 + 2 + ... + n
= 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)
简单的方法:
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)