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!
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).
Time complexity of nested loops is equal to the number of times the innermost statement is executed.
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)).
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With