algorithm analysis

Master Theorem Guide

Complete reference for applying the Master Theorem to solve divide-and-conquer recurrence relations with detailed examples and case analysis.

15 minutes
Reading time
6
Sections
intermediate
Level
algorithm analysis
Category

The Setup

Form: T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

  • a ≥ 1 = number of recursive subproblems
  • b > 1 = factor by which problem size shrinks
  • f(n) = work done outside recursive calls (divide + combine)

Key Insight: Compare f(n) with n^(log_b a) to determine which dominates

Essential Logarithm Rules

log⁡b(bx)=xExample: log⁡2(23)=3\log_b(b^x) = x \quad \text{Example: } \log_2(2^3) = 3

log⁡b(a)=log⁡(a)log⁡(b)Example: log⁡2(4)=log⁡(4)log⁡(2)=2\log_b(a) = \frac{\log(a)}{\log(b)} \quad \text{Example: } \log_2(4) = \frac{\log(4)}{\log(2)} = 2

log⁡b(xy)=log⁡b(x)+log⁡b(y)Example: log⁡2(8⋅4)=log⁡2(8)+log⁡2(4)=3+2=5\log_b(xy) = \log_b(x) + \log_b(y) \quad \text{Example: } \log_2(8 \cdot 4) = \log_2(8) + \log_2(4) = 3 + 2 = 5

log⁡b(xk)=k⋅log⁡b(x)Example: log⁡2(n3)=3⋅log⁡2(n)\log_b(x^k) = k \cdot \log_b(x) \quad \text{Example: } \log_2(n^3) = 3 \cdot \log_2(n)

Most Critical: n^(log_b a) represents the "breakeven point"

The Three Cases

Case 1: Recursion Dominates

When: f(n)=O(nlog⁡ba−ε)f(n) = O(n^{\log_b a - \varepsilon}) for some ε>0\varepsilon > 0 Result: T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a})

Memory Trick: "Recursion wins" → Answer is just nlog⁡ban^{\log_b a}

Case 2: Perfect Balance

When: f(n)=Θ(nlog⁡ba⋅log⁡kn)f(n) = \Theta(n^{\log_b a} \cdot \log^k n) for some k≥0k \geq 0 Result: T(n)=Θ(nlog⁡ba⋅log⁡k+1n)T(n) = \Theta(n^{\log_b a} \cdot \log^{k+1} n)

Memory Trick: "Tied game" → Answer gets an extra log factor

Case 3: Outside Work Dominates

When: f(n)=Ω(nlog⁡ba+ε)f(n) = \Omega(n^{\log_b a + \varepsilon}) AND regularity condition holds Result: T(n)=Θ(f(n))T(n) = \Theta(f(n))

Memory Trick: "Outside work wins" → Answer is just f(n)f(n)

Regularity Condition: a⋅f(n/b)≤c⋅f(n)a \cdot f(n/b) \leq c \cdot f(n) for some c<1c < 1

Step-by-Step Process

Step 0: Preprocessing - Combine Identical Terms

CRITICAL: Before applying Master Theorem, check if you can combine identical recursive terms:

T(n)=T(n/3)+T(n/3)+n=2T(n/3)+n✓ Can combineT(n) = T(n/3) + T(n/3) + n = 2T(n/3) + n \quad \checkmark \text{ Can combine}

T(n)=T(n/2)+T(n/3)+nCannot combine - different sizes×T(n) = T(n/2) + T(n/3) + n \quad \text{Cannot combine - different sizes} \quad \times

Step 1: Identify Components

  • Extract aa, bb, and f(n)f(n) from T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

Step 2: Calculate Critical Exponent

  • Compute c=log⁡bac = \log_b a
  • This gives you nc=nlog⁡ban^c = n^{\log_b a}

Step 3: Compare and Classify

  • Compare f(n)f(n) with ncn^c
  • Determine which case applies

Step 4: Apply Formula

  • Use the corresponding case formula

Common Examples

Recurrenceabf(n)log⁡ba\log_b aCaseSolution
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n22nnlog⁡22=1\log_2 2 = 1Case 2Θ(nlog⁡n)\Theta(n \log n)
T(n)=4T(n/2)+nT(n) = 4T(n/2) + n42nnlog⁡24=2\log_2 4 = 2Case 1Θ(n2)\Theta(n^2)
T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^222n2n^2log⁡22=1\log_2 2 = 1Case 3Θ(n2)\Theta(n^2)
T(n)=3T(n/3)+nT(n) = 3T(n/3) + n33nnlog⁡33=1\log_3 3 = 1Case 2Θ(nlog⁡n)\Theta(n \log n)
T(n)=9T(n/3)+nT(n) = 9T(n/3) + n93nnlog⁡39=2\log_3 9 = 2Case 1Θ(n2)\Theta(n^2)
T(n)=T(n/2)+1T(n) = T(n/2) + 11211log⁡21=0\log_2 1 = 0Case 2Θ(log⁡n)\Theta(\log n)

Worked Examples

Example 1: Merge Sort

T(n)=2T(n/2)+nT(n) = 2T(n/2) + n

  1. Identify: a=2a = 2, b=2b = 2, f(n)=nf(n) = n
  2. Calculate: log⁡ba=log⁡22=1\log_b a = \log_2 2 = 1, so nlog⁡ba=n1=nn^{\log_b a} = n^1 = n
  3. Compare: f(n)=n=Θ(n1log⁡0n)f(n) = n = \Theta(n^1 \log^0 n)
  4. Case: Case 2 with k=0k = 0
  5. Solution: T(n)=Θ(n1log⁡0+1n)=Θ(nlog⁡n)T(n) = \Theta(n^1 \log^{0+1} n) = \Theta(n \log n)

Example 2: Binary Search

T(n)=T(n/2)+1T(n) = T(n/2) + 1

  1. Identify: a=1a = 1, b=2b = 2, f(n)=1f(n) = 1
  2. Calculate: log⁡ba=log⁡21=0\log_b a = \log_2 1 = 0, so nlog⁡ba=n0=1n^{\log_b a} = n^0 = 1
  3. Compare: f(n)=1=Θ(1⋅log⁡0n)f(n) = 1 = \Theta(1 \cdot \log^0 n)
  4. Case: Case 2 with k=0k = 0
  5. Solution: T(n)=Θ(1⋅log⁡0+1n)=Θ(log⁡n)T(n) = \Theta(1 \cdot \log^{0+1} n) = \Theta(\log n)

Example 3: Matrix Multiplication (Strassen)

T(n)=7T(n/2)+n2T(n) = 7T(n/2) + n^2

  1. Identify: a=7a = 7, b=2b = 2, f(n)=n2f(n) = n^2
  2. Calculate: log⁡ba=log⁡27≈2.807\log_b a = \log_2 7 \approx 2.807, so nlog⁡ba=n2.807n^{\log_b a} = n^{2.807}
  3. Compare: f(n)=n2=O(n2.807−ε)f(n) = n^2 = O(n^{2.807 - \varepsilon}) for ε=0.8\varepsilon = 0.8
  4. Case: Case 1
  5. Solution: T(n)=Θ(nlog⁡27)=Θ(n2.807)T(n) = \Theta(n^{\log_2 7}) = \Theta(n^{2.807})

Common Mistakes

Wrong: Forgetting to combine identical terms

T(n) = T(n/2) + T(n/2) + n
→ "This doesn't fit Master Theorem form"

Correct: T(n)=2T(n/2)+nT(n) = 2T(n/2) + n → Apply Master Theorem

Wrong: Misidentifying f(n)

T(n) = 2T(n/2) + 3n + 5
→ f(n) = 3n (ignoring the constant)

Correct: f(n)=3n+5=Θ(n)f(n) = 3n + 5 = \Theta(n)

Wrong: Incorrect logarithm calculation

T(n) = 4T(n/2) + n
→ log₂(4) = 4

Correct: log⁡2(4)=2\log_2(4) = 2

Quick Reference Card

When you see: T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

  1. Calculate: c=log⁡bac = \log_b a
  2. Compare f(n)f(n) with ncn^c:
    • If f(n)<ncf(n) < n^c → Case 1 → Θ(nc)\Theta(n^c)
    • If f(n)=nclog⁡knf(n) = n^c \log^k n → Case 2 → Θ(nclog⁡k+1n)\Theta(n^c \log^{k+1} n)
    • If f(n)>ncf(n) > n^c → Case 3 → Θ(f(n))\Theta(f(n))

Most Common Results:

  • Binary Search: Θ(log⁡n)\Theta(\log n)
  • Merge Sort: Θ(nlog⁡n)\Theta(n \log n)
  • Matrix Multiplication: Θ(n3)\Theta(n^3) (naive) or Θ(n2.807)\Theta(n^{2.807}) (Strassen)

Share