Lite / Independent Engineering Research / 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?
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.
Predict
T(n) = 2·T(n/2) + n with T(1) = 1. Roughly what is T(1024)?
- About 1,024
- About 11,000
- About 1,000,000
- About 2,048
Guess first, then reveal
There are log₂ 1024 = 10 levels of splitting and each level does n = 1024 work in total, plus the 1024 leaves: 1024 × 10 + 1024 = 11,264. That is n log₂ n + n, the famous Θ(n log n).
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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).
Mathematical
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).
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Where in a recursion tree did you expect the work to be before this lesson, and where is it for merge sort?
Write your answer on paper or in your own notes. Lite never stores what you write.