Master Theorem
The master theorem provides a solution to recurrence relations of the form
for constants and with asymptotically positive. Such recurrences occur frequently in the runtime analysis of many commonly encountered algorithms.
Introduction
Many algorithms have a runtime of the form
where is the size of the input and and are constants with asymptotically positive. For instance, one can show that runtime of the merge sort algorithm satisfies
Similarly, traversing a binary tree takes time
By comparing to the asymptotic behavior of , the master theorem provides a solution to many frequently seen recurrences.
Statement of the Master Theorem
First, consider an algorithm with a recurrence of the form
where represents the number of children each node has, and the runtime of each of the three initial nodes is the runtime of .
The tree has a depth of and depth contains nodes. So there are leaves, and hence the runtime is .
Intuitively, the master theorem argues that if an asymptotically positive function is added to the recurrence so that one instead has
it is possible to determine the asymptotic form of based on a relative comparison between and .
Master Theorem
Given a recurrence of the form
for constants ) and with asymptotically positive, the following statements are true:
Case 1. If for some , then .
Case 2. If , then .
Case 3. If for some and for some for all sufficiently large then .
Simply put, if is polynomially smaller than , then dominates, and the runtime is . If is instead polynomially larger than , then dominates, and the runtime is . Finally, if and are asymptotically the same, then .
Note that the master theorem does not provide a solution for all . In particular, if is smaller or larger than by less than a polynomial factor, then none of the three cases are satisfied. For instance, consider the recurrence
In this case, . While is asymptotically larger than , it is larger only by a logarithmic factor; it is not the case that for some . Therefore, the master theorem makes no claim about the solution to this recurrence.
Examples
As mentioned in the introduction, the mergesort algorithm has runtime
and , so case 2 of the master theorem gives .
Similarly, as mentioned before, traversing a binary tree takes time
, which is asymptotically larger than a constant factor, so case 1 of the master theorem gives .
Consider the recurrence
In this case, and . Since is polynomially smaller than , case 1 of the master theorem implies that .
Consider the recurrence
In this case, and . Since is asymptotically the same as , case 2 of the master theorem implies that .
Consider the recurrence
In this case, and . Since is asymptotically larger than , case 3 of the master theorem asks us to check whether for some and all sufficiently large. This is indeed the case, so .
Consider the recurrence
In this case, and . is smaller than but by less than a polynomial factor. Therefore, the master theorem makes no claim about the solution to the recurrence.
See Also
References
[1] Cormen, T.H., et al. Introduction to Algorithms. MIT Press, 2009.