Asymptotic notation
Asymptotic notation describes how cost grows for large inputs while ignoring constants. This lesson defines O, Ω, Θ, o and ω precisely, explains upper, lower and tight bounds, dominant terms and hidden constants, and lets you test the definitions with explicit witnesses.
Learning goals
- State the formal definitions of O, Ω and Θ
- Find witnesses c and n₀ for a bound
- Explain why constants and lower-order terms are dropped, and when they matter anyway
Prerequisites
Big O
f(n) = O(g(n)) means there are constants c > 0 and n₀ such that f(n) ≤ c · g(n) for all n ≥ n₀. It is an upper bound: f grows no faster than g, up to a constant factor. Example: 3n² + 5n + 7 = O(n²) with c = 4 and n₀ = 7 (check: 3n² + 5n + 7 ≤ 4n² when n² ≥ 5n + 7, true for n ≥ 7).
Use the explorer: choose f and g, then move c and n₀ until the green region shows f below c · g from n₀ onward. That pair is a witness.
Big-O and Big-Ω witness explorer
Interactive · drag the slidersNot a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.
Big Omega
f(n) = Ω(g(n)) means f(n) ≥ c · g(n) for all n ≥ n₀: a lower bound. Example: 3n² + 5n + 7 = Ω(n²) with c = 3 and n₀ = 1, since the extra terms are positive.
Ω is used for “at least this much work”: any comparison sort is Ω(n log n) in the worst case. Switch the explorer to “Lower bound” to look for a c and n₀ with f above c · g.
Big-O and Big-Ω witness explorer
Interactive · drag the slidersNot a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.
Big Theta
f(n) = Θ(g(n)) means f is both O(g) and Ω(g): it is bounded above and below by constant multiples of g for large n. So 3n² + 5n + 7 = Θ(n²), with constants 3 and 4. Θ states exactly how a cost grows.
Saying “binary search is O(n)” is true but weak; “Θ(log n) in the worst case” is precise.
| Notation | Meaning | Informal |
|---|---|---|
| f = O(g) | f ≤ c·g eventually | grows at most as fast |
| f = Ω(g) | f ≥ c·g eventually | grows at least as fast |
| f = Θ(g) | both | grows at the same rate |
| f = o(g) | f / g → 0 | grows strictly slower |
| f = ω(g) | f / g → ∞ | grows strictly faster |
Big-O and Big-Ω witness explorer
Interactive · drag the slidersNot a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.
little-o
f(n) = o(g(n)) means f grows strictly slower than g: for every c > 0 there is an n₀ with f(n) < c · g(n) for n ≥ n₀, equivalently f(n)/g(n) → 0. So n = o(n²) and log n = o(n), but n² ≠ o(n²).
Little-o is a strict inequality between growth classes, while big-O allows equal growth.
little-omega
f(n) = ω(g(n)) means f grows strictly faster than g: f(n)/g(n) → ∞. So n² = ω(n) and 2ⁿ = ω(n¹⁰⁰). It is the strict counterpart of Ω.
f = ω(g) holds exactly when g = o(f).
Upper and lower bounds
An upper bound on an algorithm says it never costs more than this; a lower bound says it never costs less. A problem has its own bounds too: a problem's upper bound is the cost of the best known algorithm, and its lower bound is what any algorithm must spend. When they meet, the problem is solved optimally; comparison sorting has both at Θ(n log n).
Bounds should say which case they cover: worst, average or best.
| Statement | Kind |
|---|---|
| Merge sort runs in O(n log n) in every case | algorithm upper bound |
| Any comparison sort needs Ω(n log n) comparisons | problem lower bound |
| Therefore merge sort is optimal among comparison sorts | bounds match |
Tight bounds
A bound is tight when it is Θ: it cannot be improved by more than a constant factor. A loose bound is still correct but unhelpful, such as saying insertion sort is O(n³). Always report the tightest bound you can prove.
Test the tightness of a witness in the explorer: if you can also find a lower witness with the same g, the bound is tight.
Big-O and Big-Ω witness explorer
Interactive · drag the slidersNot a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.
Dominant terms
In a sum of terms, the fastest-growing term dominates for large n, so lower-order terms are dropped: 5n³ + 100n² + 1,000n + 10⁶ = Θ(n³). The order from slowest to fastest is 1, log n, √n, n, n log n, n², n³, 2ⁿ, n!.
Be careful: the cost of different phases adds, so it is O(max), but costs inside nested loops multiply.
| Cost | Simplified |
|---|---|
| n² + n log n + 50 | Θ(n²) |
| 3 · 2ⁿ + n⁵ | Θ(2ⁿ) |
| n log n + n | Θ(n log n) |
| n phases of O(n) work | Θ(n²) |
Practice ladder
Now try it yourself
Easy
- Find c and n₀ showing 5n + 20 = O(n)
- Simplify 4n³ + n² log n + 99
Medium
- Prove that n log n = O(n²) but n² ≠ O(n log n)
- Is 2ⁿ⁺¹ = O(2ⁿ)? Is 2²ⁿ = O(2ⁿ)?
Advanced
- Show that log(n!) = Θ(n log n) using the bounds (n/2)^(n/2) ≤ n! ≤ nⁿ
Key takeaways
- O is an upper bound, Ω a lower bound, Θ both; small o and ω are strict.
- Keep the dominant term and drop constants, but remember constants decide real speed.
- Report the tightest bound and say which case it covers.