# Master theorem

For the result in enumerative combinatorics, see MacMahon Master theorem.
For the result about Mellin transforms, see Ramanujan's master theorem.

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.

## 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 procedure


In 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 ${\displaystyle f(n)}$. The total amount of work done by the entire tree is the sum of the work performed by all the nodes in the tree.

Algorithms such as above can be represented as a recurrence relation ${\displaystyle T(n)=a\;T\left({\frac {n}{b}}\right)+f(n)}$, where ${\displaystyle f(n)=d(n)+c(n)}$ in the above procedure. This recursive relation can be successively substituted into itself and expanded to obtain expression for total amount of work done.[1]

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:

${\displaystyle T(n)=a\;T\!\left({\frac {n}{b}}\right)+f(n)}$ where ${\displaystyle a>0{\mbox{, }}b>1}$

In the application to the analysis of a recursive algorithm, the constants and function take on the following significance:

• n is the size of the problem.
• a is the number of subproblems in the recursion.
• n/b is the size of each subproblem. (Here it is assumed that all subproblems are essentially the same size.)
• f (n) is the cost of the work done outside the recursive calls, which includes the cost of dividing the problem and the cost of merging the solutions to the subproblems.

It is possible to determine an asymptotic tight bound in these three cases:

### Case 1

#### Generic form

If ${\displaystyle f(n)\in O\left(n^{c}\right)}$ where ${\displaystyle c<\log _{b}a}$ (using big O notation)

then:

${\displaystyle T(n)\in \Theta \left(n^{\log _{b}a}\right)}$

#### Example

${\displaystyle T(n)=8T\left({\frac {n}{2}}\right)+1000n^{2}}$

As one can see from the formula above:

${\displaystyle a=8,\,b=2,\,f(n)=1000n^{2}}$, so
${\displaystyle f(n)\in O\left(n^{c}\right)}$, where ${\displaystyle c=2}$

Next, we see if we satisfy the case 1 condition:

${\displaystyle \log _{b}a=\log _{2}8=3>c}$.

It follows from the first case of the master theorem that

${\displaystyle T(n)\in \Theta \left(n^{\log _{b}a}\right)=\Theta \left(n^{3}\right)}$

(indeed, the exact solution of the recurrence relation is ${\displaystyle T(n)=1001n^{3}-1000n^{2}}$, assuming ${\displaystyle T(1)=1}$).

### Case 2

#### Generic form

If it is true, for some constant k ≥ 0, that:

${\displaystyle f(n)\in \Theta \left(n^{c}\log ^{k}n\right)}$ where ${\displaystyle c=\log _{b}a}$

then:

${\displaystyle T(n)\in \Theta \left(n^{c}\log ^{k+1}n\right)}$

#### Example

${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+10n}$

As we can see in the formula above the variables get the following values:

${\displaystyle a=2,\,b=2,\,c=1,\,f(n)=10n}$
${\displaystyle f(n)=\Theta \left(n^{c}\log ^{k}n\right)}$ where ${\displaystyle c=1,k=0}$

Next, we see if we satisfy the case 2 condition:

${\displaystyle \log _{b}a=\log _{2}2=1}$, and therefore, yes, ${\displaystyle c=\log _{b}a}$

So it follows from the second case of the master theorem:

${\displaystyle T(n)=\Theta \left(n^{\log _{b}a}\log ^{k+1}n\right)=\Theta \left(n^{1}\log ^{1}n\right)=\Theta \left(n\log n\right)}$

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 ${\displaystyle T(n)=n+10n\log _{2}n}$, assuming ${\displaystyle T(1)=1}$.)

### Case 3

#### Generic form

If it is true that:

${\displaystyle f(n)\in \Omega \left(n^{c}\right)}$ where ${\displaystyle c>\log _{b}a}$

and if it is also true that:

${\displaystyle af\left({\frac {n}{b}}\right)\leq kf(n)}$ for some constant ${\displaystyle k<1}$ and sufficiently large ${\displaystyle n}$ (often called the regularity condition)

then:

${\displaystyle T\left(n\right)\in \Theta \left(f(n)\right)}$

#### Example

${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+n^{2}}$

As we can see in the formula above the variables get the following values:

${\displaystyle a=2,\,b=2,\,f(n)=n^{2}}$
${\displaystyle f(n)=\Omega \left(n^{c}\right)}$, where ${\displaystyle c=2}$

Next, we see if we satisfy the case 3 condition:

${\displaystyle \log _{b}a=\log _{2}2=1}$, and therefore, yes, ${\displaystyle c>\log _{b}a}$

The regularity condition also holds:

${\displaystyle 2\left({\frac {n^{2}}{4}}\right)\leq kn^{2}}$, choosing ${\displaystyle k=1/2}$

So it follows from the third case of the master theorem:

${\displaystyle T\left(n\right)=\Theta \left(f(n)\right)=\Theta \left(n^{2}\right).}$

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 ${\displaystyle T(n)=2n^{2}-n}$, assuming ${\displaystyle T(1)=1}$.)

The following equations cannot be solved using the master theorem:[2]

• ${\displaystyle T(n)=2^{n}T\left({\frac {n}{2}}\right)+n^{n}}$
a is not a constant; the number of subproblems should be fixed
• ${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+{\frac {n}{\log n}}}$
non-polynomial difference between f(n) and ${\displaystyle n^{\log _{b}a}}$ (see below)
• ${\displaystyle T(n)=0.5T\left({\frac {n}{2}}\right)+n}$
${\displaystyle a<1}$ cannot have less than one sub problem
• ${\displaystyle T(n)=64T\left({\frac {n}{8}}\right)-n^{2}\log n}$
f(n), which is the combination time, is not positive
• ${\displaystyle T(n)=T\left({\frac {n}{2}}\right)+n(2-\cos n)}$
case 3 but regularity violation.

In the second inadmissible example above, the difference between ${\displaystyle f(n)}$ and ${\displaystyle n^{\log _{b}a}}$ can be expressed with the ratio ${\displaystyle {\frac {f(n)}{n^{\log _{b}a}}}={\frac {n/\log n}{n^{\log _{2}2}}}={\frac {n}{n\log n}}={\frac {1}{\log n}}}$. It is clear that ${\displaystyle {\frac {1}{\log n}} for any constant ${\displaystyle \epsilon >0}$. Therefore, the difference is not polynomial and the Master Theorem does not apply.

## Application to common algorithms

Algorithm Recurrence Relationship Run time Comment
Binary search ${\displaystyle T(n)=T\left({\frac {n}{2}}\right)+O(1)}$ ${\displaystyle O(\log n)}$ Apply Master theorem case ${\displaystyle c=\log _{b}a}$, where ${\displaystyle a=1,b=2,c=0,k=0}$[3]
Binary tree traversal ${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+O(1)}$ ${\displaystyle O(n)}$ Apply Master theorem case ${\displaystyle c<\log _{b}a}$ where ${\displaystyle a=2,b=2,c=0}$[3]
Optimal Sorted Matrix Search ${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+O(\log n)}$ ${\displaystyle O(n)}$ Apply the Akra–Bazzi theorem for ${\displaystyle p=1}$ and ${\displaystyle g(u)=\log(u)}$ to get ${\displaystyle \Theta (2n-\log n)}$
Merge Sort ${\displaystyle T(n)=2T\left({\frac {n}{2}}\right)+O(n)}$ ${\displaystyle O(n\log n)}$ Apply Master theorem case ${\displaystyle c=\log _{b}a}$, where ${\displaystyle a=2,b=2,c=1,k=0}$

## Notes

1. ^ Duke University, "Big-Oh for Recursive Functions: Recurrence Relations", http://www.cs.duke.edu/~ola/ap/recurrence.html
2. ^ Massachusetts Institute of Technology (MIT), "Master Theorem: Practice Problems and Solutions", http://www.csail.mit.edu/~thies/6.046-web/master.pdf
3. ^ a b Dartmouth College, http://www.math.dartmouth.edu/archive/m19w03/public_html/Section5-2.pdf