In the analysis of algorithms, the master theorem provides a solution in asymptotic terms (using Big O notation) for recurrence relations of types that occur in the analysis of many divide and conquer algorithms. It was popularized by the canonical algorithms textbook Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein. Not all recurrence relations can be solved with the use of the master theorem; its generalizations include the Akra–Bazzi method.
Contents
Introduction
Consider a problem that can be solved using a recursive algorithm such as the following:
procedure T( n : size of problem ) defined as: if n < some constant k then exit Create ′a′ subproblems of size n/b in d(n) time repeat for a total of ′a′ times T(n/b) end repeat Combine results from subproblems in c(n) time end procedureIn the above algorithm we are dividing the problem into a number of subproblems recursively, each subproblem being of size n/b. This can be visualized as building a call tree with each node of the tree as an instance of one recursive call and its child nodes being instances of subsequent calls. In the above example, each node would have a number of child nodes. Each node does an amount of work that corresponds to the size of the sub problem n passed to that instance of the recursive call and given by
Algorithms such as above can be represented as a recurrence relation
The Master theorem allows us to easily calculate the running time of such a recursive algorithm in Θ-notation without doing an expansion of the recursive relation above.
Generic form
The master theorem concerns recurrence relations of the form:
In the application to the analysis of a recursive algorithm, the constants and function take on the following significance:
It is possible to determine an asymptotic tight bound in these three cases:
Generic form
If
then:
Example
As one can see from the formula above:
Next, we see if we satisfy the case 1 condition:
It follows from the first case of the master theorem that
(indeed, the exact solution of the recurrence relation is
Generic form
If it is true, for some constant k ≥ 0, that:
then:
Example
As we can see in the formula above the variables get the following values:
Next, we see if we satisfy the case 2 condition:
So it follows from the second case of the master theorem:
Thus the given recurrence relation T(n) was in Θ(n log n).
(This result is confirmed by the exact solution of the recurrence relation, which is
Generic form
If it is true that:
and if it is also true that:
then:
Example
As we can see in the formula above the variables get the following values:
Next, we see if we satisfy the case 3 condition:
The regularity condition also holds:
So it follows from the third case of the master theorem:
Thus the given recurrence relation T(n) was in Θ(n2), that complies with the f (n) of the original formula.
(This result is confirmed by the exact solution of the recurrence relation, which is
Inadmissible equations
The following equations cannot be solved using the master theorem:
In the second inadmissible example above, the difference between