DM 09 Recurrences and asymptotics
Previous: DM 08 Number Theory and Modular Arithmetic | DM 00 Course Map | Next: DM 10 Graphs
Big-O notation describes how a cost grows when the input gets large, ignoring constants, and recurrences let you compute that growth for recursive algorithms.
Why asymptotics
Running time depends on the machine, the language and the input. What survives all of that is how the cost scales: does doubling the input double the time, quadruple it, or add one step?
| 10 | 3.3 | 10 | 33 | 100 | 1,024 |
| 1,000 | 10 | 1,000 | 10,000 | about | |
| 20 | hopeless |
At a billion operations per second, for takes about 17 minutes; takes 0.02 seconds. No constant-factor tuning bridges a gap like that.
Big-O, Big-Omega, Big-Theta
Big-O (upper bound). if there are constants and such that
“Eventually, grows no faster than a constant multiple of .”
Big-Omega (lower bound). if eventually.
Big-Theta (tight bound). if both hold: same growth rate.
Worked example. Show . For : and , so . Take , . It is also (since it is at least ), hence .
Rules of thumb.
- Keep the fastest-growing term, drop the rest, drop the constant: .
- Logarithm bases do not matter: differs only by a constant, so just write .
- Growth order:
is a true statement, just a weak one. People often write when they mean . When precision matters, say which.
Limit test. If is a positive constant, then . If it is 0, grows strictly slower.
Counting operations in code
for i in range(n): # n iterations
for j in range(n): # n iterations each
work() # constant time.
for i in range(n):
for j in range(i): # i iterations
work(), by the sum formula from DM 06 Induction and Recursion. Half the work, same growth.
while n > 1:
n = n // 2The value halves each time, so the loop runs about times: . Whenever a problem is cut by a constant factor per step, expect a logarithm.
Recurrences
A recursive algorithm’s cost is naturally described by a recurrence.
Binary search. Look at the middle, recurse into one half:
Merge sort. Sort each half recursively, then merge in linear time:
Naive Fibonacci. fib(n) calls fib(n-1) and fib(n-2):
Solving recurrences by unrolling
Binary search. . The size reaches 1 when , so .
Merge sort, as a recursion tree.
- Level 0: one problem of size , merge cost .
- Level 1: two problems of size , total merge cost .
- Level : problems of size , total cost .
- Levels until size 1: .
Total: per level times levels, so . This is why merge sort beats the of simple sorts.
Tower of Hanoi. gave in DM 06 Induction and Recursion: exponential.
The master theorem
For divide-and-conquer recurrences of the form
( subproblems, each of size , plus work to split and combine), compare with :
| Case | Result | Intuition |
|---|---|---|
| the top-level work dominates | ||
| every level contributes equally | ||
| the many tiny subproblems dominate |
Check. Merge sort: , , , and , so . Binary search: , , , and , so .
Example. : , so . Splitting into four half-size problems is no better than brute force here. Karatsuba’s multiplication trick reduces 4 subproblems to 3, giving .
Linear recurrences
For a recurrence like , try . Substituting gives the characteristic equation
If its roots , the general solution is , with fixed by the initial values.
Fibonacci. gives . The larger root is the golden ratio . So
Since , : Fibonacci numbers grow exponentially. So does naive recursive fib, because its call tree mirrors the recurrence. Storing results (memoization) makes it . That change, from exponential to linear, is dynamic programming in one example.
The method is the eigenvalue analysis of LA 08 Eigenvalues and Eigenvectors in disguise: write as a vector updated by a matrix, and the roots are its eigenvalues.
Best, worst and average case
Cost often depends on the particular input, not just its size.
- Worst case: guaranteed upper bound. The usual default.
- Average case: expected cost under some input distribution (PS 04 Expectation and Variance).
- Best case: rarely informative.
Quicksort is in the worst case but on average, and fast in practice.
Space
Memory obeys the same notation. Merge sort uses extra space; recursion uses stack space proportional to its depth. A attention matrix over a sequence of length is the reason long contexts are expensive (Appendix - Neural Networks and Attention).
Common mistakes
- Counting a nested loop as when the inner loop is short or the outer halves.
- Ignoring the cost of library calls.
x in listis ;x in setis on average. - Treating Big-O as exact time. Constants matter for small , and real hardware rewards memory locality.
- Assuming recursion is cheap. Overlapping subproblems can explode.
Learn by doing
- Give a bound for .
- What is the running time of a loop where
istarts at 1 and doubles until it exceeds , with a nested loop of steps inside? - Solve with the master theorem: and .
- Rank: , , , , .
- .
- The outer loop runs times; total .
- First: , . Second: , .
- , , , , .
Where this shows up
Sampling-based planners, nearest-neighbour searches over demonstration datasets, and attention layers each have a growth rate that decides what is feasible on a robot’s compute budget (15 Robot Systems and Deployment).