Master Theorem
The Master Theorem provides a quick, direct way to calculate the time complexity of divide-and-conquer algorithms. Instead of manually calculating limits or drawing recurrence trees, you can simply extract variables from the equation and apply a set rule.
The Standard Form
The theorem applies to recurrence relations that fit this specific structure:
-
: Size of the problem.
-
: Number of subproblems in the recursion ().
-
: Factor by which the subproblem size is reduced ().
-
: Power of in the cost of dividing/merging ().
-
: Power of in the cost of dividing/merging ().
The Three Cases
To find the final time complexity, you simply calculate and compare and .
Case 1: Subproblems cost more (Leaves do the most work)
-
Condition:
-
Result: T(n)=Θ(nlogba)
Case 2: Work is distributed equally across all levels
-
Condition:
-
Result: T(n)=Θ(nklogp+1n)
(Note: If there is no log term originally, , making the result simply )
Case 3: Dividing/merging costs more (Root does the most work)
-
Condition:
-
Result: T(n)=Θ(nklogpn)
Quick Example: Merge Sort
The recurrence relation for Merge Sort is:
Let’s extract the variables:
-
(splits into 2 subproblems)
-
(each subproblem is half the size)
-
(the work done outside the recursion is )
-
(there is no term attached to the extra work)
Now, compare and :
-
-
-
Since (), this triggers Case 2.
Result: T(n)=Θ(n1log0+1n)=Θ(nlogn)