Asymptotic Notation
How does a function behave when gets large? Asymptotic notation ignores constant factors and small inputs and keeps only the growth rate. It is the language of algorithm analysis: "merge sort is " says how its running time scales, independent of the computer. For an informal first look see Growth Rates and Big-O and Time Complexity; this lesson gives the precise definitions.
Definitions
For functions from to the positive reals:
| Notation | Meaning | Definition |
|---|---|---|
| grows at most as fast as | ||
| at least as fast | ||
| same rate | and | |
| strictly slower | ||
| asymptotically equal |
The "" is a convention: means belongs to the set . It is not symmetric: but .
Example. : for , and , so the expression is at most (take , ); and it is at least .
Limits make it easy
If exists, then means , means , and means . Using L'Hôpital's rule or known limits:
- every polynomial beats every logarithm: for any ;
- every exponential beats every polynomial: for ;
- and .
import math
funcs = {
"log2 n": lambda n: math.log2(n),
"sqrt n": lambda n: math.sqrt(n),
"n": lambda n: n,
"n log2 n": lambda n: n * math.log2(n),
"n^2": lambda n: n ** 2,
"2^n": lambda n: 2.0 ** n,
}
print(f"{'n':>6} " + " ".join(f"{k:>10}" for k in funcs))
for n in [10, 20, 40, 80]:
print(f"{n:>6} " + " ".join(f"{f(n):10.3g}" for f in funcs.values())) n log2 n sqrt n n n log2 n n^2 2^n
10 3.32 3.16 10 33.2 100 1.02e+03
20 4.32 4.47 20 86.4 400 1.05e+06
40 5.32 6.32 40 213 1.6e+03 1.1e+12
80 6.32 8.94 80 506 6.4e+03 1.21e+24
Doubling adds 1 to , multiplies by 4, and squares .
Rules
- Constants don't matter: for ; so and the base of a log is omitted.
- Sums keep the larger term: .
- Products multiply: if and then .
- Transitivity: and give .
Pitfalls
- Big-O is an upper bound only. "Insertion sort is " is true but useless; say (worst case) when you can.
- Constants matter in practice. An algorithm with constant loses to an one with constant 1 for every . Library sorts switch to insertion sort for small arrays for exactly this reason.
- Don't apply Big-O inside an induction. " for all by induction" is nonsense: the constant must not change with .
- Exponents are not constants: is not , since .
import math
# limits of ratios decide the relation
pairs = [("n log n", "n^1.1", lambda n: n * math.log(n) / n ** 1.1),
("n^3", "2^n", lambda n: n ** 3 / 2 ** n),
("4^n", "2^n", lambda n: 4 ** n / 2 ** n),
("3n^2+10n+50", "n^2", lambda n: (3 * n * n + 10 * n + 50) / (n * n))]
for f, g, ratio in pairs:
print(f"{f:>12} / {g:<6}:", " ".join(f"{ratio(10 ** k):.3g}" for k in [1, 2, 3])) n log n / n^1.1 : 1.83 2.91 3.46
n^3 / 2^n : 0.977 7.89e-25 9.33e-293
4^n / 2^n : 1.02e+03 1.27e+30 1.07e+301
3n^2+10n+50 / n^2 : 4.5 3.1 3.01
The first ratio does go to 0, but only for very large (it starts decreasing after ): asymptotic statements can hide large thresholds .
In software
Complexity classes decide what is feasible: simple operations take about a second. So at , is instant, takes minutes, and is out of the question at . Profilers measure constants; asymptotics predict how a program behaves when the data grows by 10× or 1000×.
Exercises
Exercise 1: Prove a Big-O
Show with explicit and .
Answer
, works.
Solution
For : and , so .
Exercise 2: Order these
Sort by growth rate: , , , , , , .
Answer
< (which is ) < and (both ) < < < .
Solution
. By Stirling, . Any exponential with base greater than 1, even , eventually beats any polynomial such as (although only for astronomically large ).
Exercise 3: True or false
(a) ; (b) ; (c) .
Answer
(a) true; (b) false; (c) true.
Solution
(a) , a constant factor. (b) . (c) .
Going further: the Algorithm Theory track applies asymptotic notation to running times, with recursion trees and the master theorem.
Test yourself
Which is ?
If , then…
Is ?
- Yes, constants don't matter
- No, is unbounded
- Only for small
- Yes, by L'Hôpital