Lecture W2W1: Recurrences, Recursion Trees, and the Master Theorem
September 2, 2026
And a reminder: HW1 is due Wednesday, September 9 at 9:59 PM. Everything on it is covered by the end of today.
By the end of today, you will be able to:
Monday’s class in four bullets:
Go over the math behind the last class then meet a theorem that skips the algebra entirely, and spend the rest of class learning to apply it fast.
\(a^k\) means \(k\) copies of \(a\) multiplied together: \(2^3 = 2 \cdot 2 \cdot 2 = 8\). Every rule below is just counting copies:
\(\log_b n\) answers exactly one question: how many times must I multiply by \(b\) to reach \(n\)?
Check: \(n = 16 \to 8 \to 4 \to 2 \to 1\) is four halvings, and \(\log_2 16 = 4\).
The story: a schoolteacher assigns young Gauss \(1 + 2 + \cdots + 100\) as busywork; he answers \(5050\) in seconds. His idea: do not add left to right; pair the ends.
\[\begin{array}{cccccc} 1 & 2 & 3 & \cdots & n-1 & n\\ n & n-1 & n-2 & \cdots & 2 & 1 \end{array}\]
Look at the column in position \(k\), counting from the left:
Check it at \(n = 5\): the columns are \((1,5),\,(2,4),\,(3,3),\,(4,2),\,(5,1)\), and every one of them totals \(6\).
Every number in the table gets added exactly once either way, so the two grand totals must agree:
Check at \(n = 5\): by rows, \(15 + 15 = 30\); by columns, \(5 \cdot 6 = 30\). So \(2S = 30\) and \(S = 15\), which is indeed \(1+2+3+4+5\).
Whenever you meet \(1 + 2 + \cdots + n\), its value is \(\frac{n(n+1)}{2}\): roughly half of \(n^2\). (\(n(n+1)\) is the area of an \(n \times (n+1)\) rectangle, and two staircase copies of \(S\), one flipped upside down, tile it exactly.) That is the only fact insertion sort’s bill needs.
Monday’s chain, turned into algebra. Each line does one thing:
Sanity check with real numbers: \(n = 4\) gives \(c(4 + 3 + 2 + 1) = 10c\), and the formula gives \(c \cdot \frac{4 \cdot 5}{2} = 10c\). They agree, so trust the letters.
Insertion sort handed us a finished list of numbers to add, \(1 + 2 + \cdots + n\), and Gauss’s pairing trick added it. Merge sort hands us something else entirely:
So this is a genuinely different kind of solve from Gauss. It will end in a sum too, but a much friendlier one: the layers will all turn out to be identical, so no pairing trick is needed at all.
Feed the rule into itself, watching what each substitution does:
Why every substitution adds exactly \(c\,n\): at step \(i\) there are \(2^i\) pending sorts of size \(n/2^i\), and \(2^i \cdot c \cdot \frac{n}{2^i} = c\,n\). Twice as many pieces, each half as big: the two effects cancel perfectly. the layer number cancels here, the way the position \(k\) cancelled for Gauss
The substitutions stop when the pending sorts hit arrays of size 1, which are already sorted:
Sanity check: \(n = 8\) gives \(8 \cdot 3 + 8 = 32\), and Monday’s worksheet counted \(24\) merge copies plus \(8\) base cases. They agree.
Notice where each sum got easy:
| Insertion sort | Merge sort | |
|---|---|---|
| What we had to add | layers of different sizes: \(1, 2, \ldots, n\) | layers of identical size: \(c\,n\) each |
| How many layers | \(n\) | \(\log_2 n\) |
| How to add them | Gauss’s pairing trick | just count: \(\log_2 n\) layers \(\times\) \(c\,n\) |
| The bill | \(\Theta(n^2)\) | \(\Theta(n \log n)\) |
We just spent five lines of algebra on one recurrence, and dozens more are coming this semester. The Master Theorem answers them in one step. Learning to use it is the rest of today.
Merge sort was not a one-off. It is an instance of a design pattern:
For merge sort: \(a = 2\) subproblems, each \(b = 2\) times smaller, and the split plus the two-finger merge cost \(f(n) = \Theta(n)\).
Every algorithm built this way has running time \(T(n) = a \, T(n/b) + f(n)\). Analyze the pattern once, and you have analyzed every algorithm that will ever fit it.
Binary search fits the pattern with the smallest possible numbers: to find key \(k\) in a sorted array, compare against the middle and recurse on one half.
\[T(n) = 1 \cdot T(n/2) + c \qquad (a = 1, \; b = 2, \; f(n) = c)\]




How many leaves does the tree of \(T(n) = a\,T(n/b) + f(n)\) have?
\(n^{\log_b a}\) is the watershed function: the total cost of the bottom of the tree. The whole analysis reduces to one question: does \(f(n)\) grow faster, slower, or at the same rate as \(n^{\log_b a}\)?
Why “watershed”? A watershed is the ridge line on a mountain range: rain that lands on one side flows to one river, rain on the other side flows to the other. \(n^{\log_b a}\) is the ridge.

A course nickname, not standard notation: CLRS just writes \(n^{\log_b a}\).

Before the theorem, the four symbols in \(T(n) = a\,T(n/b) + f(n)\), in plain words:
| Symbol | Meaning |
|---|---|
| \(a\) | how many subproblems each call spawns |
| \(b\) | how many times smaller each subproblem is than its parent |
| \(f(n)\) | the work a call does itself: dividing and combining |
| \(n^{\log_b a}\) | the watershed: the total cost of all the leaves |
Read the first three straight off the recurrence; compute the fourth. Same four every time, for every divide-and-conquer algorithm you will ever meet.
Master Theorem. Let \(T(n) = a\,T(n/b) + f(n)\) with \(a \geq 1\) and \(b > 1\) constants. Compare \(f(n)\) to \(n^{\log_b a}\):
It is not magic: each case is exactly one of the three tree shapes, with the geometric series summed once and for all. The theorem is Monday’s activity, industrialized.
The same three moves, every single time: (1) read \(a\), \(b\), and \(f(n)\) straight off the recurrence, (2) compute the watershed \(n^{\log_b a}\), (3) compare \(f(n)\) against it and name the case. Three examples, solved at sight:
The theorem is powerful, but it cannot digest every recurrence. It stays silent when:
The Master Theorem is a compiled recursion tree. When it does not apply, do not force it; draw the tree.
Take one worksheet per pair. Eight recurrences have come to the clinic complaining about their running times.
Side 1: for each patient, chart \(a\), \(b\), the watershed \(n^{\log_b a}\), the case, and the \(\Theta\) diagnosis. Warning: not every patient can be treated by the Master Theorem; write “refer out” and say why.
Side 2: two published “proofs” contain malpractice; find the flaw in each. Then design your own patient with a prescribed diagnosis.
10 minutes. Worksheets come in at the end; one per pair, both names on it.
| Patient | \(n^{\log_b a}\) | Case | Diagnosis |
|---|---|---|---|
| \(2T(n/2) + c\,n\) | \(n\) | 2 | \(\Theta(n \log n)\) |
| \(9T(n/3) + c\,n\) | \(n^2\) | 1 | \(\Theta(n^2)\) |
| \(T(n/2) + c\) | \(1\) | 2 | \(\Theta(\log n)\) |
| \(3T(n/3) + c\,n^2\) | \(n\) | 3 | \(\Theta(n^2)\) |
| \(8T(n/2) + c\,n^2\) | \(n^3\) | 1 | \(\Theta(n^3)\) |
| \(7T(n/2) + c\,n^2\) | \(n^{\log_2 7}\) | 1 | \(\Theta(n^{\log_2 7})\) |
| \(2T(n/2) + c\,n \log n\) | \(n\) | 2, \(k=1\) | \(\Theta(n \log^2 n)\) |
| \(T(n-1) + c\,n\) | refer out | \(\Theta(n^2)\) by chain |
The last row is the trap that matters most in practice: check the shape before you reach for the theorem.
Multiplying two \(n \times n\) matrices the definitional way costs \(\Theta(n^3)\). Divide and conquer seems to offer hope: split each matrix into four \(\frac{n}{2} \times \frac{n}{2}\) blocks.
\(f(n)\) was untouched; shrinking \(a\) rewrote the watershed. In a leaf-dominated recurrence, the branching factor is where the money is.
Divide and conquer always bills you \(T(n) = a\,T(n/b) + f(n)\). Its recursion tree has three possible shapes: root-heavy, balanced, or leaf-heavy, and the watershed \(n^{\log_b a}\), the cost of the leaves, tells you which by one comparison against \(f(n)\). The Master Theorem is that comparison, precomputed. When the recurrence has the wrong shape, draw the tree instead. And when the leaves win, improving the algorithm means attacking \(a\): that is how matrix multiplication got faster.
No class Monday (Labor Day). We meet again Wednesday, September 9, when Sam takes the wheel.
Topics for Wednesday: Hashing: how dictionaries answer find(k) in constant expected time, without sorting anything. A third notion of cost, expected time, joins worst-case and amortized.
Supplemental reading (highly recommended): CLRS Chapter 11.
Upcoming deliverables:
