Calculate the complexity of a nested for loop

坚强是说给别人听的谎言 提交于 2019-12-11 03:36:45

问题


I want to calculate the complexity of this nested for loop:

s = 0;
for(i=1; i<=n; i*=2)
   for(j=1; j<=i; j*=2)
      s++;

What strategy do I use to find the Big O complexity of this piece of code?


回答1:


The outer loop marches through 1, 2, 4, 8, ... n, which takes O(lg n) steps because you can only double one O(lg n) times until you hit n.

The inner loop does the same. It only goes up to i, but in the final iteration of the outer loop, i reaches its maximum value which is again n, so that's also O(lg n).

Putting this together gives an upper bound of O((lg n)²), which is commonly abbreviated O(lg² n).




回答2:


Strategy for getting the answer yourself

Plug in different values of n into the equation, and make a chart of how many times the innermost part of the loop runs:

s = 0;
for(i=1; i<=n; i*=2)
  for(j=1; j<=i; j*=2)
    s++;

Something like this:

n     num_times_inner_loop_part_runs
1     1
2     3
3     3
4     6
5     6
6     6
7     6
8     10
9     10
...
15    10
16    15
...
31    15
32    21

You can get these data points with a program like this:

int n = 9;  //change this n
int counter = 0;
for(i=1; i<=n; i*=2){
  for(j=1; j<=i; j*=2){
    s++;
    counter++;
  }
}
cout << "counter is: " <<  counter << endl;

Plot the num_times_inner_loop_part runs on an X/Y coordinate plane and you'll see a curve.

Name the curve that fits closest. In this case, it is X = (log(Y)^2)

If you plot your data and X = (log(Y)^2), you'll find they should overlap each other.

Therefore, the complexity of this function is O((log(n))^2) which is an improvement over O(n)




回答3:


Time analysis of this piece of code:




回答4:


Your algorithm time complexity can be portrayed formally as the following:

This document (last slide) might be enormously useful to you.



来源:https://stackoverflow.com/questions/15433435/calculate-the-complexity-of-a-nested-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!