2

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?

4
  • 1
    Try looking through this: stackoverflow.com/questions/8331479/determining-big-o-notation I doubt anyone will just give you the answer. Commented Mar 15, 2013 at 13:17
  • @smoore: The value of i in the inner loop is bounded above by n. Commented Mar 15, 2013 at 13:30
  • 1
    Yep, just revoked my comment :-) Commented Mar 15, 2013 at 13:31
  • @smoore: happens all the time. I wasn't going to post one until I saw incorrect answers popping up. Commented Mar 15, 2013 at 13:33

4 Answers 4

3

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Many books consider the latter notation to mean log log n
@user1952500: I've never seen that use of lg² n, but that's exactly why I gave both notations (see also math.se).
loglogn means log(logn) while log^2(n) means logn*logn
While the result is correct, the analysis is crude and could easily miss the tightest bound.
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)

2 Comments

Your conclusion does not match your results. num_times_inner_loop_part_runs = n(n + 1) / 2. Perhaps you had i++ instead of i*=2
ooh the increment is j*=2. I fixed it.
1

Time analysis of this piece of code:

Analysis

Comments

0

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

enter image description here

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.