Master theorem (analysis of algorithms)

Short description: Analysis of divide and conquer algorithms

In the analysis of algorithms, the master theorem for divide-and-conquer recurrences provides an asymptotic analysis for many recurrence relations that occur in the analysis of divide-and-conquer algorithms. The approach was first presented by Jon Bentley, Dorothea Blostein (née Haken), and James B. Saxe in 1980, where it was described as a "unifying method" for solving such recurrences.[1] The name "master theorem" was popularized by the widely-used algorithms textbook Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein.

Not all recurrence relations can be solved by this theorem; its generalizations include the Akra–Bazzi method.

## Contents

* 1 Introduction
* 2 Generic form
* 2.1 Examples
* 2.1.1 Case 1 example
* 2.1.2 Case 2 example
* 2.1.3 Case 3 example
* 3 Inadmissible equations
* 4 Application to common algorithms
* 5 See also
* 6 Notes
* 7 References

## Introduction

Consider a problem that can be solved using a recursive algorithm such as the following:

procedure p(input x of size n):
if n < some constant k:
Solve x directly without recursion
else:
Create a subproblems of x, each having size n/b
Call procedure p recursively on each subproblem
Combine the results from the subproblems

Solution tree.

The above algorithm divides the problem into a number of subproblems recursively, each subproblem being of size n/b. Its solution tree has a node for each recursive call, with the children of that node being the other calls made from that call. The leaves of the tree are the base cases of the recursion, the subproblems (of size less than k) that do not recurse. The above example would have a child nodes at each non-leaf node. Each node does an amount of work that corresponds to the size of the subproblem n passed to that instance of the recursive call and given by f(n). The total amount of work done by the entire algorithm is the sum of the work performed by all the nodes in the tree.

The runtime of an algorithm such as the p above on an input of size n, usually denoted T(n), can be expressed by the recurrence relation

T(n)=aT(nb)+f(n),

where f(n) is the time to create the subproblems and combine their results in the above procedure. This equation can be successively substituted into itself and expanded to obtain an expression for the total amount of work done.[2] The master theorem allows many recurrence relations of this form to be converted to Θ-notation directly, without doing an expansion of the recursive relation.

## Generic form

The master theorem always yields asymptotically tight bounds to recurrences from divide and conquer algorithms that partition an input into smaller subproblems of equal sizes, solve the subproblems recursively, and then combine the subproblem solutions to give a solution to the original problem. The time for such an algorithm can be expressed by adding the work that they perform at the top level of their recursion (to divide the problems into subproblems and then combine the subproblem solutions) together with the time made in the recursive calls of the algorithm. If T(n) denotes the total time for the algorithm on an input of size n, and f(n) denotes the amount of time taken at the top level of the recurrence then the time can be expressed by a recurrence relation that takes the form:

T(n)=aT(nb)+f(n)

Here n is the size of an input problem, a is the number of subproblems in the recursion, and b is the factor by which the subproblem size is reduced in each recursive call (b>1). Crucially, a and b must not depend on n. The theorem below also assumes that, as a base case for the recurrence, T(n)=Θ(1) when n is less than some bound κ>0, the smallest input size that will lead to a recursive call.

Recurrences of this form often satisfy one of the three following regimes, based on how the work to split/recombine the problem f(n) relates to the critical exponent ccrit=logba. (The table below uses standard big O notation).

ccrit=logba=log⁡(#subproblems)/log⁡(relative subproblem size)
Case  | Description  | Condition on f(n) in relation to ccrit, i.e. logba | Master Theorem bound  | Notational examples

|
|
|
|

1  | Work to split/recombine a problem is dwarfed by subproblems.

i.e. the recursion tree is leaf-heavy

| When f(n)=O(nc) where c<ccrit

(upper-bounded by a lesser exponent polynomial)

| ... then T(n)=Θ(nccrit)

(The splitting term does not appear; the recursive tree structure dominates.)

| If b=a2 and f(n)=O(n1/2−ϵ), then T(n)=Θ(n1/2).
2  | Work to split/recombine a problem is comparable to subproblems.  | When f(n)=Θ(nccritlogkn) for a k≥0

(rangebound by the critical-exponent polynomial, times zero or more optional logs)

| ... then T(n)=Θ(nccritlogk+1n)

(The bound is the splitting term, where the log is augmented by a single power.)

| If b=a2 and f(n)=Θ(n1/2), then T(n)=Θ(n1/2log⁡n).

If b=a2 and f(n)=Θ(n1/2log⁡n), then T(n)=Θ(n1/2log2n).

3  | Work to split/recombine a problem dominates subproblems.

i.e. the recursion tree is root-heavy.

| When f(n)=Ω(nc) where c>ccrit

(lower-bounded by a greater-exponent polynomial)

| ... this doesn't necessarily yield anything. Furthermore, if

af(nb)≤kf(n) for some constant k<1 and sufficiently large n (often called the regularity condition)

then the total is dominated by the splitting term f(n):

T(n)=Θ(f(n))
| If b=a2 and f(n)=Ω(n1/2+ϵ) and the regularity condition holds, then T(n)=Θ(f(n)).

A useful extension of Case 2 handles all values of k:[3]

Case  | Condition on f(n) in relation to ccrit, i.e. logba | Master Theorem bound  | Notational examples

|
|
|

2a  | When f(n)=Θ(nccritlogkn) for any k>−1 | ... then T(n)=Θ(nccritlogk+1n)

(The bound is the splitting term, where the log is augmented by a single power.)

| If b=a2 and f(n)=Θ(n1/2/log1/2n), then T(n)=Θ(n1/2log1/2n).
2b  | When f(n)=Θ(nccritlogkn) for k=−1 | ... then T(n)=Θ(nccritlog⁡log⁡n)

(The bound is the splitting term, where the log reciprocal is replaced by an iterated log.)

| If b=a2 and f(n)=Θ(n1/2/log⁡n), then T(n)=Θ(n1/2log⁡log⁡n).
2c  | When f(n)=Θ(nccritlogkn) for any k<−1 | ... then T(n)=Θ(nccrit)

(The bound is the splitting term, where the log disappears.)

| If b=a2 and f(n)=Θ(n1/2/log2n), then T(n)=Θ(n1/2).

### Examples

#### Case 1 example

T(n)=8T(n2)+1000n2

As one can see from the formula above:

a=8,b=2,f(n)=1000n2, so
f(n)=O(nc), where c=2

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

logba=log28=3>c.

It follows from the first case of the master theorem that

T(n)=Θ(nlogba)=Θ(n3)

(This result is confirmed by the exact solution of the recurrence relation, which is T(n)=1001n3−1000n2, assuming T(1)=1).

#### Case 2 example

T(n)=2T(n2)+10n

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

a=2,b=2,c=1,f(n)=10n
f(n)=Θ(nclogkn) where c=1,k=0

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

logba=log22=1, and therefore, c and logba are equal

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

T(n)=Θ(nlogbalogk+1n)=Θ(n1log1n)=Θ(nlog⁡n)

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

(This result is confirmed by the exact solution of the recurrence relation, which is T(n)=n+10nlog2n, assuming T(1)=1).

#### Case 3 example

T(n)=2T(n2)+n2

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

a=2,b=2,f(n)=n2
f(n)=Ω(nc), where c=2

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

logba=log22=1, and therefore, yes, c>logba

The regularity condition also holds:

2(n24)≤kn2, choosing k=1/2

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

T(n)=Θ(f(n))=Θ(n2).

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 T(n)=2n2−n, assuming T(1)=1.)

## Inadmissible equations

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

* T(n)=2nT(n2)+nn

a is not a constant; the number of subproblems should be fixed
* T(n)=2T(n2)+nlog⁡n

non-polynomial difference between f(n) and nlogba (see below; extended version applies)
* T(n)=0.5T(n2)+n

a<1 cannot have less than one sub problem
* T(n)=64T(n8)−n2log⁡n

f(n), which is the combination time, is not positive
* T(n)=T(n2)+n(2−cos⁡n)

case 3 but regularity violation.

In the second inadmissible example above, the difference between f(n) and nlogba can be expressed with the ratio f(n)nlogba=n/log⁡nnlog22=nnlog⁡n=1log⁡n. It is clear that 1log⁡n<nϵ for any constant ϵ>0. Therefore, the difference is not polynomial and the basic form of the Master Theorem does not apply. The extended form (case 2b) does apply, giving the solution T(n)=Θ(nlog⁡log⁡n).

## Application to common algorithms

Algorithm  | Recurrence relationship  | Run time  | Comment

|
|
|

Binary search  | T(n)=T(n2)+O(1) | O(log⁡n) | Apply Master theorem case c=logba, where a=1,b=2,c=0,k=0[5]
Binary tree traversal  | T(n)=2T(n2)+O(1) | O(n) | Apply Master theorem case c<logba where a=2,b=2,c=0[5]
Optimal sorted matrix search  | T(n)=2T(n2)+O(log⁡n) | O(n) | Apply the Akra–Bazzi theorem for p=1 and g(u)=log⁡(u) to get Θ(2n−log⁡n)
Merge sort | T(n)=2T(n2)+O(n) | O(nlog⁡n) | Apply Master theorem case c=logba, where a=2,b=2,c=1,k=0

## See also

* Akra–Bazzi method
* Asymptotic complexity

## Notes

1. ↑ Bentley, Jon Louis; Haken, Dorothea; Saxe, James B. (September 1980), "A general method for solving divide-and-conquer recurrences", ACM SIGACT News 12 (3): 36–44, doi:10.1145/1008861.1008865, http://www.dtic.mil/get-tr-doc/pdf?AD=ADA064294
2. ↑ Duke University, "Big-Oh for Recursive Functions: Recurrence Relations", http://www.cs.duke.edu/~ola/ap/recurrence.html
3. ↑ Chee Yap, A real elementary approach to the master recurrence and generalizations, Proceedings of the 8th annual conference on Theory and applications of models of computation (TAMC'11), pages 14–26, 2011. Online copy.
4. ↑ Massachusetts Institute of Technology (MIT), "Master Theorem: Practice Problems and Solutions", https://people.csail.mit.edu/thies/6.046-web/master.pdf
5. ↑ 5.0 5.1 Dartmouth College, http://www.math.dartmouth.edu/archive/m19w03/public_html/Section5-2.pdf

## References

* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw–Hill, 2001. ISBN 0-262-03293-7. Sections 4.3 (The master method) and 4.4 (Proof of the master theorem), pp. 73-90.
* Michael T. Goodrich and Roberto Tamassia. Algorithm Design: Foundation, Analysis, and Internet Examples. Wiley, 2002. ISBN 0-471-38365-1. The master theorem (including the version of Case 2 included here, which is stronger than the one from CLRS) is on pp. 268–270.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Master theorem (analysis of algorithms). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Master_theorem_(analysis_of_algorithms)&oldid=185790"