Sub topic 4
Simplifying: Dropping Constants and Lower-Order Terms
This chapter assumes you can read Python loops and functions, and that you know the definition of Big O from the previous chapter: f(n) is O(g(n)) if there are constants c and n0 such that f(n) ≤ c·g(n) for every n ≥ n0. It also assumes basic algebra with exponents. Logarithms appear briefly.
Why simplify at all
When you count the steps an algorithm takes, you get an exact expression such as 3n² + 5n + 20. That expression depends on details you usually can't know in advance: which CPU runs the code, how the compiler orders instructions, or whether you count a comparison as one step or two.
Big O drops those details on purpose. The result is a short statement of how cost grows with input size, and that statement stays true across machines and languages. Two rules do almost all the work.
The two rules
- Drop constant factors. O(5n) is O(n).
- Drop lower-order terms. O(n² + n) is O(n²).
Apply them together to the example: 3n² + 5n + 20 becomes O(n²).
Rule 1: drop constant factors
A constant factor multiplies the whole cost by a fixed number. The definition of Big O already contains a free constant c, so multiplying by a constant never changes the class. If f(n) = 5n, choose c = 5 and g(n) = n. Then f(n) ≤ 5·n for all n, so f(n) is O(n).
This is why "this loop does three operations per iteration" and "this loop does one" have the same Big O. They differ in real running time, but they grow in the same way.
Rule 2: drop lower-order terms
For large n, the fastest-growing term takes over the total. The table shows each term of 3n² + 5n + 20 at three input sizes.
| n | 3n² | 5n | 20 |
|---|---|---|---|
| 10 | 300 | 50 | 20 |
| 100 | 30,000 | 500 | 20 |
| 1,000 | 3,000,000 | 5,000 | 20 |
At n = 1,000 the quadratic term is more than 99.8% of the total. The other terms still exist, but they no longer change the shape of the growth.
You can run this check yourself by counting steps in code.
def steps(n):
count = 20 # setup work: constant
for _ in range(n):
count += 5 # 5 steps per iteration: 5n in total
for _ in range(n):
for _ in range(n):
count += 3 # 3 steps per inner iteration: 3n^2 in total
return count
for n in (10, 100, 1000):
print(f"{n:>5} {steps(n):>9} {steps(n) / n**2:.3f}")The output is:
10 370 3.700
100 30520 3.052
1000 3005020 3.005The last column is steps(n) divided by n². It settles toward the constant 3 as n grows. That is what O(n²) claims: the cost is bounded by a constant multiple of n².
Checking a simplification against the definition
You can prove the simplification instead of trusting it. To show 3n² + 5n + 20 is O(n²), find c and n0.
For n ≥ 1, we have n ≤ n² and 1 ≤ n². So:
3n² + 5n + 20 ≤ 3n² + 5n² + 20n² = 28n²
Choose c = 28 and n0 = 1. The definition holds, so the function is O(n²).
Many pairs work. With c = 4, the inequality 3n² + 5n + 20 ≤ 4n² needs n² ≥ 5n + 20, which first holds at n = 8. So c = 4 and n0 = 8 also work. The definition only asks that some pair exists. Don't spend effort finding the best one.
The same method shows why you can't drop terms in the other direction. No constant c makes n² ≤ c·n true for all large n, because n² / n = n grows without limit. So n² is not O(n).
The procedure
Use the same steps every time you simplify an exact count.
Rules for combining pieces
Real code is built from several parts. These rules tell you how to combine their costs.
- Sum rule. Code blocks that run one after another add their costs. The total is dominated by the largest. A loop of O(n) followed by a nested loop of O(n²) is O(n + n²), which is O(n²).
- Product rule. A block nested inside another multiplies their costs. An O(n) loop whose body costs O(log n) is O(n log n).
- Constants in exponents of the loop count do not matter. Running a loop 10 times, or exactly 3 times, adds only a constant factor.
For example:
def summarize(items):
smallest = min(items) # O(n)
largest = max(items) # O(n)
ordered = sorted(items) # O(n log n)
return smallest, largest, orderedThe cost is O(n + n + n log n) = O(2n + n log n). Drop the constant 2, then drop the linear term, since n grows more slowly than n log n. The result is O(n log n).
Cases that look like they break the rules
Most mistakes in simplification come from a few specific situations.
Logarithm bases are constants
Changing the base of a logarithm multiplies it by a constant: log₂ n = log₁₀ n / log₁₀ 2. Since 1 / log₁₀ 2 is a fixed number, the base falls under Rule 1. You can write O(log n) without naming a base.
Exponentials are not interchangeable
2^(n+1) = 2 · 2^n, so the "+1" in the exponent gives only a constant factor and O(2^(n+1)) is O(2^n). But 4^n = (2^n)², and 4^n / 2^n = 2^n grows without limit. So 4^n is not O(2^n). The same holds for 3^n versus 2^n.
Lower-order only means lower in growth
"Lower-order" refers to growth rate, not to how the term is written. In n² + n log n, the term n log n is lower-order because n log n grows more slowly than n². Likewise, in n log n + n, the plain n is lower-order. Compare terms using the ranking from the previous chapter: 1, log n, n, n log n, n², n³, 2^n, n!.
Several input variables
If a function takes two independent inputs, you cannot decide which dominates. Consider this code:
def pair_count(a_items, b_items):
count = 0
for a in a_items: # runs len(a_items) times
for b in b_items: # runs len(b_items) times per outer iteration
count += 1
return countIf a = len(aitems) and b = len(bitems), the cost is O(a · b). A function that loops over one list and then the other costs O(a + b). Neither a nor b can be dropped, because either could be far larger than the other. Write O(n²) only if you know both lists have size n.
A "constant" that depends on n
A factor is a constant only if it does not change as n grows. If a loop runs n times and each pass copies a list of length n, that second n is not a constant. The cost is n · n, so O(n²). Check each factor you plan to drop and ask whether it stays fixed for every input size.
Constants you drop still matter in practice
Dropping a constant says nothing about whether it is small. An algorithm that takes 1,000n steps and one that takes n² steps are both fast to describe, but for n below 1,000 the quadratic one does less work. Big O hides this by design. A later chapter returns to it under practical limits.
Common errors
- Writing O(3n) or O(n + 5) as the final answer. Both should be O(n).
- Keeping a lower-order term "to be safe". O(n² + n) is correct but not simplified, and it suggests the author does not know which term dominates.
- Dropping a variable that is independent. O(a + b) is not O(a).
- Treating O(2^n) and O(3^n) as the same class because "both are exponential".
- Confusing "drop the constant" with "the constant is unimportant". It affects real running time, just not the growth class.
Exercises
Exercise
Simplify each expression to its Big O class: (a) 7n + 100, (b) n² + n log n + 500, (c) 5 · 2^n + n³, (d) 3 log₂ n + 8.
Show answer
(a) O(n). Drop the constant 100 and the factor 7. (b) O(n²). The term n log n grows more slowly than n², and 500 is a constant. (c) O(2^n). An exponential eventually outgrows any polynomial, so n³ is lower-order, and the factor 5 is dropped. (d) O(log n). Drop the constant 8 and the factor 3, and the base of the logarithm does not matter.
Exercise
Show with the definition of Big O that 2n² + 6n + 4 is O(n²). Give values for c and n0.
Show answer
For n ≥ 1 we have n ≤ n² and 1 ≤ n². So 2n² + 6n + 4 ≤ 2n² + 6n² + 4n² = 12n². Choose c = 12 and n0 = 1. Then f(n) ≤ 12·n² for every n ≥ 1, so the function is O(n²). Other pairs also work, such as c = 3 with n0 = 7.
Exercise
A function takes two lists of lengths a and b. It loops once over the first list, then loops once over the second list, then runs a nested loop over both lists. What is its Big O in terms of a and b?
Show answer
The three parts cost O(a), O(b), and O(a · b). By the sum rule the total is O(a + b + a · b). Since a + b ≤ 2ab when a, b ≥ 1, the nested loop dominates and the answer is O(a · b). Neither variable can be dropped, because they are independent.