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.

master-theoremrecurrence-relationsdivide-conquercomplexity-analysismathematical-analysis

Cheat Sheet Overview

Quick reference and study metrics

Intermediate
15 minutes

Reading Time

6

Sections

0

Downloads

📚algorithm-analysis

Category

Enhanced SEO fields

Master Theorem

📋 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

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

logb(a)=log(a)log(b)Example: log2(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

logb(xy)=logb(x)+logb(y)Example: log2(84)=log2(8)+log2(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

logb(xk)=klogb(x)Example: log2(n3)=3log2(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(nlogbaε)f(n) = O(n^{\log_b a - \varepsilon}) for some ε>0\varepsilon > 0 Result: T(n)=Θ(nlogba)T(n) = \Theta(n^{\log_b a})

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

Case 2: Perfect Balance 🟡

When: f(n)=Θ(nlogbalogkn)f(n) = \Theta(n^{\log_b a} \cdot \log^k n) for some k0k \geq 0 Result: T(n)=Θ(nlogbalogk+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)=Ω(nlogba+ε)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: af(n/b)cf(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=logbac = \log_b a
  • This gives you nc=nlogban^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)logba\log_b aCaseSolution
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n22nnlog22=1\log_2 2 = 1Case 2Θ(nlogn)\Theta(n \log n)
T(n)=4T(n/2)+nT(n) = 4T(n/2) + n42nnlog24=2\log_2 4 = 2Case 1Θ(n2)\Theta(n^2)
T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^222n2n^2log22=1\log_2 2 = 1Case 3Θ(n2)\Theta(n^2)
T(n)=3T(n/3)+nT(n) = 3T(n/3) + n33nnlog33=1\log_3 3 = 1Case 2Θ(nlogn)\Theta(n \log n)
T(n)=9T(n/3)+nT(n) = 9T(n/3) + n93nnlog39=2\log_3 9 = 2Case 1Θ(n2)\Theta(n^2)
T(n)=T(n/2)+1T(n) = T(n/2) + 11211log21=0\log_2 1 = 0Case 2Θ(logn)\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: logba=log22=1\log_b a = \log_2 2 = 1, so nlogba=n1=nn^{\log_b a} = n^1 = n
  3. Compare: f(n)=n=Θ(n1log0n)f(n) = n = \Theta(n^1 \log^0 n)
  4. Case: Case 2 with k=0k = 0
  5. Solution: T(n)=Θ(n1log0+1n)=Θ(nlogn)T(n) = \Theta(n^1 \log^{0+1} n) = \Theta(n \log n)

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: logba=log21=0\log_b a = \log_2 1 = 0, so nlogba=n0=1n^{\log_b a} = n^0 = 1
  3. Compare: f(n)=1=Θ(1log0n)f(n) = 1 = \Theta(1 \cdot \log^0 n)
  4. Case: Case 2 with k=0k = 0
  5. Solution: T(n)=Θ(1log0+1n)=Θ(logn)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: logba=log272.807\log_b a = \log_2 7 \approx 2.807, so nlogba=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)=Θ(nlog27)=Θ(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: log2(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=logbac = \log_b a
  2. Compare f(n)f(n) with ncn^c:
    • If f(n)<ncf(n) < n^cCase 1Θ(nc)\Theta(n^c)
    • If f(n)=nclogknf(n) = n^c \log^k nCase 2Θ(nclogk+1n)\Theta(n^c \log^{k+1} n)
    • If f(n)>ncf(n) > n^cCase 3Θ(f(n))\Theta(f(n))

Most Common Results:

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

Share this cheat sheet