What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop?

StackOverflow https://stackoverflow.com/questions/362059

  •  21-08-2019
  •  | 
  •  

Question

What is the Big-O time complexity of the following nested loops:

for(int i = 0; i < N; i++) 
{
    for(int j = i + 1; j < N; j++)
    {
        System.out.println("i = " + i + " j = " + j);
    }

}

Would it be O(N^2) still?

Was it helpful?

Solution

Yep, it's still O(n^2), it has a smaller constant factor, but that doesn't affect O notation.

OTHER TIPS

Yes. Recall the definition of Big-O: O(f(n)) by definition says that the run time T(n)kf(n) for some constant k. In this case, the number of steps will be (n-1)+(n-2)+...+0, which rearranges to the sum of 0 to n-1; this is

T(n)=(n-1)((n-1)+1)/2.

Rearrange that and you can see that T(n) will always be ≤ 1/2(n²); by the definition, thus T(n) = O(n²).

It's N squared if you ignore the System.out.println. If you assume that the time taken by that will be linear in its output (which it may well not be, of course), I suspect you end up with O ( (N^2) * log N).

I mention this not to be picky, but just to point out that you don't just need to take the obvious loops into account when working out complexity - you need to look at the complexity of what you call as well.

Yes, it would be N squared. The actual number of steps would the sum of 1 to N, which is .5*(N - 1)^2, if I'm not mistaken. Big O only takes into account the highest exponant and no constants, and thus, this is still N squared.

If you had N = 10, you iterations would be: 10+9+8+7+6+5+4+3+2+1. (this is: ten iterations plus nine iterations plus eight iterations... etc.).

Now, you need to find into the addition how many times you can get a N (10 in the example):

1:(10), 2:(9+1), 3:(8+2), 4:(7+3), 5:(6+4). Which is 5 times... and rests 5 iterations.

Now you know that you have five tens + 5:

10(5) + 5

In terms of f(n) (or N), we can easily see that this would be:

f(n) = n(n/2) + n/2 = (n^2)/2 + n/2 = (n^2 + n)/2... this is exactly the complexity of these nested loop.

But, considering the asymptotic behavior of Big O, we can get rid of the less significant values of f(n), which are the single n and the denominator.

Result: O(n^2)

AFAIL, being begun from inner loop through outer ones is adequate way for calculation of nested loop complexity. enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top