Lesson 02 / 25
Asymptotic Notation
Definitions of big O, big omega, and big theta, elimination of constants, sum and product rules, and common misreadings.
Contents
The previous lesson settled on operation count as the metric but left a problem: in a thousand-element array, whether 1000 or 1002 comparisons were made does not matter. These details depend on the implementation; what belongs to the algorithm itself is the rate at which the operation count grows as the input grows.
Asymptotic notation is the language that captures this rate and discards the rest.
Big O: Upper Bound
Let and be functions from positive integers to positive real numbers.
In words: if, for sufficiently large , never exceeds a constant multiple of , then grows at most as fast as .
Both quantifiers in the definition are necessary. The constant eliminates factor-of differences — an implementation that runs twice as fast stays in the same class. The threshold eliminates irregularities at small inputs; an asymptotic statement is a claim about large inputs.
This can be shown explicitly with an example. For , choosing , the values and satisfy the definition:
def f(n: int) -> int: return 3 * n**2 + 5 * n + 20 c, n0 = 4, 8 print(f(7) <= c * 7**2) # False — not satisfied below the threshold print(f(8) <= c * 8**2) # True print(all(f(n) <= c * n**2 for n in range(n0, 10_000))) # True
That it does not hold at seven is not a problem: the definition says something about what comes after the threshold. From eight onward, the inequality always holds; hence is written.
Big Omega: Lower Bound
Big O gives a ceiling, big omega gives a floor. It is used to say “this algorithm does at least this much work.”
Lower bounds are powerful when they are stated for a problem, not for a single algorithm: the statement “comparison-based sorting requires at least comparisons” says that no comparison-based algorithm can go below this bound. This kind of result will be taken up together with the proof idea in the searching and sorting topic.
Big Theta: Tight Bound
Theta says that growth is exactly on the order of : both the ceiling and the floor are the same function.
The expression is . The same expression is also — an upper bound may be loose — but it is not .
In everyday use, big O is often written while meaning a tight bound. This is a convenience; where precision matters, theta is preferred.
Simplification Rules
Complexity expressions simplify by a few rules.
Constant factors are eliminated. . The constant is already absorbed by the in the definition.
In a sum, the largest term remains. . Whichever of two sequential parts is more expensive is decisive.
In a product, factors are preserved. Costs multiply in nested loops: .
Transitivity holds. If and , then .
| Expression | Simplified | Reason |
|---|---|---|
| Constants and the constant term are eliminated | ||
| The larger term dominates | ||
| and | The same | A base change is a constant factor |
| and | Different classes | Factorial grows faster than exponential |
The third row answers a frequently asked point: a logarithm’s base is not specified, because a base change produces only a constant factor, and that factor is eliminated.
Finding the Constant and the Threshold
Proving a claim means producing a suitable and . For polynomials there is a mechanical method: for , every lower-degree term is bounded by the dominant term, and the coefficients are summed.
In the example , for it can be written that and . Summed, this gives ; that is, , is also a valid pair that satisfies the definition.
Above, a smaller constant was found with , but it was not necessary: the definition asks for the existence of a pair, not the smallest one. This explains why asymptotic claims are easy to prove — even a crude bound suffices.
Common Misreadings
“ does not mean exactly operations.” It is an upper bound; and are both .
“ does not mean worst case.” These are two separate axes. , , and can each be written separately for the best, worst, and average case. “ in the worst case” and “ in every case” are different claims.
“A smaller class is not always better.” Because asymptotic notation eliminates constants, an algorithm can be slower than an algorithm on small inputs. This is why sorting libraries apply a different algorithm to small chunks.
The notation is not an equality. The way of writing is established but misleading; correctly, it is set membership of the form . For this reason the equality is not symmetric: is not written.
What Is Input Size
One last definition is needed: what does count? The answer varies by problem, and the expression is meaningless if it is not specified.
- In array operations, the number of elements.
- In text algorithms, the number of characters.
- In graph algorithms, both the number of nodes and the number of edges — this is why costs are written with two variables, as in .
- In numerical algorithms, usually the number’s bit length; not the number itself.
The last item is a subtle distinction. A method that tests whether a number is prime by dividing up to the number itself appears to be if the number’s value is taken as the input; but the input is not , it is the number of bits needed to write it. In terms of bit length, the same method is exponential. This distinction is the foundation of classification in complexity theory and is taken up in the Theory of Computation course.
Summary
- Asymptotic notation eliminates constants and lower-order terms, keeping only the growth rate.
- Big O gives an upper bound, big omega a lower bound, big theta a tight bound.
- The constant in the definition eliminates factor-of differences, and the threshold eliminates small inputs.
- In simplification, constants are eliminated, the largest term remains in a sum, and factors are preserved in a product; a logarithm’s base is not written, because a base change is a constant factor.
- and “worst case” are different axes; a smaller asymptotic class does not guarantee being fast on small inputs.
- What is counted as input size must be stated explicitly.
Next Step
Big O and omega do not say whether a bound is tight: is both and . The next lesson will define the little notations that state a bound is strictly loose, and show what statements they make possible.
To keep your progress and take notes, Log in
My notes
Log in to take notes.