Many recursive algorithms split their input into equal parts, solve the parts, and combine the answers. Their recurrences all have the same form, and the master theorem solves that form in one step.
The form a T(n/b) + nd
- a is how many calls each call makes (at least 1).
- b is how many times smaller each part is (more than 1).
- nd (d copies of n multiplied together) is the work a call does itself, outside its calls. d = 0 means a fixed amount, because n⁰ = 1, and d = 1 means work in step with n.
Merge sort is a = 2, b = 2, d = 1. Binary search is a = 1, b = 2, d = 0.
Three cases
Look at the recursion tree level by level. Level k has aᵏ calls, each of size n / bᵏ, each doing (n / bᵏ)d work. So level k does
Every level is the level above it times the same ratio r = a / bd. The total is a sum where each term is r times the one before. Only three things can happen:
- r < 1, so a < bd. Each level does less than the one above. The sum is at most a fixed multiple of its first term, so the top level decides: T(n) = Θ(nd).
- r = 1, so a = bd. Every level does nd work, and there are about logb n levels (the number of times you can divide n by b). So T(n) = Θ(nd log n).
- r > 1, so a > bd. Each level does more than the one above. The bottom level, with the most calls, decides. It has alogb n = nlogb a calls, so T(n) = Θ(nlogb a). Here logb a is the power that b must be raised to, to give a: for example, log₂ 8 = 3, because 2³ = 8.
Step through the four examples and watch where the work sits:
| Level | Calls | Size | Each | Level total | Share |
|---|---|---|---|---|---|
| 0 | |||||
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 |
T(n) = 2T(n/2) + n. Each call makes 2 calls on inputs 2 times smaller, and does n steps of work itself. Here n = 16.
The “sum where each term is r times the last” is called a geometric series. The fact behind case 1 is that 1 + r + r² + … stays below 1 / (1 − r) when r < 1, however many terms there are. For r = 1/2 it stays below 2.
Using it
Find a, b and d, compare a with bd, and read off the case:
- T(n) = 2T(n/2) + n: 2 = 2¹, case 2, Θ(n log n).
- T(n) = T(n/2) + 1: 1 = 2⁰, case 2, Θ(log n).
- T(n) = 4T(n/2) + n: 4 > 2¹, case 3, Θ(nlog₂ 4) = Θ(n²), and n² is n times n.
- T(n) = 3T(n/2) + n: 3 > 2, case 3, Θ(nlog₂ 3), which is about Θ(n1.585). This is Karatsuba’s method for multiplying large numbers (the DVCQ station).
- T(n) = 2T(n/2) + n²: 2 < 4, case 1, Θ(n²).
When it does not apply
- The parts are not equal in size, such as T(n) = T(n/3) + T(2n/3) + n. Draw the recursion tree instead.
- The input shrinks by subtracting, such as T(n) = T(n − 1) + n. That is not division by b. Unroll it as in lesson 5.
- The work outside the calls is not a power of n, such as n log n. Extended versions of the theorem exist, but a tree is simpler.
Now try BIGO-P5, Solve the recurrence, which asks you to write the theorem as a function.
BIGO-6 counts as completed once you step through the figure to the last step.