Master's Theorem

Interactive Proof Visualization

📚 What is Master's Theorem?

Master's Theorem gives us a quick way to solve recurrences that arise in divide-and-conquer algorithms.

Instead of solving each recurrence from scratch, we can plug values into a formula and get the answer! 🎯

The recurrence form:

$$T(n) = a \cdot T\left(\frac{n}{b}\right) + f(n)$$

🔑 Key Parameters

  • a Number of subproblems
  • b Factor by which we divide n
  • f(n) Work done at each level (outside recursion)

💡 Intuition: The Recursion Tree

Imagine your problem as a tree. At each level, we split into 'a' subproblems, each of size n/b. The work $f(n)$ is done at the root, then $f(n/b)$ at each child, and so on...

Root (Level 0)

1 problem of size n, work = $f(n)$

Level 1

$a$ problems of size $n/b$, total work = $a \cdot f(n/b)$

Level i

$a^i$ problems of size $n/b^i$, total work = $a^i \cdot f(n/b^i)$

Leaves (Level h)

$a^h$ problems of size 1, work = $\Theta(a^h) = \Theta(n^{\log_b a})$

📐 Step-by-Step Proof

Follow the proof as it unfolds

1 / 10

Recursion Tree

Click "Next" to build the tree step by step

Mathematical Derivation

Starting with the recurrence:

$$T(n) = a \cdot T\left(\frac{n}{b}\right) + f(n)$$

This represents a divide-and-conquer algorithm where:

  • We split into a subproblems
  • Each subproblem is 1/b the original size
  • f(n) is the work to divide/combine

📊 The Three Cases - Visual Comparison

Understand when each case applies

Case 1: Leaf Work Dominates

$$f(n) = O(n^{\log_b a - \varepsilon})$$

The work at the root is polynomially smaller than leaf work

What this means:

  • 📉 Work decreases as we go down the tree
  • 🌿 Most work happens at the leaves
  • 📊 The sum is dominated by the last term (geometric series with ratio < 1)

Answer:

$$T(n) = \Theta(n^{\log_b a})$$

Node sizes decrease as we go down (leaves are biggest)

📝 Example: $T(n) = 8T(n/2) + n$

Find: $a=8, b=2 \rightarrow \log_2 8 = 3$
Compare: $f(n)=n$ vs $n^3$
Result: $n = O(n^{3-\varepsilon})$ ✓
$$T(n) = \Theta(n^3)$$

🔬 Case Studies: Real Algorithm Examples

Apply Master's Theorem to Merge Sort and Quick Sort

Merge Sort Analysis

How Merge Sort Works:

  1. Divide array into 2 equal halves
  2. Recursively sort each half
  3. Merge the sorted halves in O(n) time

Step 1: Identify Parameters

• $a = 2$ (we split into 2 subproblems)

• $b = 2$ (each subproblem is half the size)

• $f(n) = n$ (merging takes linear time)

Step 2: Calculate Critical Exponent

$$\log_b a = \log_2 2 = 1$$

So we compare $f(n) = n$ with $n^{\log_b a} = n^1 = n$

Step 3: Match to Case

$$f(n) = n = \Theta(n^1 \cdot \log^0 n)$$

This matches Case 2 with $k = 0$

Recursion Tree for Merge Sort

Final Answer

$$T(n) = \Theta(n \log n)$$

Linear work at each of $\log_2 n$ levels

💡 Intuition

At each level of recursion, we do O(n) work merging. There are $\log_2 n$ levels. So total work = $n \times \log_2 n$.

✏️ Practice Problems

Test your understanding! Click to reveal the answer.

📋 Master's Theorem Cheat Sheet

1️⃣

Case 1

$$f(n) \ll n^{\log_b a}$$

$$T(n) = \Theta(n^{\log_b a})$$

"Leaves dominate"

2️⃣

Case 2

$$f(n) \approx n^{\log_b a} \log^k n$$

$$T(n) = \Theta(n^{\log_b a} \log^{k+1} n)$$

"Balanced"

3️⃣

Case 3

$$f(n) \gg n^{\log_b a}$$

$$T(n) = \Theta(f(n))$$

"Root dominates"