Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O complexity of loop with two independent inner loops

I have a function that depends on three variables, T, N, and M. The loop is the following:

for each t from 0 to T {
    for each n from 0 to N {
         process(n,t);
    }
    for each m from 0 to M {
         process(m,t);
    }
}

What would be the big-O run-time complexity of this? I am thinking O(T*Max(n,m)) but is this standard? Thanks!

like image 903
Sam Gomez Avatar asked Jun 13 '13 20:06

Sam Gomez


People also ask

What is the time complexity of two separate loops?

Thus, the complexity is O(N * M). In a common special case where the stopping condition of the inner loop is j < N instead of j < M (i.e., the inner loop also executes N times), the total complexity for the two loops is O(N2).

What is the time complexity of two nested loops?

Time complexity of nested loops is equal to the number of times the innermost statement is executed.

How do you calculate big O for nested loops?

You can calculate big O like this: Any number of nested loops will add an additional power of 1 to n. So, if we have three nested loops, the big O would be O(n^3). For any number of loops, the big O is O(n^(number of loops)).

What is the big O complexity for an algorithm that contains nested loop?

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.


1 Answers

One way to analyze this is to look at the total amount of work being done across all iterations of the loop by determining how many times the outer loop executes and multiplying it by the amount of work done by the body of the loop. To do this, we'll work inside-out.

Starting with the inside, note that the first loop runs O(N) times, and does some amount of work on each iteration (I'll assume that it's O(1) work, though this analysis can be modified in case that's not true). Consequently, this loop does O(N) work. The second loop, similarly, does O(M) work. Thus the total amount of work done by the body of the loop is O(M + N). Since the top-level loop runs O(T) times, each iteration doing O(M + N) work, the total amount of work done is O(T(M + N)) = O(TM + TN).

Your claim that this is equal to O(T max{M, N}) is also correct. One way to see this is as follows: note that N = O(max{M, N}) and M = O(max{M, N}), and therefore we have that

O(TM + TN)

= O(T max{M, N} + T max{M, N})

= O(2T max{M, N})

= O(T max{M, N})

Hope this helps!

like image 114
templatetypedef Avatar answered Sep 20 '22 03:09

templatetypedef