Sub topic 3
What Big O means: an upper bound for large n
This chapter assumes you can read basic algebra (exponents, inequalities), write loops and functions in Python, and know that an algorithm's cost depends on its input size n. Earlier chapters showed that cost is counted in steps, not seconds, and that functions such as n, n^2, and 2^n grow at very different rates. Here we turn that intuition into a precise definition.
The definition
Big O is a statement about two functions. One is f(n), the cost of an algorithm (for example, the number of steps on an input of size n). The other is g(n), a simpler reference function such as n or n^2.
We write f(n) = O(g(n)) when this holds:
- There is a positive constant c.
- There is a positive threshold n0.
- For every n >= n0, f(n) <= c * g(n).
In words: past some point, f never rises above a constant multiple of g. Big O is an upper bound that only has to hold for large n.
Each part of the definition does a job:
- The constant c absorbs machine speed, language, and counting details. Whether a loop body takes 3 steps or 30 does not change the class.
- The threshold n0 ignores small inputs, where a function with a bad growth rate may still look cheap.
- The inequality is one-sided. g only has to be above f. It does not have to be close.
Worked proof: 3n^2 + 5n + 2 is O(n^2)
Let f(n) = 3n^2 + 5n + 2 and g(n) = n^2. We must find c and n0.
For n >= 1, two facts hold: n <= n^2, and 1 <= n^2. Therefore:
f(n) = 3n^2 + 5n + 2 <= 3n^2 + 5n^2 + 2n^2 = 10n^2
So c = 10 and n0 = 1 satisfy the definition. Other pairs also work, such as c = 4 with n0 = 6. You can check that case: 4n^2 - (3n^2 + 5n + 2) = n^2 - 5n - 2, which is positive for n >= 6. The pair is not unique, and you only need to show that one exists.
Checking the definition in code
A program cannot prove a Big O claim, because it can only test finitely many values of n. It is still a useful way to look for counterexamples and to see what c and n0 do.
def first_violation(f, g, c, n0, n_max):
"""Return the smallest n in [n0, n_max] where f(n) > c * g(n), or None."""
for n in range(n0, n_max + 1):
if f(n) > c * g(n):
return n
return None
def f(n):
return 3 * n * n + 5 * n + 2
def g(n):
return n * n
# 1. The constants from the proof: no violation found.
print(first_violation(f, g, 10, 1, 10000)) # None
# 2. c = 3 is too small, because the 5n + 2 part pushes f above 3 * g.
print(first_violation(f, g, 3, 1, 10000)) # 1
# 3. n^2 against n: any fixed c eventually loses.
print(first_violation(lambda n: n * n, lambda n: n, 1000, 1, 5000)) # 1001
# 4. 100n against n^2: the threshold n0 matters.
print(first_violation(lambda n: 100 * n, lambda n: n * n, 1, 1, 10000)) # 1
print(first_violation(lambda n: 100 * n, lambda n: n * n, 1, 100, 10000)) # NoneCase 4 shows why n0 exists. The function 100n is above n^2 for n < 100, yet 100n is O(n^2) because from n0 = 100 onward the bound holds.
Showing that a claim is false
To show f is not O(g), you must show that no pair (c, n0) works. The standard method is to assume a pair exists and derive a contradiction.
Claim: n^2 is not O(n). Suppose n^2 <= c * n for all n >= n0. Dividing by n gives n <= c for all n >= n0. That is false for n = max(n0, c + 1), which is larger than c. No constant works, so n^2 is not O(n). Case 3 in the code is this argument in miniature: c = 1000 fails at n = 1001.
Reading Big O correctly
It is an upper bound, not an exact rate
If f(n) = 5n, then f is O(n). It is also O(n^2), O(n^3), and O(2^n), because each of those is an upper bound too. All of these statements are true. The useful one is the smallest bound you can prove, and people usually mean that one when they say "the complexity is O(n)".
This is why a statement like "this algorithm is O(n^2)" does not by itself say the algorithm is slow. It says the cost will not grow faster than n^2. A later chapter introduces Big Omega and Big Theta for lower and tight bounds.
The equals sign is a convention
Writing f(n) = O(g(n)) is traditional, but it does not mean equality. "O(g(n))" stands for the set of all functions bounded above by a constant multiple of g, and f belongs to that set. Because of this, you can never reverse the statement: n = O(n^2) is true, but n^2 = O(n) is false. Some texts write f(n) in O(g(n)) to make this clear.
It is not the same as worst case
Big O describes how a function grows. "Worst case" describes which inputs you measure. These are separate choices. You can give a Big O bound on the best-case cost, the average-case cost, or the worst-case cost of one algorithm. Mixing them up is a common error, and the chapter on case analysis returns to it.
It always refers to a variable
Say what n is. For a function that takes two lists of lengths n and m, cost such as n m is O(n m). Collapsing it to O(n^2) assumes n and m are about the same size, and that is an extra claim you have to state.
Common errors
| Error | Why it is wrong | Correct view |
|---|---|---|
| "O(n) means exactly n steps" | Big O hides constants and lower terms | O(n) means at most c * n steps for large n |
| "O(n) is faster than O(n^2) on every input" | The definition only covers n >= n0 | A smaller class wins eventually, not necessarily at small n |
| "n^2 = O(n) is fine because n^2 is bigger" | The bound must be above f, not below | Only g that is at least as large as f can be used |
| Choosing c that depends on n | c must be a fixed constant | If c has to grow with n, the claim is false |
| Treating the log base as important | log2 n and log10 n differ by a constant factor | Both are O(log n) |
| Proving O by testing a few values of n | The inequality must hold for all n >= n0 | Use algebra for a proof and code only as a check |
Why the definition is built this way
Real cost functions are messy. The same algorithm costs different numbers of steps on different machines, and counting every operation exactly is tedious and rarely meaningful. The definition fixes this in two ways. The constant c removes machine-level detail, and the threshold n0 removes small-input noise. What remains is the shape of growth, which is what decides whether a program still works when the input becomes a thousand or a million times larger.
Exercises
Exercise
Show that f(n) = 4n + 10 is O(n) by giving a valid pair c and n0 and a short justification.
Show answer
For n >= 1 we have 10 <= 10n, so f(n) = 4n + 10 <= 4n + 10n = 14n. Take c = 14 and n0 = 1. Another valid pair is c = 5 and n0 = 10, since 5n - (4n + 10) = n - 10 >= 0 for n >= 10.
Exercise
Is it true that n = O(n^2)? Is it true that n^2 = O(n)? Explain each answer using the definition.
Show answer
n = O(n^2) is true: for n >= 1, n <= 1 n^2, so c = 1 and n0 = 1 work. n^2 = O(n) is false: it would require n^2 <= c n, so n <= c, for all n >= n0. That fails for n = max(n0, c + 1), so no constants exist.
Exercise
Using first_violation, which call would you run to test whether 2n^2 + 7 is O(n^2) with c = 3 and n0 = 3, and what result should you expect? Does the result prove the claim?
Show answer
Call first_violation(lambda n: 2nn + 7, lambda n: n*n, 3, 3, 10000). It returns None, because 3n^2 - (2n^2 + 7) = n^2 - 7 > 0 for n >= 3. The output does not prove the claim for all n. The algebra n^2 - 7 > 0 for n >= 3 is the proof.