Time complexity O(N) of nested loops with if-statement: O(N^4)?

心不动则不痛 提交于 2019-12-07 05:06:02

问题


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.


回答1:


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




回答2:


    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)


来源:https://stackoverflow.com/questions/46130145/time-complexity-on-of-nested-loops-with-if-statement-on4

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