[英]About the time complexity algorithm and asymptotic growth
I've got the question about the time complexity algorithm and asymptotic growth. 我有一个关于时间复杂度算法和渐近增长的问题。 The pseudo code of question is 问题的伪代码是
1: function NAIVE(x,A)
2: answer = 0
3: n= length of A
4: for I from - to n do
5: aux = 1
6. for j from 1 to I do
7: aux = aux*x
8: answer = answer + aux * A[I]
9. return answer
I have to find upperbound with O-notation and lowerbound witn Ω-notation. 我必须找到带有O表示法的上限和带有Ω表示法的下限。 I got the time complexity f(n) = 5n^2 + 4n + 8 and g(n) = n^2. 我得到了时间复杂度f(n)= 5n ^ 2 + 4n + 8和g(n)= n ^ 2。 My question is I'm not too sure about the running time of line number 6 to 8. I got constant 2 and time n+1 for line number 4 and constant 1 and time 1 for line 5. I'm stuck on after that. 我的问题是,我不太确定6号线到8号线的运行时间。对于4号线,我的常数为2,时间为n + 1;对于5号线,我的常数为1,时间为1。 。 I tried it and I got constant 2 and time n^2 + 1 for line 6, because it runs in the for loop (for loop and for loop) so I thought its n^2+1. 我尝试了一下,得到了常数6,第6行的时间为n ^ 2 +1,因为它在for循环(for循环和for循环)中运行,所以我认为它的n ^ 2 + 1。 Is that correct? 那是对的吗? And for line number 8 its has 3 constants and run time is n^2. 对于第8行,它具有3个常量,运行时间为n ^ 2。 is this correct? 这个对吗? I'm not too sure about the line number 8. This is how I got f(n) = 5n^2 + 4n + 8! 我不太清楚第8行。这就是我得到f(n)= 5n ^ 2 + 4n + 8的方法! Please help me out to complete this question! 请帮我完成这个问题! I wonder my work is right or not! 我不知道我的工作正确与否!
Thank you 谢谢
Let's go through this step by step. 让我们逐步进行此步骤。
The complexity of line 7 T7 = 1
. 7 T7 = 1
行的复杂度T7 = 1
。
The complexity of the surrounding for
will be T6(I) = I * T7 = I
. 的周围的复杂性for
将是T6(I) = I * T7 = I
。
The complexity of line 5 T5 = 1
. 5 T5 = 1
行的复杂度T5 = 1
。
The complexity of line 8 T8 = 1
. 8 T8 = 1
行的复杂度T8 = 1
。
The complexity of the surrounding for
(assuming that -
stands for 0) is 的周围的复杂性for
(假设-
代表0)是
T4(n) = Sum{I from 0 to n} (T5 + T6(I) + T8)
= Sum{I from 0 to n}(2 + I)
= Sum{I from 0 to n}(2) + Sum{I from 0 to n}(I)
= (n + 1) * 2 + (n+1)/2 * (0 + n)
= 2n + 2 + n^2/2 + n/2
= 1/2 n^2 + 5/2 n + 2
The complexity of the remaining lines is T2 = T3 = T9 = 1
. 其余线路的复杂度为T2 = T3 = T9 = 1
。
The complexity of the entire algorithm is 整个算法的复杂度是
T(n) = T2 + T3 + T4(n) + T9
= 1 + 1 + 1/2 n^2 + 5/2 n + 2 + 1
= 1/2 n^2 + 5/2 n + 5
This runtime is in the complexity classes O(n^2)
and Ω(n^2)
. 该运行时位于复杂度类O(n^2)
和Ω(n^2)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.