什么是总频率计数和运行时间(Big-O 表示法)?
我有以下代码片段:
1. for (i = 1; i < n; i++)
2. for (j = 1; j < i*i; j++)
3. if(j % i == 0)
4. for(k = 0; k < j;k++)
5. sum++;
总频率计数和运行时间(Big-Oh 表示法)是多少?
频率计数检查一段代码并预测要执行的指令数量(例如,对于每条指令,预测代码运行时每条指令会遇到多少次。)
我尝试通过以下方式进行分析:
Loop1执行n-1次,则FC为2n
Loop2执行了(ii)-1次,则FC为3(ii)
Loop1+loop2 的总频率计数为 2n + sum (从 i=1 到 n-1)3*i*i
我对 if(j%i==0) 有疑问。这里的循环执行是什么?
Loop4执行j次,则FC为2j+2
I have the following code snippet:
1. for (i = 1; i < n; i++)
2. for (j = 1; j < i*i; j++)
3. if(j % i == 0)
4. for(k = 0; k < j;k++)
5. sum++;
What is total frequency count and running time (Big-Oh notation)?
Frequency count examine a piece code and predict the number of instructions to be executed (e.g. for each instruction predict how many times each will be encountered as the code runs.)
I try to analyse in the following way:
Loop1 is executed n-1 times, then F.C. is 2n
Loop2 is executed (ii)-1 times, then F.C. is 3(ii)
total frequency count for loop1+loop2 is 2n + sum (from i=1 to n-1)3*i*i
I have a problem with if(j%i==0). What is loop execution here?
Loop4 is executed j times, then F.C. is 2j+2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
前 2 行(i 和 j 循环)是 n^3。
第4行,第k个循环是n^2。我很想将它们相乘并表示 n^5。但你必须考虑第 3 行的 if 。
if 语句仅在每 i 次迭代中为真一次,因此您必须除以 i(即除以 n):(n^3)/n = n^2 给出 n^2 * n^2 = n^4。
O(n^4)
The first 2 lines (the i and j loops) are n^3.
Line 4, the k loop is n^2. I'm tempted to mutiply them together and say n^5. But you have to consider the if on line 3.
The if statement is only true once every i itertions so you must divide by i (i.e., divide by n): (n^3)/n = n^2 giving us n^2 * n^2 = n^4.
O(n^4)
让我们尝试一种实验方法,而不是严格的数学方法。我有心情去玩玩。使用java:
输出为:
当n加倍时,总和乘以约19.3。当 n 为 40 时,总和为 293930,比率为 17.48 (293930 / 16815 = 17.48)。随着 n 增加,比率接近 16。由于 2^4 = 16,答案是 O(n^4)。顺便说一句,最后一行需要很长时间来计算。
O(n^4)
Let's try an experimental approach, as opposed to a rigorous, mathematical one. I'm in a mood to play around. Using java:
The output is:
When n is doubled sum is multiplied by about 19.3. When n is 40 sum is 293930, a ratio of 17.48 (293930 / 16815 = 17.48). As n increases the ratio approaches 16. Since 2^4 = 16 the answer is O(n^4). btw, the last line takes a long time to compute.
O(n^4)
这里有一些可疑的地方:
既然我们只在 j 是 i 的倍数时执行循环体,为什么不计入 i:
这是 O(n^4)
查看你的原始算法。前 2 行的工作时间复杂度为 O(n^3)。然后 O(n^2) 的时间,(j%i == 0),我们做了 O(n^2) 多的工作。所以上面的算法是O(n^4)。
There's something fishy here:
Since We're only executing the loop body when j is a multiple of i, why not count in i's:
Which is O(n^4)
Looking at your original algorithm. The first 2 lines do O(n^3) work. Then O(n^2) of the time, (j%i == 0), we do O(n^2) more work. So The algorithm above is O(n^4).
您有一个总和:
TimeComplexity ==
Sum_{i=1..n} Sum_{j=1..i^2} (1 + [j % i = 0]*Sum_{k=1..j} 1) =
= Sum_{i=1..n} Sum_{j=1..i^2} (1 + [j % i = 0]*j) =
= Sum_{i=1..n} Sum_{j= 1..i^2} 1 + Sum_{i=1..n} Sum_{j=1..i^2} [j % i = 0]*j ==
Sum_{i=1..n} i ^2 + Sum_{i=1..n} (i+2i+3i+...+i*i) =
= Sum_{i=1..n} i^2 + Sum_{i=1..n} i(1+2+3+...+i) =
= Sum_{i=1..n} i^2 + Sum_{i=1..n} i^2(i+1)/2 =
= Sum_{i=1..n} (i^3 + O(i^2)) =
= O(n^4)。
享受
You have a sum:
TimeComplexity =
= Sum_{i=1..n} Sum_{j=1..i^2} (1 + [j % i = 0]*Sum_{k=1..j} 1) =
= Sum_{i=1..n} Sum_{j=1..i^2} (1 + [j % i = 0]*j) =
= Sum_{i=1..n} Sum_{j=1..i^2} 1 + Sum_{i=1..n} Sum_{j=1..i^2} [j % i = 0]*j =
= Sum_{i=1..n} i^2 + Sum_{i=1..n} (i+2i+3i+...+i*i) =
= Sum_{i=1..n} i^2 + Sum_{i=1..n} i(1+2+3+...+i) =
= Sum_{i=1..n} i^2 + Sum_{i=1..n} i^2(i+1)/2 =
= Sum_{i=1..n} (i^3 + O(i^2)) =
= O(n^4).
Enjoy