Sub topic 16
Analyzing Recursive Algorithms with Recurrence Relations
This chapter assumes you know Big-O notation and have read the earlier chapters on tree recursion, memoization, and merge sort. A recurrence relation describes the cost of a recursive function in terms of the cost of its smaller calls. Once you can write a recurrence, you can estimate both running time and stack depth without running the code.
Writing a Recurrence
To write a recurrence, look at one call and answer two questions:
- How many recursive calls does it make, and on what input sizes?
- How much work does it do outside those calls?
For factorial(n), each call makes one call on n - 1 and does a constant amount of work. That gives T(n) = T(n-1) + O(1), which unrolls to n constant steps, so the time is O(n). The calls form a single chain of n frames, so the stack space is also O(n).
Common Recurrences
| Function | Recurrence | Time | Stack depth |
|---|---|---|---|
| `factorial(n)` | T(n) = T(n-1) + O(1) | O(n) | O(n) |
| `total(items)` with slicing | T(n) = T(n-1) + O(n) | O(n²) | O(n) |
| naive `fib(n)` | T(n) = T(n-1) + T(n-2) + O(1) | O(φⁿ), φ ≈ 1.618 | O(n) |
| memoized `fib(n)` | each of n values computed once | O(n) | O(n) |
| `merge_sort(items)` | T(n) = 2T(n/2) + O(n) | O(n log n) | O(log n) |
Stack depth is the length of the longest chain of calls, not the total number of calls. Naive fib makes an exponential number of calls, but no more than n of them are on the stack at the same time.
Checking a Recurrence Empirically
You can confirm a recurrence by counting calls:
calls = 0
def fib(n):
global calls
calls += 1
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
for n in (10, 20, 25):
calls = 0
fib(n)
print(n, calls)
# 10 177
# 20 21891
# 25 242785Each increase of 5 in n multiplies the call count by about 11, which is φ⁵. That ratio is the signature of exponential growth.
The Recursion Tree Method
For divide-and-conquer recurrences, draw the tree of calls and add up the work at each level.
Each level merges a total of n elements, and halving reaches size 1 after log₂ n levels. That gives a total of O(n log n).
Common Errors
- Ignoring hidden work. Slicing with
items[1:]copies the list, which costs O(n) per call. This turns a linear-lookingtotalinto O(n²). - Confusing calls with depth. Time counts every call. Stack space counts only the deepest chain.
- Assuming halving means O(log n). Halving the input gives log n levels, but the total also depends on how many calls each level makes and how much work each call does.
Exercise
Recursive binary search checks the middle element and then recurses on one half of the list, passing index bounds rather than slices. Write its recurrence and give its time and stack depth.
Show answer
T(n) = T(n/2) + O(1). The input halves log₂ n times and each call does constant work, so the time is O(log n) and the stack depth is O(log n).
Exercise
A function makes three recursive calls on inputs of size n-1 and does constant work otherwise. What is its time complexity?
Show answer
T(n) = 3T(n-1) + O(1). The number of calls triples at each level across n levels, so the time is O(3ⁿ). The stack depth is still O(n).