Sub topic 3
What Big O means: an upper bound for large n
This chapter assumes you can read simple Python, know what a function of n is, and are comfortable with basic algebra. Earlier chapters showed that we count steps as a function of input size n and compare how those counts grow. Here we turn that idea into a precise definition.
The formal definition
Let f(n) be the number of steps an algorithm takes on an input of size n. Let g(n) be a simpler function, such as n or n^2. We write f(n) = O(g(n)) when this holds:
- There are positive constants c and n0 such that f(n) <= c * g(n) for every n >= n0.
The definition has three parts:
- c lets g(n) be scaled by a fixed factor. This is why constant factors do not matter.
- n0 lets the inequality fail for small inputs. Only large n counts.
- For every n >= n0 means the bound must hold from n0 onward, not just at one point.
In short, Big O says that f(n) eventually stays at or below some constant multiple of g(n).
Checking the definition
Take f(n) = 3n + 10 and claim it is O(n). We need to find c and n0.
- Try c = 4. We need 3n + 10 <= 4n.
- Subtract 3n from both sides. This gives 10 <= n.
- So n0 = 10 works. For every n >= 10, 3n + 10 <= 4n.
The chart shows the two lines. Below n = 10 the bound is violated. From n = 10 on, 4n stays above 3n + 10.
Now try to prove n^2 = O(n). We would need n^2 <= c * n for all large n. Dividing by n gives n <= c. No fixed c is larger than every n, so the claim is false.
You can also test a candidate pair (c, n0) with code. A check over a finite range is not a proof, but it quickly catches bad choices.
def holds(f, g, c, n0, limit=10_000):
"""Check f(n) <= c * g(n) for n0 <= n < limit."""
return all(f(n) <= c * g(n) for n in range(n0, limit))
f = lambda n: 3 * n + 10
print(holds(f, lambda n: n, c=4, n0=10)) # True
print(holds(f, lambda n: n, c=4, n0=1)) # False: fails at n = 1..9
print(holds(lambda n: n * n, lambda n: n, c=1000, n0=1)) # False: fails once n > 1000Big O is an upper bound, not an exact rate
Because the definition only requires f(n) <= c * g(n), many bounds are valid at once. The function 3n + 10 is O(n), but it is also O(n^2) and O(2^n). Those bounds are true but loose.
By convention, we report the tightest bound we can justify. Saying "binary search is O(n^3)" is correct and useless. The next chapters cover Big Theta, which states a tight bound.
Common errors
- Reading O as "worst case". Big O describes a function's growth. Worst case, best case, and average case each give a different f(n), and each can have its own Big O bound.
- Treating "=" as equality. In f(n) = O(g(n)), the equals sign means "belongs to the set of functions bounded by g". Writing O(n) = f(n) is meaningless.
- Ignoring n0. An algorithm that is O(n) can still lose to an O(n^2) algorithm on small inputs. Big O makes no claim below n0.
- Using Big O for exact timing. It says nothing about seconds. It only tells you how the cost scales as n grows.
Exercise
Show that f(n) = 5n + 20 is O(n) by giving constants c and n0 that satisfy the definition.
Show answer
Choose c = 6. We need 5n + 20 <= 6n, which simplifies to 20 <= n. So n0 = 20 works. For every n >= 20, 5n + 20 <= 6n, so f(n) = O(n). Other valid pairs exist, such as c = 25 and n0 = 1.