Loading lessons...
Divide-and-Conquer Recurrences & the Master Theorem
1. Divide-and-Conquer Recurrences
An algorithm that splits a problem of size n into a subproblems of size n/b and spends f(n) to divide and combine has running time
| Algorithm | Recurrence | Solution |
|---|---|---|
| Binary search | T(n) = T(n/2) + 1 | Θ(log n) |
| Merge sort | T(n) = 2T(n/2) + n | Θ(n log n) |
| Max/min by halves | T(n) = 2T(n/2) + 2 | Θ(n) |
| Karatsuba multiplication | T(n) = 3T(n/2) + n | Θ(n1.585) |
| Strassen matrix multiplication | T(n) = 7T(n/2) + n² | Θ(n2.81) |
2. Substitution (Iteration) Method
T(n) = 2T(n/2) + n, T(1) = 1 (n a power of 2): T(n) = 2[2T(n/4) + n/2] + n = 4T(n/4) + 2n = … = 2k T(n/2k) + kn. Put 2k = n (k = log₂ n): T(n) = n + n log₂ n = Θ(n log n).
T(n) = nT(n − 1), T(1) = 1 → T(n) = n! = O(nⁿ).
3. Recursion-Tree Method
Draw the tree of subproblem costs and add level by level.
Recursion tree for T(n) = aT(n/b) + f(n): level j has aʲ nodes of cost f(n/bʲ)
- 2T(n/2) + cn: every level costs cn, there are log₂ n levels → Θ(n log n).
- 3T(n/4) + cn²: level j costs (3/16)j cn2 - a decreasing geometric series, total ≤ (16/13)cn² → Θ(n²).
- T(n/3) + T(2n/3) + cn: each level ≤ cn, longest path has log3/2 n levels → O(n log n).
4. The Master Theorem
Let a ≥ 1, b > 1, T(n) = aT(n/b) + f(n), and let E = logb a (the critical exponent). Compare f(n) with nE:
| Case | Condition | Solution |
|---|---|---|
| 1 | f(n) = O(nE-ε) for some ε > 0 (leaves dominate) | T(n) = Θ(nE) |
| 2 | f(n) = Θ(nE) (balanced) | T(n) = Θ(nE log n) |
| 3 | f(n) = Ω(nE+ε) and a·f(n/b) ≤ c·f(n) for some c < 1 (root dominates) | T(n) = Θ(f(n)) |
Examples
| Recurrence | a, b, nE | Case | Answer |
|---|---|---|---|
| 9T(n/3) + n | 9, 3, n² | 1 | Θ(n²) |
| 8T(n/2) + n² | 8, 2, n³ | 1 | Θ(n³) |
| T(2n/3) + 1 | 1, 3/2, 1 | 2 | Θ(log n) |
| 2T(n/2) + n | 2, 2, n | 2 | Θ(n log n) |
| 3T(n/4) + n log n | 3, 4, n0.79 | 3 | Θ(n log n) |
| 2T(n/2) + n log n | 2, 2, n | gap (not covered) | Θ(n log² n) by the extended form below |
Teacher's (h, u) form
Write T(n) = nE[T(1) + u(n)] where h(n) = f(n)/nE:
| h(n) | u(n) |
|---|---|
| nr, r > 0 | O(nr) |
| nr, r < 0 | O(1) |
| (log n)i, i ≥ 0 | (log2 n)i+1/(i + 1) |
Example 8T(n/2) + n²: E = 3, h = n²/n³ = n⁻¹ → u = O(1) → T = Θ(n³). For T(n/2) + c: E = 0, h = c = c(log n)⁰ → u = Θ(log n) → T = Θ(log n). For 2T(n/2) + n log n: h = log n → u = (log n)²/2 → T = Θ(n log² n).
- Proof of the Master Theorem (n an exact power of b)
Step 1-unroll the recurrence. Expanding j levels, level j has aʲ subproblems of size n/bʲ. After L = logb n levels the subproblems have size 1, and there are aL = nE (a to the power "log base b of n" equals n to the power "log base b of a" - take logs base b of both sides) leaves. Hence
Step 2-bound g(n) in each case.
Case 1. f(n/bj) = O((n/bj)E-ε), so g(n) = O(nE-ε ∑ (abε/bE)j) = O(nE-ε ∑ (bε)j) since bE = a. This geometric series is O(bεL) = O(nε), so g(n) = O(nE) and T(n) = Θ(nE).
Case 2. aj f(n/bj) = Θ(aj (n/bj)E) = Θ(nE) because aj/bjE = 1. There are L = Θ(log n) terms, so g(n) = Θ(nE log n) - this dominates the leaves.
Case 3. The regularity condition a·f(n/b) ≤ c·f(n) iterates to aj f(n/bj) ≤ cj f(n), so g(n) ≤ f(n)(1 + c + c² + …) = f(n)/(1 − c) = O(f(n)). Also g(n) ≥ f(n) (the j = 0 term). Since f(n) = Ω(nE+ε) dominates the leaves, T(n) = Θ(f(n)). ∎
For n not a power of b, floors and ceilings change T(n) by only a constant factor, so the same bounds hold.
Exam answer outline: (1) recursion tree, (2) leaves = nE, (3) g(n) as a sum, (4) geometric-series argument for each case.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. State and prove the Master theorem. (10 Marks)
- Q2. Solve using the Master theorem: (i) T(n) = 9T(n/3) + n (ii) T(n) = 2T(n/2) + n (iii) T(n) = 3T(n/4) + n log n. (5 Marks)
- Q3. Solve T(n) = 2T(n/2) + n, T(1) = 1 by substitution and by recursion tree. (5 Marks)
- Q4. Write the recurrence for binary search and solve it. (2.5 Marks)