📚 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:
🔑 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
Recursion Tree
Mathematical Derivation
Starting with the recurrence:
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
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:
Node sizes decrease as we go down (leaves are biggest)
📝 Example: $T(n) = 8T(n/2) + n$
🔬 Case Studies: Real Algorithm Examples
Apply Master's Theorem to Merge Sort and Quick Sort
Merge Sort Analysis
How Merge Sort Works:
- Divide array into 2 equal halves
- Recursively sort each half
- 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
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
Case 1
$$T(n) = \Theta(n^{\log_b a})$$
"Leaves dominate"
Case 2
$$T(n) = \Theta(n^{\log_b a} \log^{k+1} n)$$
"Balanced"
Case 3
$$T(n) = \Theta(f(n))$$
"Root dominates"