Sub topic 2
The Master Theorem for T(n) = aT(n/b) + f(n)
This chapter assumes you know Big-Theta notation, logarithm identities such as a^(logb n) = n^(logb a), and the recursion tree method from earlier.
The Statement
A divide-and-conquer algorithm splits a problem of size n into a subproblems, each of size n/b. It also spends f(n) time dividing the input and combining the results. Its running time satisfies:
T(n) = aT(n/b) + f(n), with a ≥ 1 and b > 1.
To use the master theorem, first read off the three parameters:
| Algorithm | a (calls) | b (shrink factor) | f(n) (non-recursive work) |
|---|---|---|---|
| Merge sort | 2 | 2 | Θ(n) |
| Binary search | 1 | 2 | Θ(1) |
| Karatsuba | 3 | 2 | Θ(n) |
The Key Comparison: f(n) Versus n^(log_b a)
In the recursion tree, level i has a^i nodes, and the tree has depth logb n. That gives a^(logb n) = n^(logb a) leaves. Let c = logb a, called the critical exponent. Then n^c measures the total work at the leaves, and f(n) is the work at the root. The theorem picks a case by comparing these two.
Case 1: When the Leaves Dominate
If f(n) = O(n^(c−ε)) for some constant ε > 0, then T(n) = Θ(n^c).
In this case the work per level grows geometrically from the root down, so the leaves account for most of the total. For example, T(n) = 8T(n/2) + n² has c = 3, and n² = O(n^(3−1)), so T(n) = Θ(n³). Karatsuba has c = log₂ 3 ≈ 1.585, which gives Θ(n^1.585).
Case 2: When Every Level Does Equal Work
If f(n) = Θ(n^c), then T(n) = Θ(n^c log n).
Each of the roughly log_b n levels does about n^c work, so the total is the work of one level times the number of levels. Merge sort has c = 1 and f(n) = n, which gives Θ(n log n). Binary search has c = 0 and f(n) = 1 = n⁰, which gives Θ(log n). The base of the logarithm only changes a constant factor, so it does not matter here.
A common mistake is to judge by eye whether f "looks linear." Always compute c exactly and compare exponents.
Exercise
Solve T(n) = 4T(n/2) + n and T(n) = 9T(n/3) + n² with the master theorem.
Show answer
For the first, c = log₂ 4 = 2 and n = O(n^(2−1)), so case 1 gives Θ(n²). For the second, c = log₃ 9 = 2 and f(n) = Θ(n²), so case 2 gives Θ(n² log n).