Sub topic 4
Describing Running Time with Recurrences
This chapter assumes you know Big-O notation, can trace a recursive function, and have seen the divide, conquer, combine pattern from earlier chapters. The goal here is to turn a recursive algorithm into an equation for its running time. The next two chapters cover how to solve those equations.
What a Recurrence Is
A recurrence relation defines a function in terms of its own values on smaller inputs. For a divide-and-conquer algorithm, let T(n) be the worst-case time on an input of size n. Most of these algorithms fit one template:
T(n) = a · T(n/b) + f(n)
- a is the number of recursive calls the algorithm actually makes.
- n/b is the size of each subproblem.
- f(n) is the work done outside the recursive calls, which covers dividing the input and combining the results.
A base case completes the definition, usually T(1) = O(1).
Reading Recurrences Off Code
To get the recurrence from code, count the recursive calls, note the size of each one, and add up everything else.
| Algorithm | Calls | Subproblem size | Non-recursive work | Recurrence |
|---|---|---|---|---|
| Binary search | 1 | n/2 | compare, O(1) | T(n) = T(n/2) + O(1) |
| Merge sort | 2 | n/2 | merge, O(n) | T(n) = 2T(n/2) + O(n) |
| Fast exponentiation | 1 | n/2 | one or two multiplications, O(1) | T(n) = T(n/2) + O(1) |
| Karatsuba | 3 | n/2 | shifts and additions, O(n) | T(n) = 3T(n/2) + O(n) |
You can check a recurrence by evaluating it directly. The script below computes the merge sort recurrence and compares it with n log₂ n:
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
# T(n) = T(floor(n/2)) + T(ceil(n/2)) + n, with T(1) = 1
if n <= 1:
return 1
return T(n // 2) + T(n - n // 2) + n
for n in [16, 256, 4096]:
print(n, T(n), round(T(n) / (n * math.log2(n)), 3))
# 16 80 1.25
# 256 2304 1.125
# 4096 53248 1.083The ratio settles toward a constant, which suggests that T(n) = Θ(n log n).
Common Errors
- Counting calls the code could make instead of calls it does make. Binary search has two branches, but each step runs only one of them, so a = 1. Writing 2T(n/2) would make it O(n).
- Leaving out hidden costs. In Python, the slice
a[:mid]copies its elements, so it takes O(n) time. In merge sort that cost is absorbed by the O(n) merge. A binary search that slices on each call has T(n) = T(n/2) + O(n), which is O(n), not O(log n). - Assuming the split is even. Quicksort with a bad pivot gives T(n) = T(n − 1) + O(n), which sums to O(n²). The subproblem size depends on how the input is split, not only on how many calls there are.
- Worrying about floors and ceilings. Writing n/2 instead of ⌊n/2⌋ and ⌈n/2⌉ does not change the asymptotic answer for these recurrences.
Exercise
A function splits its input into three parts of size n/3, recurses on all three, and then does a linear-time pass to combine the results. Write its recurrence.
Show answer
T(n) = 3T(n/3) + O(n), with T(1) = O(1).
Exercise
Quickselect recurses into only one side of the partition. Write its recurrence when the pivot always lands in the middle, and again when the pivot is always the smallest element.
Show answer
With a middle pivot, T(n) = T(n/2) + O(n), which is O(n). With the smallest element as pivot, T(n) = T(n − 1) + O(n), which is O(n²).