How to find the Big O of a nested loop
Determine the time complexity of a nested loop by evaluating the number of iterations for the inner loop and multiplying it by the number of iterations of the outer loop. This applies when evaluating standard iterative algorithms where loop bounds are defined by the input size .
The setup
Identify the outer loop, the inner loop, and the operations performed inside the deepest nesting level. Define the loop variable bounds and step sizes in terms of the input size .
The steps
- Determine the iteration count of the outer loop as a function of . 2. Determine the iteration count of the inner loop. 3. If the inner loop bounds are independent of the outer loop, multiply the two counts. If the inner loop depends on the outer loop variable, express the total iterations as a summation over the outer loop variable. 4. Evaluate the summation or multiplication, then drop lower-order terms and constant factors to yield the final notation.
Checking the result
Select small integer values for and trace the loops to count the exact number of innermost executions. Verify that the polynomial degree of the exact count matches the calculated asymptotic complexity.
Common errors
A frequent error is assuming any two nested loops automatically result in complexity. Another common mistake is ignoring step sizes, such as a variable doubling (), which yields logarithmic iterations rather than linear ones.
Worked example
Find the Big O time complexity of a loop where the outer loop variable goes from 1 to , and the inner loop variable goes from 1 to , with both incrementing by 1.
The outer loop runs times. For each iteration , the inner loop runs exactly times. The total number of operations is expressed by the summation: . Dropping the constant divisor and the lower-order linear term , the asymptotic time complexity is .
FAQ
Run your own problem
References: Introduction to Algorithms (Cormen, Leiserson, Rivest, Stein) · The Algorithm Design Manual (Skiena)
See also