Asymptotic Notation: Describing How Cost Grows
In the previous lesson we obtained exact expressions such as and saw that the constants depend on arbitrary counting decisions and, in reality, on the machine. What does not depend on them is the rate of growth of the function: how fast increases when becomes large.
Asymptotic notation is the mathematical language for describing that rate of growth. It refers to the speed of growth of the values of a function as tends to infinity, ignoring constant factors and lower-order terms. It is the notation used in every textbook, paper and technical interview to talk about the efficiency of algorithms.
- The formal definition of (big-O) and how to prove that a function belongs to .
- The related notations (big-Omega) and (big-Theta), and what each one really says.
- The algebraic properties that let us simplify expressions without going back to the definition.
- The logarithm rules you need for analyzing algorithms, and why the base of a logarithm does not matter.
- How to determine the order of complex expressions quickly and reliably.
Big-O: An Upper Bound on Growth
Definition. A function is if there exist two positive constants and such that:
In words: from some point onwards, never exceeds a constant multiple of . If is , we say that has, at most, a growth rate of .
Two details of the definition are important:
- The constant absorbs the constant factors: , and are all . This is exactly the freedom we need, since constants depend on the machine and on the counting conventions.
- The threshold means that only the behavior for large values of matters. What happens for small inputs is irrelevant to the definition.
Proving a bound: an example
Let us prove that is . We need constants and such that:
If we pick , the inequality becomes , which simplifies to , and (dividing by ) to . So it holds mathematically for every , i.e. with and . We can see the crossing point directly in a table:
| ? | |||
|---|---|---|---|
| 1 | 9 | 6 | ✗ |
| 2 | 112 | 96 | ✗ |
| 3 | 513 | 486 | ✗ |
| 4 | 1,536 | 1,536 | ✓ () |
| 5 | 3,625 | 3,750 | ✓ |
| 10 | 54,000 | 60,000 | ✓ |
The pair is not unique—for example, with the inequality already holds from —we only need to show that some valid pair exists.
When the limit exists, we can compare growth rates with:
- If is a finite number (including 0), then is .
- If , grows strictly more slowly than .
- If is a finite positive number, both functions grow at the same rate.
- If , grows faster, and it is not .
For example, , so both functions grow at the same rate.
Big-O is a bound, not an exact description
Since is an upper bound, a function belongs to many classes: is , but also and , because all those functions grow at least as fast as . These statements are true but not very informative. When we analyze an algorithm, we always give the tightest bound we can prove: saying that the sum of a vector is is correct, but misleading.
A common misconception is that describes the worst case. It does not: , and describe functions, and any of the functions we studied in the previous lesson can be described with them. The worst-case time of linear search is , its best-case time is , its average-case time is . When someone says that "an algorithm is " without further detail, they usually mean that its worst-case time is , but the two ideas are independent.
Big-Omega and Big-Theta
Big-O gives an upper bound. Two related notations complete the picture:
- (big-Omega), a lower bound. is if there exist positive constants and such that for all . It says that grows at least as fast as .
- (big-Theta), an exact order. is if it is both and , that is, if there exist positive constants , and such that for all . It says that grows exactly at the rate of , up to constants.
| Notation | Meaning | Analogy | Example |
|---|---|---|---|
| grows at most as fast as | (true, but not tight) | ||
| grows at least as fast as | |||
| grows exactly as fast as |
These notations also allow us to talk about problems, not only algorithms. For example, we saw in the case study that any algorithm for the maximum pairwise product must read all the elements, so every algorithm for that problem takes time in the worst case. Since the linear scan is , it is optimal: no algorithm can have a better growth rate. Lower bounds are what allow us to measure the inherent difficulty of a problem.
In everyday use (and in this course), people often write when they actually mean : "the sum of a vector is " is usually intended as "it grows exactly linearly". Also, although is formally a set of functions, you will commonly see instead of . Read the "" as "is", not as a true equality: is correct, but makes no sense.
Why Constants Can Be Ignored (and When They Matter)
With asymptotic notation, we can evaluate a program by its growth rate, ignoring constants: a program that is will be better than one that is for large enough inputs.
However, there are values of for which an algorithm with time behaves worse than one with time : as long as , is smaller than . Asymptotic notation guarantees that the linear algorithm wins eventually, not that it wins for every input size.
In practice:
- For large inputs, the growth rate is decisive, and the constants become irrelevant.
- For small inputs, constants can matter, and a "worse" algorithm may be faster. This is why, for example, efficient sorting libraries often switch to a simple quadratic algorithm (insertion sort) for very small subarrays.
To facilitate the analysis, the values of the constants are usually removed. But a good engineer remembers that they exist.
Properties of the Asymptotic Notation
The following properties allow us to simplify expressions without returning to the definition every time:
- Reflexivity. is . Every function is a bound of itself (take ).
- Constant factors. for any constant . Multiplying by a constant does not change the class.
- Sum rule. If , with fixed, then is of the order of the largest term: . This is the rule for sequential code: the slowest part dominates.
- Product rule. If , then is . This is the rule for nested code: a loop of iterations whose body costs is .
- Base of logarithms. for any bases .
- Powers inside a logarithm. for any constant .
- Powers of a logarithm. for . Here means , which grows faster than .
Properties 5, 6 and 7 deserve a closer look, because logarithms appear constantly in algorithm analysis.
Why the base of the logarithm does not matter
The change-of-base formula says that, for any bases :
Since is a constant, logarithms in different bases differ only by a constant factor, and property 2 tells us that constant factors do not change the class. That is why we simply write . In computing, when a base is needed, it is usually 2, because many algorithms halve the problem at each step.
Power inside versus power outside
Compare and :
- , which is just a constant times , so it is (property 6).
- is the logarithm raised to the eighth power. It grows faster than any constant times , so it is not (property 7). It is still much smaller than any power of : even is eventually smaller than , although only for very large .
Logarithm Rules to Remember
These identities, valid for positive values and bases different from 1, are used continuously when analyzing algorithms:
Rule 4 is less familiar but very useful, especially in divide and conquer: it lets you swap the base and the argument , turning an expression like (which looks exponential at first sight) into the polynomial . To see why it holds, apply to both sides and use rule 1 ():
- Left-hand side:
- Right-hand side:
Both sides produce the exact same product, so the original positive numbers must be equal. With concrete numbers (): and .
Check your understanding
🧠 Is ?
Yes. It is rule 4 with base 3. Taking of both sides gives in both cases.
🧠 Is ? And ?
The first one, yes: by rule 1, , and both are . The second one, no: is a power of the logarithm and grows faster than (property 7). Always pay attention to whether the exponent applies to or to the logarithm.
🧠 Is ?
No. The logarithm of a product is the sum of the logarithms (rule 2): . Asymptotically, it is still , because is just a constant added to .
🧠 Is ?
Yes. It is rule 5 with and , a form of the change-of-base formula.
Determining the Order of an Expression
In practice, finding the order of an expression consists of identifying its dominant term and dropping its constant factor. To identify the dominant term, it helps to remember the order of growth of the most common functions, from slowest to fastest:
and two general facts:
- Any power of (even a tiny one, such as ) eventually grows faster than any power of .
- Any exponential with eventually grows faster than any polynomial .
The following examples apply these ideas. Cover the "Order" column and try to find the order of each expression yourself; the "Why?" tab explains the ones that hide a trap.
- Examples (I)
- Examples (II)
- Why?
| Order | |
|---|---|
| Order | |
|---|---|
Some of these examples contain a trap:
- : the coefficient of is tiny and the coefficient of is large, but still grows faster. Constants never change the class; they only change when the dominant term takes over (here, for astronomically large ).
- : expanding and multiplying by gives a dominant term .
- : for large , this behaves like .
- : even a small power of beats a logarithm.
- : the constant is enormous, but it is still a constant.
- : compare with by dividing both by : grows faster than .
🧠 Checkpoint: Order these functions
Question: Order these functions by increasing growth rate: , , , , , , , , .
Answer: and (same order; they differ only by a constant factor) .
The most delicate pair is versus : dividing both by , we compare with , and any power of eventually beats the logarithm. However, the crossover happens for an astronomically large , so for any practical input size, is the smaller of the two.
- Reading as an exact order. is true but not tight; use to state the exact order.
- Identifying with the worst case and with the best case. Bounds and cases are independent ideas: each case has its own , and .
- Dropping powers of logarithms. The base of a logarithm does not matter, but .
- Not simplifying. Write , not or .
Summary
- Asymptotic notation describes the growth rate of a function, ignoring constant factors and the behavior for small .
- means from some onwards: an upper bound. is a lower bound and an exact order.
- Asymptotic bounds and the best, worst and average cases are independent ideas.
- Lower bounds on problems let us prove that an algorithm is optimal.
- Key properties: constants are dropped, a sum is dominated by its largest term, products multiply, and the base of logarithms is irrelevant, but powers of logarithms are not.
- To find the order of an expression, identify the dominant term using the hierarchy of functions.
Practice
- ✍️ Prove it. Using the definition, find constants and that prove that is . Then prove that it is also , and conclude that it is .
- ❌ Disprove it. Show that is not . (Hint: assume that constants and exist and find an that breaks the inequality.)
- 🔁 Logarithms. Simplify , and . Which of them grow polynomially?
- 📊 Visualize. Plot , and for between 1 and 200 (with a spreadsheet or any plotting tool). Where does each curve overtake the others?
- 🎯 Tight bounds. For each of the methods you analyzed in the previous lesson (maximum of a vector, checking if a vector is sorted), give the tightest bound for the best and the worst case.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.