Master Theorem
How Does the Master Theorem Work?
The Master Theorem solves recurrence relations by comparing two important terms.
- nlogba
- f(n)
The relationship between these two terms determines which case of the Master Theorem should be applied.
Step-by-Step Procedure
- Write the recurrence relation in the standard form.
- Identify the values of a, b and f(n).
- Calculate logb(a).
- Find nlogba.
- Compare f(n) with nlogba.
- Select the correct case.
- Write the final Time Complexity.
Case 1 of Master Theorem
Case 1 in Very Simple Language
Suppose a company has 10 managers, and every manager assigns work to two employees. The amount of work done by the employees is much larger than the time spent assigning the work.
Since the recursive work dominates the dividing and combining work, the total running time mainly depends on the recursive calls.
This situation belongs to Case 1.
Result of Case 1
If
f(n)
is smaller than
n^(logb a)
Then
T(n)=Θ(n^(logb a))
Solved Example 1
T(n)=8T(n/2)+n
Step 1
a=8
b=2
f(n)=n
Step 2
log₂8=3
Therefore
n^(log₂8)=n³
Step 3
Compare
n
and
n³
Since
n
is much smaller than
n³
Case 1 applies.
Answer
Θ(n³)
Solved Example 2
T(n)=9T(n/3)+n
a=9
b=3
f(n)=n
log₃9=2
n^(log₃9)=n²
Compare
n
and
n²
n
is smaller.
Therefore
Case 1
Final Answer
Θ(n²)
Solved Example 3
T(n)=16T(n/4)+n²
a=16
b=4
f(n)=n²
log₄16=2
n^(log₄16)=n²
This example does NOT belong to Case 1.
It belongs to Case 2.
Case 2 of Master Theorem
Case 2 in Very Simple Language
Imagine a company where every manager divides the work equally among two employees.
The time required to divide the work and combine the final result is almost equal to the time spent solving the smaller problems.
Neither part dominates the other. Both contribute equally to the total execution time. This situation is called Case 2.
Result of Case 2
If
f(n)
=
n^(logba)
Then
T(n)
=
Θ(n^(logba) log n)
Solved Example 1
T(n)=2T(n/2)+n
a = 2
b = 2
f(n)=n
Step 1
log₂2 = 1
Step 2
n^(log₂2)
=
n¹
=
n
Step 3
Compare
n
and
n
Both are equal.
Therefore,
Case 2
Answer
Θ(n log n)
Solved Example 2
T(n)=4T(n/2)+n²
a=4
b=2
f(n)=n²
log₂4=2
n^(log₂4)=n²
Both are equal.
Case 2 applies.
Final Answer
Θ(n² log n)
Practical Application - Merge Sort
Merge Sort divides an array into two equal halves until each sub-array contains only one element. After that, all the sub-arrays are merged back into a sorted array.
The recurrence relation of Merge Sort is
T(n)=2T(n/2)+n
Here,
- a = 2
- b = 2
- f(n)=n
Since f(n)=n^(log₂2), Merge Sort belongs to Case 2. Therefore,
Time Complexity
Θ(n log n)
Practical Application - Binary Search
Binary Search repeatedly divides the search space into two equal parts. Its recurrence relation is
T(n)=T(n/2)+1
Although Binary Search is a Divide and Conquer algorithm, it does not satisfy the equality condition required for Case 2. Therefore, it belongs to a different case of the Master Theorem.
Shortcut Trick
Exam Tip
Remember
Case 3 of Master Theorem
Case 3 in Very Simple Language
Suppose a company spends very little time solving smaller problems, but a very large amount of time combining the final reports.
In this situation, the combining work dominates the recursive work. Therefore, the total execution time mainly depends on f(n).
Result of Case 3
If
f(n)
>
n^(logba)
Then
T(n)=Θ(f(n))
Solved Example 1
T(n)=2T(n/2)+n²
a=2
b=2
f(n)=n²
log₂2=1
n^(log₂2)=n
Compare
n²
and
n
n² is larger.
Therefore,
Case 3
Answer
Θ(n²)
Solved Example 2
T(n)=4T(n/2)+n³
a=4
b=2
f(n)=n³
log₂4=2
n^(log₂4)=n²
n³
>
n²
Case 3
Final Answer
Θ(n³)
Comparison of All Three Cases
| Case | Condition | Result |
|---|---|---|
| Case 1 | f(n) is Smaller | Θ(nlogba) |
| Case 2 | f(n) is Equal | Θ(nlogba log n) |
| Case 3 | f(n) is Larger | Θ(f(n)) |
Decision Flowchart
Memory Trick
Where is the Master Theorem Used?
- Merge Sort
- Binary Search
- Quick Sort (Average Case)
- Strassen Matrix Multiplication
- Karatsuba Multiplication
- Closest Pair of Points
- Computational Geometry
- Parallel Algorithms
Limitations of the Master Theorem
- Applicable only to Divide and Conquer recurrences.
- The recurrence must follow the form T(n)=aT(n/b)+f(n).
- Not suitable for T(n)=T(n−1)+1.
- Cannot solve every recurrence relation.
Common Mistakes
- Using the wrong values of a and b.
- Forgetting to calculate logba.
- Comparing coefficients instead of growth rates.
- Ignoring the regularity condition for Case 3.
Practice Problems
- Solve T(n)=2T(n/2)+1.
- Solve T(n)=3T(n/2)+n.
- Solve T(n)=4T(n/2)+n².
- Solve T(n)=8T(n/2)+n.
- Solve T(n)=9T(n/3)+n².
- Solve T(n)=2T(n/2)+n².
- Solve T(n)=16T(n/4)+n².
- Solve T(n)=5T(n/5)+n.
AKTU Important Questions
- State the Master Theorem.
- Explain all three cases with examples.
- Solve T(n)=2T(n/2)+n using the Master Theorem.
- Discuss the limitations of the Master Theorem.
- Differentiate between Case 1, Case 2 and Case 3.
Interview Questions
- What is the Master Theorem?
- When can it be applied?
- When should it not be applied?
- Explain all three cases.
- What is the recurrence relation of Merge Sort?
- Can Binary Search be solved using the Master Theorem?
- Why is Merge Sort classified under Case 2?
- What is the regularity condition?
Summary
The Master Theorem is one of the most important techniques for solving recurrence relations in Divide and Conquer algorithms. It classifies recurrence relations into three cases based on the comparison between f(n) and nlogba. By selecting the appropriate case, the time complexity of many recursive algorithms can be determined quickly without expanding the recurrence relation manually.
Leave Comment