Lesson 22 +40 XP

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

T(n) = aT(n/b) + f(n) , a ≥ 1, b > 1

AlgorithmRecurrenceSolution
Binary searchT(n) = T(n/2) + 1Θ(log n)
Merge sortT(n) = 2T(n/2) + nΘ(n log n)
Max/min by halvesT(n) = 2T(n/2) + 2Θ(n)
Karatsuba multiplicationT(n) = 3T(n/2) + nΘ(n1.585)
Strassen matrix multiplicationT(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ʲ)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:

CaseConditionSolution
1f(n) = O(nE-ε) for some ε > 0 (leaves dominate)T(n) = Θ(nE)
2f(n) = Θ(nE) (balanced)T(n) = Θ(nE log n)
3f(n) = Ω(nE+ε) and a·f(n/b) ≤ c·f(n) for some c < 1 (root dominates)T(n) = Θ(f(n))

Examples

Recurrencea, b, nECaseAnswer
9T(n/3) + n9, 3, n²1Θ(n²)
8T(n/2) + n²8, 2, n³1Θ(n³)
T(2n/3) + 11, 3/2, 12Θ(log n)
2T(n/2) + n2, 2, n2Θ(n log n)
3T(n/4) + n log n3, 4, n0.793Θ(n log n)
2T(n/2) + n log n2, 2, ngap (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 > 0O(nr)
nr, r < 0O(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).

  1. 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

T(n) = Θ(nE) + g(n) , g(n) = ∑j=0L-1 aj f(n/bj)

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.

💡 Tip

Exam answer outline: (1) recursion tree, (2) leaves = nE, (3) g(n) as a sum, (4) geometric-series argument for each case.

🎯 END-TERM EXAM PREP

📝 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)