Master theorem

From Wikipedia, the free encyclopedia

In the analysis of algorithms, the master theorem, which is a specific case of the Akra-Bazzi theorem, provides a cookbook solution in asymptotic terms for recurrence relations of types that occur in practice. It was popularized by the canonical algorithms textbook Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein, which introduces and proves it in sections 4.3 and 4.4, respectively. Nevertheless, not all recurrence relations can be solved with the use of the master theorem.

Contents

[edit] Generic Form

Given a relation of the form:

T(n) = a T\left(\frac{n}{b}\right) + f(n)  \;\;\;\; \emph{where} \;\; a \geq 1 , b > 1

where

  • a = the number of subproblems in the recursion,
  • 1/b = the portion of the original problem represented by each sub-problem
  • f(n) = the cost of dividing the problem + the cost of merging the solution

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

[edit] Case 1

[edit] Generic Form

If it is true that:

f(n) \in \mathcal{O}\left( n^{\log_b a - \epsilon} \right) for some constant ε > 0

it follows that:

T(n) \in \Theta\left( n^{\log_b a} \right).

[edit] Example

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

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

a = 8 \,, b = 2 \,, f(n) = 1000n^2 \,, \log_b a = \log_2 8 = 3 \,

Now you have to check that the following equation holds:

f(n) \in \mathcal{O}\left( n^{\log_b a - \epsilon} \right)

If you insert the values from above, you get:

1000n^2 \in \mathcal{O}\left( n^{3 - \epsilon} \right)

If you choose ε = 1, you get:

1000n^2 \in \mathcal{O}\left( n^{3 - 1} \right) \in \mathcal{O}\left( n^{2} \right)

Since this equation holds, the first case of the master theorem applies to the given recurrence relation, thus resulting in the conclusion:

T(n) \in \Theta\left( n^{\log_b a} \right).

If you insert the values from above, you finally get:

T(n) \in \Theta\left( n^{3} \right).

Thus the given recurrence relation T(n) was in Θ(n³)

[edit] Case 2

[edit] Generic Form

If it is true that:

f(n) \in \Theta\left( n^{\log_b a} \right)

it follows that:

T(n) \in \Theta\left( n^{\log_b a} \log(n)\right).

[edit] Example

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

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

a = 2 \,, b = 2 \,, f(n) = 10n \,, \log_b a = \log_2 2 = 1 \,

Now you have to check that the following equation holds:

f(n) \in \Theta\left( n^{\log_b a} \right)

If you insert the values from above, you get:

10n \in \Theta\left( n^{1} \right) = \Theta\left( n \right)

Since this equation holds, the second case of the master theorem applies to the given recurrence relation, thus resulting in the conclusion:

T(n) \in \Theta\left( n^{\log_b a} \log(n)\right).

If you insert the values from above, you finally get:

T(n) \in \Theta\left( n \log(n)\right).

Thus the given recurrence relation T(n) was in Θ(nlog(n))

[edit] Case 3

[edit] Generic Form

If it is true that:

f(n) \in \Omega\left( n^{\log_b a + \epsilon} \right) for a ε > 0

and if it is also true that:

a f\left( \frac{n}{b} \right) \le c f(n) for a c < 1 and sufficiently large n

it follows that:

T(n) \in \Theta(f(n)).

[edit] Example

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

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

a = 2 \,, b = 2 \,, f(n) = n^2 \,, \log_b a = \log_2 2 = 1 \,

Now you have to check that the following equation holds:

f(n) \in \Omega\left( n^{\log_b a + \epsilon} \right)

If you insert the values from above, and choose ε = 1, you get:

n^2 \in \Omega\left( n^{1 + 1} \right) = \Omega\left( n^2 \right)

Since this equation holds, you have to check the second Condition, namely if it is true that:

a f\left( \frac{n}{b} \right) \le c f(n)

If you insert once more the values from above, you get:

2 \left( \frac{n}{2} \right)^2\le c n^2 \Leftrightarrow \frac{1}{2} n^2 \le cn^2

If you choose c = \frac{1}{2}, it is true that:

\frac{1}{2} n^2 \le \frac{1}{2} n^2 \forall n \ge 1

So it follows:

T(n) \in \Theta(f(n)).

If you insert once more the necessary values, you get:

T(n) \in \Theta(n^2).

Thus the given recurrence relation T(n) was in Θ(n²), that complies with the f(n) of the original formula.

[edit] References

[edit] External Links