Sub topic 10
Dropping Constants: Big-O and Big-Theta
This chapter assumes you can already produce exact step counts for single loops, sequential blocks, nested loops, triangular loops, and halving loops, as covered in earlier chapters. It also assumes basic algebra with exponents and logarithms. The goal here is to take a formula such as 3n + 2 or n(n-1)/2 and reduce it to a short label that describes how the cost grows.
Why Exact Counts Are More Than You Need
Exact counts depend on choices that do not matter much. Two people can count the same loop and get 3n + 2 and 4n + 1, because one counted the comparison as a step and the other also counted the increment. Both counts grow in a straight line with n. Doubling n roughly doubles the work in either version. That growth rate is what predicts how a program behaves on large inputs, so it is the part worth keeping.
The Definitions
Let f(n) be your exact step count and g(n) a simple function such as n, n^2, or log n.
- Big-O (upper bound):
f(n)isO(g(n))if there are constantsc > 0andn0such thatf(n) <= c * g(n)for everyn >= n0. - Big-Omega (lower bound):
f(n)isΩ(g(n))if there are constantsc > 0andn0such thatf(n) >= c * g(n)for everyn >= n0. - Big-Theta (tight bound):
f(n)isΘ(g(n))if it is bothO(g(n))andΩ(g(n)).
For example, 3n + 2 <= 4n whenever n >= 2, so 3n + 2 is O(n). Also 3n + 2 >= 3n for all n >= 1, so it is Ω(n). Together these give Θ(n).
For the triangular count n(n-1)/2: it is at most n^2/2, and for n >= 2 it is at least n^2/4. That makes it Θ(n^2).
The Reduction Rules
In practice you rarely write out c and n0. You apply these rules:
- Keep only the fastest-growing term. In
2n^2 + 5n + 7, then^2term dominates. - Drop its constant factor.
2n^2becomesn^2. - Ignore the base of a logarithm.
log2 nandlog10 ndiffer by a constant factor, so both areΘ(log n).
| Exact count | Order of growth |
|---|---|
| `n` | `Θ(n)` |
| `n * m` | `Θ(n·m)` |
| `n(n-1)/2` | `Θ(n^2)` |
| `floor(log2 n) + 1` | `Θ(log n)` |
| `n * (floor(log2 n) + 1)` | `Θ(n log n)` |
Seeing the Constant Settle
The ratio of an exact count to its Theta function should level off at a constant as n grows. This script checks that for the triangular loop:
def count_pairs(n):
steps = 0
for i in range(n):
for j in range(i + 1, n):
steps += 1
return steps
for n in (10, 100, 1000):
steps = count_pairs(n)
print(n, steps, round(steps / n**2, 4))
# 10 45 0.45
# 100 4950 0.495
# 1000 499500 0.4995The ratio approaches 0.5. That constant is the factor Big-Theta discards.
Common Errors
- Treating Big-O as a tight bound. A single loop is
O(n^2), since the bound holds, but that answer is loose and misleading. UseΘwhen you have shown the bound is tight. - Dropping a term in a second variable. In
n + m, neither term dominates unless you know hownandmrelate. KeepΘ(n + m). - Dropping a term that grows faster. In
n log n + 50n, then log nterm wins, becauselog neventually exceeds 50. - Assuming constants never matter. For small inputs, a
Θ(n^2)algorithm with a tiny constant can beat aΘ(n log n)algorithm with a large one. Order of growth describes behavior on large inputs only.
Exercise
A function's exact step count is 5n^2 + 3n·log2(n) + 100. Give its order of growth with Big-Theta, and say why the claim "it is O(n^3)" is true but not useful.
Show answer
It is Θ(n^2). The 5n^2 term grows faster than 3n·log2(n) and the constant 100, and the factor 5 is dropped. The O(n^3) claim is true because n^3 eventually exceeds the count, but it is only an upper bound. It overstates the growth, while Θ(n^2) gives the tight rate.
Exercise
A loop over range(n) runs one block of 2 steps per iteration, then a separate nested loop over range(n) and range(m) runs 1 step per inner iteration, with m >= 1. Write the exact count and its order of growth.
Show answer
The exact count is 2n + n·m. Since m >= 1, 2n <= 2n·m, so the n·m term dominates and the result is Θ(n·m). If m were a fixed constant, this would reduce to Θ(n).