简体   繁体   English

递归关系的时间复杂度 T(n) = T(n-1)*n

[英]Time Complexity for the recurrence relation T(n) = T(n-1)*n

I need help with the following recurrence relation.我需要以下递归关系的帮助。

T(1) = 1 T(1) = 1

T(n) = T(n-1)*n T(n) = T(n-1)*n

This is what I've tried.这是我尝试过的。 I think I might have messed up the substitution part but again please take a look at let me know if the time complexity I've got is correct.我想我可能搞砸了替换部分,但请再次查看让我知道我得到的时间复杂度是否正确。

T(n) = T(n-1)*n               T(n-1) = T(n-2)*n-1
T(n) = [T(n-2)*(n-1)]*n
T(n) = T(n-2)*(n-1)*n
T(n) = [T(n-3)*n-2]*(n-1)*n
T(n) = T(n-3)*(n-2)*(n-1)*n
...
...
...
T(n) = T(n-k)*(n-(k-1))*(n-(k-2))...*(n-1)*(n)

Assuming n-k=0, n=k

T(n) = T(n-n)*(n-n+1)*(n-n+2)...*(n-1)*(n)
T(n) = T(0)*(1)*(2)...*(n-1)*n

O(n^2)

Now I am not sure if what I did was exactly correct or not but any help would be appreciated.现在我不确定我所做的是否完全正确,但我们将不胜感激。

Only the final complexity is wrong, you end up with O(n.).只有最终的复杂性是错误的,你最终会得到 O(n.)。

The recursive relation must be T(n) = T(n-1)+n for getting O(n^2) as the complexity.递归关系必须是 T(n) = T(n-1)+n 以获得 O(n^2) 作为复杂度。

Very close!很接近! You've correctly identified that the complexity is您已经正确地确定了复杂性是

n * (n - 1) * (n - 2) *... * 3 * 2 * 1. n * (n - 1) * (n - 2) *... * 3 * 2 * 1。

However, that's not O(n 2 ).但是,这不是 O(n 2 )。 It would be O(n 2 ) if you added the terms rather than multiplying them.如果您添加术语而不是将它们相乘,则将是 O(n 2 )。

What do you call the product of all the natural numbers from 1 to n, inclusive?你把从 1 到 n 的所有自然数的乘积称为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM