Time complexity 时间复杂性:我们如何找到O(n^3)?
我知道Time complexity 时间复杂性:我们如何找到O(n^3)?,time-complexity,Time Complexity,我知道f(n)=3*n^3+4*n+2有O(n^3)的复杂性,但我想知道如何使用大O的定义来计算它: If f(n) has complexity O(g(n)), that means there exists a constant c > 0, n0 >=1 where f(n) < c*g(n) 如果f(n)的复杂度为O(g(n)),这意味着存在一个常数c>0,n0>=1,其中f(n)=1,n^3>=n>=1,所以 f(n) = 3*n^3 + 4*n + 2
f(n)=3*n^3+4*n+2
有O(n^3)的复杂性,但我想知道如何使用大O的定义来计算它:
If f(n) has complexity O(g(n)), that means there exists a constant c > 0, n0 >=1 where f(n) < c*g(n)
如果f(n)的复杂度为O(g(n)),这意味着存在一个常数c>0,n0>=1,其中f(n)
但是你如何确定什么是
c
和什么是g(n)
?你可以使用任何肮脏的黑客手段,你想发现一个c和n0是有效的,然后证明它们(归纳法通常很有效,但有时并不必要)
这里,只需注意,对于n>=1,n^3>=n>=1,所以
f(n) = 3*n^3 + 4*n + 2
<= 3*n^3 + 4*n^3 + 2*n^3
= 9n^3
f(n)=3*n^3+4*n+2