Big O Notation

1 minute read

Some quick notes on Big O notation.1

How to Determine Running-Time Complexity

Nested Loops

Consider a general sequence of nested loops in which the outer loop is iterated times and the inner loop is iterated times. Generally, these will look something like this:

for (i = 0; i < N; i++) {
  for (j = 0; j < M; j++) {
    //statements
  }
}

Every time the outer loop executes, the inner loop runs times. Thus, we expect this function to run in , or in the case that .

A similar case arises when the inner loop depends on the outer loop. Consider the function

for (i = 0; i < N; i++) {
  for (j = i + 1; j < N; j++) {
    //statements
  }
}

Since the outer loop ranges over , while the inner loop ranges from , the total number of operations is given by

\[ \sum_{i=1}^{N} i = \frac{N(N+1)}{2}\]

which is the sum of the first natural numbers, and thus this function is also in .

Function Composition

Consider a function for which the complexity depends on the number of inputs . If this function is embedded within another function that also depends on another variable , then the composition .

Again, in the special case that , then .

For example, take to be a loop, and to be a function called within the loop. Then you wind up with something like this:

  void f(n): 
  for (i = 0; i < n; i++) {
    g(n);
  }
  

The loop iterates times, and is called each time. So we have an overall complexity of .

Note: This method may not always produce the tightest upper bound possible. For more specific cases, tighter bounds on running time can be found by considering the total time spent on the inner loop and outer loop separately, and adding them.


That’s all for today’s post, although I may end up adding more information later! If you have any questions or found this helpful, feel free to comment below.


Links and References

Some great practice problems and explanations: Carl Burch at Hendrix

Comments