5

I am trying to figure out a tight bound in terms of big-O for this snippet:

for(int i = 1 ; i <= n ; i++) {
    for(int j = 1; j <= i*i ; j++) {
        if (j% i == 0){
            for(int k = 0 ; k<j ; k++ )
                sum++;
        }
    }
}

If we start with the inner most loop, it will in worst case run k = n^2 times, which accounts for O(N^2). The if-statement will be true every time j = m*i, where m is an arbitrary constant. Since j runs from 1 to i^2, this will happen when m= {1, 2, ..., i}, which means it will be true i times, and i can at most be n, so the worst case will be m={1,2, ..., n} = n times. The second loop should have a worsts case of O(N^2) if i = n. The outer loop has a worst case complexity of O(N).

I argue that this will combine in the following way: O(N^2) for the inner loop * O(N^2) for the second loop * O(N) for the outer loop gives a worst case time complexity of O(N^5)

However, the if-statement guarantees that we will only reach the inner loop n times, not n^2. But regardless of this, we still need to go through the outer loops n * n^2 times. Does the if-test influence the worst case time complexity for the snippet?

Edit: Corrected for j to i^2, not i.

2 Answers2

2

You can simplify the analysis by rewriting your loops without an if, as follows:

for(int i = 1 ; i <= n ; i++) {
    for(int j = 1; j <= i ; j++) {
        for(int k = 0 ; k<j*i ; k++ ) {
            sum++;
        }
    }
}

This eliminates steps in which the conditional skips over the "payload" of the loop. The complexity of this equivalent system of loops is O(n4).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • If I understand you correctly: The second loop now runst from j= 1 to j <= i and not i^2 because the if-test stops the program from reaching the inner loop every iteration but the i-th? – user2005142 Sep 09 '17 at 11:40
  • 1
    @user2005142 Yes, that's right. `if` prevented the code from reaching the "payload" (which is `sum++`) so we can "cut" the do-nothing iterations from the analysis. – Sergey Kalinichenko Sep 09 '17 at 11:44
  • I see, thank you! But just to clarify, the regardless of the payload, an empty loop with n iterations should still have a worst case time complexity of O(N)? – user2005142 Sep 09 '17 at 11:46
  • 1
    @user2005142 it's true. In reality, "empty" loops might be skipped by a compiler or an execution environment and might be processed in a constant time. But in Big-O analysis it's still O(N) – Dzmitry Bahdanovich Sep 09 '17 at 12:08
1
    I analyse your question in a more straightfroward way
    we first start by fix i as a costant, 
    for example, assume it to be k,
    so j=1~k^2, when j=k,2k,3k,...,k^2, assume j to be c*k (c=1~k)
    the next loop will be executed c^2 times, 
    so the complexity for a fix i can be expressed as=>
    (1+.....+1)+(1+1+...+2^2)+(1+1+...+3^2)+.....+(1+1+...+k^2)
    = O(k^3)
    so now we set k to be 1~n, so the total complexity will be O(n^4)
codecrazer
  • 525
  • 5
  • 20