School/✦ Research Engineer/Week 19
Divide, conquer and count with recurrences
Merge sort splits a list in half, sorts each half, then merges. How do you count the work of something defined in terms of itself?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
FFTs, merge sort, k-d trees, Strassen's matrix multiply and many control and vision algorithms are recursive. Their cost is given by a recurrence, and solving it tells you whether splitting the problem actually helped.
- ◇ Write a recurrence T(n) = a·T(n/b) + f(n) for a divide-and-conquer algorithm.
- ◇ Solve a recurrence by unrolling it and summing the work level by level.
- ◇ Recognise the three regimes of the master theorem and when the work is dominated by the leaves, the levels or the root.
2.Predict
T(n) = 2·T(n/2) + n with T(1) = 1. Roughly what is T(1024)?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the Code Lab, write merge sort with a global counter that increases by one for every element written during a merge. Run it for n = 16, 64, 256 and 1024 and compare the count with n log₂ n. Then change the split to thirds and see what changes.
Can't use the interactive? Read a description instead
The derivation unrolls T(n) = aT(n/b) + n^c level by level: level j costs n^c (a/b^c)^j, so the total is a geometric series whose ratio decides whether the root, every level, or the leaves dominate.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
T(n) = 3·T(n/2) + n for n > 1, with T(1) = 1. What is T(32)?
5.Understand
From code to recurrence
Read the algorithm: how many recursive calls (a), how much smaller each is (b), and how much work outside the calls (f(n)). Merge sort: two calls on halves plus a linear merge, T(n) = 2T(n/2) + n. Binary search: one call on a half plus a comparison, T(n) = T(n/2) + 1. A base case such as T(1) = 1 stops the recursion.
Unroll and sum
Draw the recursion tree. Write the work at each level, then add the levels. This always works, and it shows where the cost lives: at the root, spread evenly, or in the millions of tiny leaves. For exact values with n a power of two, you can also just build upwards: T(1), T(2), T(4), …
The master theorem is a shortcut, not magic
Comparing a with b^c tells you which end of the tree dominates. It needs the subproblems to be equal in size and f(n) to be a polynomial. Uneven splits (quicksort's worst case, T(n) = T(n−1) + n) are not covered: unrolling gives n(n+1)/2, Θ(n²).
Explain it another way
Think of a tournament bracket. Each round halves the players, and every round has the same total number of matches to referee. Rounds × work per round is the total.
A recurrence relates T(n) to T on smaller inputs. Methods: unrolling or recursion trees, substitution (guess and prove by induction), and the master theorem for T(n) = aT(n/b) + Θ(n^c).
With n = 2^k: T(2^k) = 2T(2^(k−1)) + 2^k. Divide by 2^k: S(k) = S(k−1) + 1 with S(0) = 1, so S(k) = k + 1 and T(n) = n(log₂ n + 1).
The Cooley–Tukey FFT has T(n) = 2T(n/2) + n, so it is Θ(n log n) instead of the Θ(n²) of a direct discrete Fourier transform. That single recurrence is why real-time audio and radar processing are possible on small chips.
Derivation: Unrolling T(n) = a·T(n/b) + n^c
Level 0 (the root) does n^c work. It makes a subproblems of size n/b.
Level 1 does a·(n/b)^c = n^c·(a/b^c) work. Level j does n^c·(a/b^c)^j.
There are log_b n levels, ending in a^(log_b n) = n^(log_b a) leaves of cost 1.
So T(n) = n^c · Σⱼ (a/b^c)^j, a geometric series with ratio r = a/b^c.
If r < 1 the root dominates: T = Θ(n^c). If r = 1 every level costs the same: T = Θ(n^c log n). If r > 1 the leaves dominate: T = Θ(n^(log_b a)).
Merge sort: a = 2, b = 2, c = 1, so r = 1 and T = Θ(n log n). Binary search: a = 1, b = 2, c = 0, r = 1, T = Θ(log n).
Worked example 1
Solve T(n) = 4T(n/2) + n, T(1) = 1, for n = 8.
T(2) = 4·1 + 2 = 6. T(4) = 4·6 + 4 = 28. T(8) = 4·28 + 8 = 120. Here a/b^c = 4/2 = 2 > 1, so leaves dominate: Θ(n^(log₂ 4)) = Θ(n²). Check: 2n² − n = 120 ✓.
Worked example 2
Karatsuba multiplies two n-digit numbers with 3 half-size multiplications plus linear work. What is its complexity?
T(n) = 3T(n/2) + n. r = 3/2 > 1, so T = Θ(n^(log₂ 3)) ≈ Θ(n^1.585), better than the schoolbook Θ(n²).
Common mix-ups
- Splitting a problem in half always makes it faster.
✓ Only if the number of subproblems and the combine cost do not outweigh the shrinkage. 4T(n/2) + n is Θ(n²): no better than the direct method. - The master theorem solves every recurrence.
✓ It needs equal-size subproblems and polynomial extra work. Otherwise unroll or use substitution.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
An algorithm checks the middle of a sorted array, then recurses on one half. Which recurrence describes it?
Practice 3 · a fresh version every time
T(n) = 2·T(n/2) + 1 for n > 1, with T(1) = 1. What is T(32)?
Practice 4
For T(n) = 8T(n/2) + n², which part of the recursion tree dominates?
Practice 5
Quicksort's worst case gives T(n) = T(n − 1) + n with T(1) = 1. What is T(10)?
7.Apply it
Write a recursive maximum-finder that splits the array in half, and a recursive one that removes one element at a time. Write both recurrences, solve them, then count operations in the Code Lab for n up to 4096 and plot both against your predictions.
8.Reflect
Concepts: Algorithms, searching and complexity · Root finding and iterative methods