Sub topic 6
The Master Theorem
This chapter assumes you can read asymptotic notation (O, Omega, Theta), write a recurrence for a recursive function, and expand a recurrence with a recursion tree. The Master theorem turns a common family of recurrences into a one-step lookup. You still need to know when it applies and when it does not.
The Recurrence Family
Divide-and-conquer algorithms often produce a recurrence of this form:
T(n) = a·T(n/b) + f(n)
- a ≥ 1 is the number of subproblems.
- b > 1 is the factor by which the input shrinks.
- f(n) is the cost of dividing the problem and combining the answers. It must be asymptotically positive.
Binary search has a = 1, b = 2, f(n) = Θ(1). Merge sort has a = 2, b = 2, f(n) = Θ(n).
The Critical Exponent
The theorem compares f(n) with the function n^(logb a). This function is the total number of leaves in the recursion tree. Write c* = logb a. Each of the a subproblems splits into a more, and the tree has logb n levels. That gives a^(logb n) = n^(log_b a) leaves.
The comparison decides where the work concentrates:
- If the leaves dominate, the total is about n^c*.
- If every level costs about the same, you pay f(n) once per level, which adds a factor of log n.
- If the root dominates, the total is about f(n).
The Three Cases
| Case | Condition on f(n) | Result | Where the work is |
|---|---|---|---|
| 1 | f(n) = O(n^(c* − ε)) for some ε > 0 | T(n) = Θ(n^c*) | Leaves |
| 2 | f(n) = Θ(n^c* · log^k n) for some k ≥ 0 | T(n) = Θ(n^c* · log^(k+1) n) | Every level equally |
| 3 | f(n) = Ω(n^(c* + ε)) for some ε > 0, and a·f(n/b) ≤ k·f(n) for some constant k < 1 and large n | T(n) = Θ(f(n)) | Root |
The extra condition in Case 3 is the regularity condition. It says the work shrinks by a constant factor as you go down the tree. For polynomial f(n), it holds automatically.
The most common GATE form is Case 2 with k = 0. If f(n) = Θ(n^c), then T(n) = Θ(n^c · log n).
Worked Examples
Binary search: T(n) = T(n/2) + 1. Here a = 1, b = 2, so n^(log_2 1) = n^0 = 1. Since f(n) = 1 = Θ(1), Case 2 with k = 0 applies. T(n) = Θ(log n).
Merge sort: T(n) = 2T(n/2) + n. Here n^(log_2 2) = n. Since f(n) = Θ(n), Case 2 applies. T(n) = Θ(n log n).
Case 1: T(n) = 8T(n/2) + n². Here n^(log_2 8) = n³. Since n² = O(n^(3 − 1)), Case 1 applies. T(n) = Θ(n³).
Case 3: T(n) = 2T(n/2) + n². Here n^(log_2 2) = n, and n² = Ω(n^(1 + 1)). Check regularity: 2·(n/2)² = n²/2 ≤ (1/2)·n². It holds with k = 1/2. T(n) = Θ(n²).
Non-integer exponent: T(n) = 3T(n/4) + n log n. Here c* = log_4 3 ≈ 0.793. Since n log n = Ω(n^(0.793 + ε)), Case 3 is a candidate. Regularity: 3·(n/4)·log(n/4) ≤ (3/4)·n log n. It holds. T(n) = Θ(n log n).
Checking the Result by Computation
The short program below evaluates a recurrence exactly for powers of b and compares the result with the predicted growth. The ratio T(n) / prediction should settle near a constant.
import math
from functools import lru_cache
def make_T(a, b, f):
@lru_cache(maxsize=None)
def T(n):
if n <= 1:
return 1
return a * T(n // b) + f(n)
return T
# T(n) = 2T(n/2) + n -> Theta(n log n)
T = make_T(2, 2, lambda n: n)
for p in (10, 14, 18, 22):
n = 2 ** p
print(n, T(n) / (n * math.log2(n)))
# T(n) = 8T(n/2) + n^2 -> Theta(n^3)
T = make_T(8, 2, lambda n: n * n)
for p in (5, 8, 11, 14):
n = 2 ** p
print(n, T(n) / n ** 3)In the first loop the ratio approaches 1. In the second it approaches 2, because T(n) = 2n³ − n² for these inputs. A bounded ratio is what Θ means.
When the Master Theorem Fails
The theorem has gaps. Check these before applying a case.
- The gap between cases is not polynomial. For T(n) = 2T(n/2) + n / log n, f(n) is smaller than n, but only by a log factor. No ε exists, so Case 1 fails. The standard result is Θ(n log log n), found by a recursion tree, not by the theorem.
- Case 3 without regularity. If f(n) oscillates, such as n²(2 + sin n), the condition can fail even though f is large.
- a is not constant. T(n) = n·T(n/2) + n has a growing with n. The theorem does not apply.
- a < 1. T(n) = 0.5·T(n/2) + n is invalid, because you cannot have half a subproblem.
- Subtraction instead of division. T(n) = T(n − 1) + n is not of the form T(n/b). Use substitution or expansion instead: it gives Θ(n²).
- f(n) is negative or not asymptotically positive. T(n) = 2T(n/2) − n is outside the family.
- Unequal splits. T(n) = T(n/3) + T(2n/3) + n has two different subproblem sizes. Use a recursion tree. Each level costs at most n and the depth is log_{3/2} n, so T(n) = Θ(n log n).
Floors, Ceilings, and Base Cases
Real recurrences use ⌊n/2⌋ or ⌈n/2⌉ and a base case such as T(1) = 1. Neither changes the Θ answer. The theorem holds for floors and ceilings, and any constant base case only shifts constants. So you can treat n as a power of b when you solve.
A Quick Procedure for GATE
- Read off a, b, and f(n).
- Compute c* = log_b a.
- Compare f(n) with n^c* by exponent. Ignore constants.
- Pick the case. For Case 3, confirm regularity, which is automatic for polynomial f.
- If the recurrence is not of the form aT(n/b) + f(n), do not force it. Expand it.
Some quick reference results follow.
| Recurrence | c* | Case | Result |
|---|---|---|---|
| T(n) = T(n/2) + 1 | 0 | 2 | Θ(log n) |
| T(n) = T(n/2) + n | 0 | 3 | Θ(n) |
| T(n) = 2T(n/2) + 1 | 1 | 1 | Θ(n) |
| T(n) = 2T(n/2) + n | 1 | 2 | Θ(n log n) |
| T(n) = 4T(n/2) + n | 2 | 1 | Θ(n²) |
| T(n) = 4T(n/2) + n² | 2 | 2 | Θ(n² log n) |
| T(n) = 3T(n/2) + n | 1.585 | 1 | Θ(n^(log_2 3)) |
The row T(n) = 2T(n/2) + 1 yields Θ(n), which is the cost of recursive tree traversals and recursive maximum-finding.
Exercise
Solve T(n) = 4T(n/2) + n² log n using the extended Master theorem.
Show answer
Here a = 4, b = 2, so n^(log_2 4) = n². Since f(n) = n² log n = Θ(n² · log^1 n), the extended Case 2 applies with k = 1. T(n) = Θ(n² log² n).
Exercise
Why does T(n) = 2T(n/2) + n / log n fall outside the three cases, and what is the answer?
Show answer
n^(log_2 2) = n, and f(n) = n / log n is smaller than n by only a log factor, not a polynomial factor n^ε. Case 1 needs an ε > 0 gap, so it fails. Case 2 needs f(n) = Θ(n · log^k n) with k ≥ 0, but here the exponent of the log is −1, so it fails too. A recursion tree gives a per-level cost of n / (log n − i) at level i. Summing these gives n · H(log n), which is Θ(n log log n).
Exercise
A recursive algorithm splits a problem of size n into 7 subproblems of size n/2 and combines them in Θ(n²) time. What is its running time, and is it faster than an algorithm with T(n) = 8T(n/2) + n²?
Show answer
With a = 7, b = 2, c = log2 7 ≈ 2.807. Since n² = O(n^(2.807 − ε)), Case 1 applies and T(n) = Θ(n^(log2 7)) ≈ Θ(n^2.81). The 8-subproblem version has c = 3 and runs in Θ(n³), so the 7-subproblem version is asymptotically faster. This is Strassen's matrix multiplication.