Sub topic 10
Dropping Constants: Big-O and Big-Theta
Earlier chapters produced exact step counts such as n, n * m, n(n-1)/2, and floor(log2 n) + 1. Exact counts are useful, but they are often more detail than you need. This chapter shows how to reduce an exact count to its order of growth, which is the part of the count that decides how the running time scales as the input gets large.
This chapter assumes you can read Python loops and that you have worked through the earlier counting chapters. It also assumes you can manipulate polynomials and logarithms, including changing the base of a logarithm.
Why Exact Counts Are Not the Goal
A "step" is a constant-time operation, but different steps cost different amounts. A comparison, an addition, and a list index each take a small fixed time, and those times change with the CPU, the Python version, and the cache state. A count of 3n + 5 therefore does not mean "3n + 5 nanoseconds." It means "some fixed cost per item, plus some fixed overhead."
Constants change from machine to machine. The shape of the count does not. If one function does 3n + 5 steps and another does n(n-1)/2, the second will eventually be slower on every machine, whatever the per-step costs are. Order-of-growth notation records only that shape.
The Definitions
Let f(n) be your exact step count and g(n) a simple reference function such as n, n^2, or log n. All three definitions below are about large n, written "for all n ≥ n0".
| Notation | Meaning | Formal condition |
|---|---|---|
| `f(n) = O(g(n))` | `f` grows no faster than `g` | There exist `c > 0` and `n0` with `f(n) ≤ c·g(n)` for all `n ≥ n0` |
| `f(n) = Ω(g(n))` | `f` grows at least as fast as `g` | There exist `c > 0` and `n0` with `f(n) ≥ c·g(n)` for all `n ≥ n0` |
| `f(n) = Θ(g(n))` | `f` grows at the same rate as `g` | Both `O(g(n))` and `Ω(g(n))` hold |
Big-O is an upper bound, Big-Omega is a lower bound, and Big-Theta is a tight bound. When you have an exact count, you can usually give a Θ bound, and you should, because it says more than O does.
The Practical Rule
For the counts you get from loops, you can reduce to Θ with two moves:
- Keep only the fastest-growing term.
- Drop its constant coefficient.
| Exact count | Dominant term | Order of growth |
|---|---|---|
| `3n + 5` | `3n` | `Θ(n)` |
| `n * m` | `n * m` | `Θ(nm)` |
| `n(n-1)/2 = n^2/2 - n/2` | `n^2/2` | `Θ(n^2)` |
| `floor(log2 n) + 1` | `log2 n` | `Θ(log n)` |
| `n * (floor(log2 n) + 1)` | `n log2 n` | `Θ(n log n)` |
| `4n^2 + 100n log n + 7` | `4n^2` | `Θ(n^2)` |
To pick the dominant term you need the standard ordering of common growth rates.
Any term further right eventually dominates any term to its left, whatever the coefficients are.
Proving a Θ Bound by Hand
The rule is a shortcut for the definition. It helps to apply the definition directly at least once. Take the triangular count f(n) = n(n-1)/2.
Upper bound. For every n ≥ 1, n - 1 < n, so f(n) = n(n-1)/2 ≤ n^2/2. Choose c = 1/2 and n0 = 1. So f(n) = O(n^2).
Lower bound. For n ≥ 2, n - 1 ≥ n/2, so f(n) ≥ n · (n/2) / 2 = n^2/4. Choose c = 1/4 and n0 = 2. So f(n) = Ω(n^2).
Both bounds hold, so f(n) = Θ(n^2). The lower-order term -n/2 only changed which constants work. It did not change the class.
The general argument for a polynomial a_k n^k + ... + a_0 with a_k > 0 works the same way. For large enough n, the lower terms together are smaller than (a_k / 2) n^k, so the polynomial sits between (a_k/2) n^k and 2 a_k n^k.
Checking Numerically with Ratios
If f(n) = Θ(g(n)), the ratio f(n) / g(n) stays between two positive constants as n grows. For polynomial counts the ratio usually settles toward the leading coefficient. You can watch this happen:
def count_pairs(n):
return n * (n - 1) // 2 # exact triangular count
def count_mixed(n):
return 3 * n * n + 10 * n + 50 # a made-up polynomial count
print(f"{'n':>7} {'pairs/n^2':>10} {'mixed/n^2':>10} {'mixed/n':>10}")
for n in (10, 100, 1000, 10000, 100000):
print(f"{n:>7} {count_pairs(n) / n**2:>10.4f} "
f"{count_mixed(n) / n**2:>10.4f} {count_mixed(n) / n:>10.1f}")The pairs/n^2 column approaches 0.5 (0.45, 0.495, 0.4995, ...). The mixed/n^2 column approaches 3 (4.5, 3.105, 3.010, ...). The mixed/n column keeps growing without limit, which shows that count_mixed is not O(n).
A ratio test is evidence, not proof. It is a good way to catch a wrong guess before you try to prove it.
Why the Log Base Does Not Matter
By the change-of-base formula, log_a n = log_b n / log_b a. The factor 1 / log_b a is a constant, so Θ(log2 n), Θ(log10 n), and Θ(ln n) are the same class. That is why the base is usually omitted.
import math
for n in (10, 1000, 10**6, 10**9):
print(n, math.log2(n) / math.log10(n)) # always about 3.3219 = log2(10)This applies only to the base of the logarithm. A constant inside an exponent does matter: 2^(2n) = 4^n, and 4^n / 2^n = 2^n, which grows without bound. So 4^n is not Θ(2^n).
Common Errors
Dropping a variable that is not a constant
The rule "drop lower-order terms" applies to terms in the same variable. A function that loops over a and then over b costs Θ(len(a) + len(b)). You cannot simplify this to Θ(len(a)) unless you know b is never larger than a constant multiple of a. Likewise, n * m stays Θ(nm). Writing it as Θ(n^2) assumes m grows like n, and you need to state that assumption if you make it.
Using O when you mean Θ
O is only an upper bound, so count_single(n) = O(n^3) is technically true and tells the reader almost nothing. When you have counted exactly, report Θ. When people say "this is O(n^2)" informally, they usually mean Θ(n^2). In your own analysis, be precise about which one you mean.
Treating Θ as a single number for all inputs
A Θ bound describes one function of n. If the step count depends on the input's contents as well as its size, as with an early return, then there is no single function to bound. You have to choose the best case, the worst case, or the average case first. The next chapter covers this.
Dropping constants that are hidden loops
Constants can only be dropped if they really are constant. A line such as x in some_list looks like one step but costs up to len(some_list) steps. If you count it as 1 and then drop constants, you get the wrong class. A later chapter covers these hidden loops.
Forgetting that n0 exists
Order of growth makes a claim about large n only. For small inputs, constants can dominate. Compare a 100n algorithm with an n^2 algorithm:
Below n = 100 the quadratic algorithm does fewer steps. This is why library sort routines often switch to insertion sort, which is Θ(n^2), for very small subarrays. Big-O helps you choose for large inputs. For known small inputs, measure.
Summary of the Procedure
Going from loops to an order of growth combines everything so far:
- Count the steps exactly, or bound them tightly, using the loop rules from earlier chapters.
- Expand the count into a sum of terms.
- Identify the fastest-growing term using the growth hierarchy.
- Drop its coefficient and every slower term in the same variables.
- Report the result as Θ when the bound is tight, and as O when you only have an upper bound.
- If in doubt, check the ratio
f(n) / g(n)numerically for growingn.
Exercise
Give the tightest order of growth for f(n) = 5n log2 n + 2n^2 / 1000 + 40n. Which term dominates for n = 1000, and which dominates as n grows without bound?
Show answer
f(n) = Θ(n^2), because n^2 grows faster than n log n and n, so 2n^2/1000 eventually dominates. At n = 1000, the terms are about 5·1000·9.97 ≈ 49,800, then 2,000, then 40,000, so 5n log2 n is the largest. The quadratic term overtakes the n log n term only when 2n/1000 > 5 log2 n, which first happens between n = 70,000 and n = 80,000. This shows why n0 matters.
Exercise
A function runs one loop over a list of length n, then a nested loop over a list of length m inside a loop over the same list of length m. Its exact count is 2n + m^2 + 3. A classmate says this is Θ(m^2). Is that correct?
Show answer
Not in general. The correct bound is Θ(n + m^2). The 2n term can be dropped only if n = O(m^2) is known, for example if n ≤ m. Because n and m are independent variables, neither term is lower-order than the other without an extra assumption.
Exercise
Use the formal definition to show that 3n + 5 = Θ(n). Give explicit constants c and n0 for each bound.
Show answer
Upper bound: for n ≥ 1, 5 ≤ 5n, so 3n + 5 ≤ 8n. Use c = 8 and n0 = 1. Lower bound: for n ≥ 1, 3n + 5 ≥ 3n. Use c = 3 and n0 = 1. Both bounds hold, so 3n + 5 = Θ(n).
Exercise
Is 2^(n+1) = Θ(2^n)? Is 2^(2n) = Θ(2^n)? Explain the difference.
Show answer
2^(n+1) = 2 · 2^n, so it is a constant multiple of 2^n and the answer is yes. 2^(2n) = 4^n, and 4^n / 2^n = 2^n grows without bound, so no constant c satisfies 4^n ≤ c · 2^n for all large n. Adding a constant to the exponent multiplies by a constant. Multiplying the exponent by a constant changes the base, and that changes the growth class.