AlgoFrames

Big-O, Big-Θ and Big-Ω

Big-O, Big-Θ and Big-Ω describe how cost grows, ignoring constants. Watch 3n + 5 and n² as n grows and the small terms fade away.

The idea

One algorithm takes 3n + 5 steps, another n². For small n it is hard to say which is cheaper.

Counting steps gives exact formulas. Big-O keeps only the part that matters when n gets large.

Intuition

At n = 40 the line costs 125 steps and the curve 1,600. The curve wins the race to be expensive.

Divide the cost by n. The ratio settles near 3, so the +5 stops mattering as n grows.

From n = 5, 3n + 5 stays between 3n and 4n. A line squeezes it from both sides.

O means at most c·f(n), Ω means at least c·f(n), and Θ means both. Here 3n + 5 is Θ(n).

Complexity

Common classes, slowest-growing first: log n, n, n log n, n². The biggest term decides the class.

A sum such as n² + n + 7 is O(n²): for large n the n² part dwarfs the others.

Pitfalls

Constants are dropped, not forgotten. 1000n costs more than n² until n passes 1,000.

5n is O(n²) too, but that bound is loose. Use Θ(n) to say the growth is exactly linear.

Variations

Lower terms vanish too. n² + n stays between n² and 2n², so it is Θ(n²).

Code

Pseudocode

count ← 0
for i from 1 to n
  for j from 1 to n
    count ← count + 1
return count

Python

def count_steps(n):
    count = 0
    for i in range(n):
        for j in range(n):
            count += 1
    return count

JavaScript

function countSteps(n) {
  let count = 0;
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      count++;
    }
  }
  return count;
}

Practice problems (11)

Simplify to Big-O Warm-up

Simplify 5n² + 40n + 900 to Big-O. Write the class, such as O(n).

Which statement is true? Warm-up

For f(n) = 7n + 3, which statement is true?

Match the notation to its meaning Warm-up

Match each symbol to what it says about how f(n) grows.

Rank functions by growth Standard

Give each function its place when ranked by growth for large n. 1st grows slowest, 5th fastest.

Identify the tight bound Standard

Which is the tight bound, Θ, of 3n² + 10n log n + 50?

Complexity of a triangle loop Standard

Pick the time and space complexity of countSteps for input n.

Follow 3n + 5 against n² Standard

Watch both step counts as n grows and answer the prompts.

Where n² overtakes a line Standard

A line costs a·n + b steps and a curve costs n². Return the smallest n ≥ 1 where n² is strictly bigger than the line.

Count the steps of a triangle loop Standard

Two nested loops run: i goes from 0 to n - 1, and for each i, j goes from 0 to i. Return how many times the loop body runs for input n.

Guess the growth from a doubling test Standard

An algorithm took t1 steps on size n and t2 steps on size 2n. Compute r = round(log2(t2 / t1)). Return "constant", "linear", "quadratic" or "cubic" for r = 0, 1, 2 or 3 (use "cubic" for anything above 3 and "constant" for anything below 0).

Prove n² + n is O(n²) Challenge

Put the steps of the proof in order. Give each statement its place, Step 1 first.

Next on the path: Arrays: indexing, traversal, insertion and deletion costs