Sub topic 4
Simplifying: Drop Constants and Lower-Order Terms
This chapter assumes you can read basic Python loops and functions, and that you are comfortable with exponents. You should also know the formal definition of Big O from the previous chapter: f(n) is O(g(n)) if there are constants c > 0 and n0 such that f(n) ≤ c·g(n) for all n ≥ n0.
Why simplify
Counting every operation in a function gives an exact cost, such as 3n² + 5n + 2. That exact figure depends on the language, the compiler, and the hardware. Big O keeps only what matters as n grows, so we simplify the expression to its dominant behavior.
There are two rules.
- Drop constant factors. 3n² becomes n².
- Drop lower-order terms. In n² + 5n + 2, the terms 5n and 2 grow more slowly than n², so they are dropped.
Both rules follow from the definition. They are not shortcuts or approximations.
Why dropping works
Take f(n) = 3n² + 5n + 2. For n ≥ 1, we know n ≤ n² and 1 ≤ n². So:
f(n) = 3n² + 5n + 2 ≤ 3n² + 5n² + 2n² = 10n²
Choose c = 10 and n0 = 1. The definition is satisfied, so f(n) is O(n²). The constant 10 absorbs the constant 3 and the lower-order terms at once.
The table shows how little the lower-order terms matter as n grows.
| n | 3n² | 5n + 2 | Share of total from 5n + 2 |
|---|---|---|---|
| 10 | 300 | 52 | about 15% |
| 100 | 30,000 | 502 | about 1.6% |
| 10,000 | 300,000,000 | 50,002 | about 0.02% |
Applying the rules to code
Here is a function with a mix of work.
def summarize(items):
n = len(items)
total = 0
for x in items: # n steps
total += x
count = 0
for i in range(n): # about n(n-1)/2 steps
for j in range(i + 1, n):
if items[i] == items[j]:
count += 1
return total, count # constant workThe cost is roughly n + n(n-1)/2 + c, where c is a small constant for the setup and return. Expand the middle term: n(n-1)/2 = n²/2 − n/2. The total is n²/2 + n/2 + c.
- Drop the constant factor ½ from the n² term. That leaves n².
- Drop n/2 and c, which are lower-order terms.
- The result is O(n²).
Rules for combining terms
A few patterns come up often.
- Sequential steps add, and the larger one wins. O(n) followed by O(n²) is O(n + n²), which is O(n²).
- Nested steps multiply. An O(n) loop whose body does O(m) work is O(n·m).
- Different inputs keep separate variables. A loop over
afollowed by a loop overbis O(a + b). Do not reduce it to O(n) unless you know the sizes are related. - Constants inside the exponent or base of a power are not constants. O(2ⁿ) and O(3ⁿ) are different classes, and n² and n³ are different too.
Logarithm bases
Logarithms of different bases differ only by a constant factor. The change-of-base rule says log₂(n) = log₁₀(n) / log₁₀(2). Since 1/log₁₀(2) is a constant, you can drop it. This is why we write O(log n) without a base.
Common mistakes
- Dropping a term that is actually dominant. In n + 2ⁿ, the dominant term is 2ⁿ, not n.
- Keeping a lower-order term "to be safe." Writing O(n² + n) is correct but not simplified.
- Treating a constant as free in practice. Two O(n) algorithms can differ by a factor of 100 in real time. Big O does not say which is faster. A later chapter covers this.
Exercise
Simplify each expression to its Big O class: (a) 7n³ + 100n² + 4, (b) n + 5·log n + 1000, (c) 2ⁿ + n¹⁰⁰. For (a), give values of c and n0 that satisfy the definition.
Show answer
(a) O(n³). For n ≥ 1, 100n² ≤ 100n³ and 4 ≤ 4n³, so f(n) ≤ 7n³ + 100n³ + 4n³ = 111n³. Choose c = 111 and n0 = 1. (b) O(n), because log n grows more slowly than n and 1000 is a constant. (c) O(2ⁿ), because any exponential eventually outgrows any polynomial, even n¹⁰⁰.