Master theorem (analysis of algorithms): Difference between revisions
Oops, intended deeper undo. 50kb of formatting junk and off-topic Python code (why??)( |
Ninjagecko (talk | contribs) reverting my changes |
||
Line 29: | Line 29: | ||
The Master theorem allows us to easily calculate the running time of such a recursive algorithm in [[Big O notation|Θ-notation]] without doing an expansion of the recursive relation above. |
The Master theorem allows us to easily calculate the running time of such a recursive algorithm in [[Big O notation|Θ-notation]] without doing an expansion of the recursive relation above. |
||
== Generic |
== Generic Form == |
||
The master theorem concerns recurrence relations of the form: |
|||
The Master Theorem sometimes gives us asymptotically tight bounds to recurrences of the following form, commonly found in many [[Divide_and_conquer_algorithm|divide-and-conquer algorithms]]: |
|||
⚫ | |||
:<math>work(n) = \#SubProbs * work(\frac{n}{b}) + splitwork(n) </math>, where <math>n</math> is the problem size, <math>\frac{n}{b}</math> is the subproblem size, and splitwork is the work required to split the problem into <math>a</math> problems of size <math>1/b</math> |
|||
In the application to the analysis of a recursive algorithm, the constants and function take on the following significance: |
|||
More formally: |
|||
⚫ | |||
*''n'' is the size of the problem. |
*''n'' is the size of the problem. |
||
Line 41: | Line 44: | ||
*''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. |
*''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. |
||
The Master Theorem says the following: Whenever we have a recurrence of this form, we can consider it as occurring in one of three regimes, based on how the work to split/recombine the problem relates to the special quantity <math>log_b a</math>, which we can call the critical exponent <math>c_{crit}</math>. (Below we use standard [[big O notation]].) |
|||
It is possible to determine an asymptotic tight bound in these three cases: |
|||
:<math>c_{crit} = log_b a = log(\#\text{subproblems}) log(\text{relative subproblem size})</math> |
|||
⚫ | |||
==== Generic form ==== |
|||
If <math>f(n)= O\left( n^{c} \right)</math> where <math>c < \log_b a</math> (using [[big O notation]]) |
|||
{| class="wikitable" |
|||
then: |
|||
|- |
|||
! width=10|Case |
|||
! width=200|Description |
|||
! width=300|Condition on <math>f(n)</math> in relation to <math>c_{crit}</math>, i.e. <math>log_b a</math> |
|||
! width=350|Master Theorem bound |
|||
|- |
|||
⚫ | |||
! 1 |
|||
⚫ | |||
| Work to split/recombine the problem is dwarfed by the work of further recursive subproblems. |
|||
i.e. the recursion tree is leaf-heavy |
|||
| This occurs when <math>f(n)</math> is asymptotically upper-bounded by a lesser polynomial in <math>O(n^{c_{crit}-\epsilon})</math>, less than the critical polynomial. That is, any polynomial with a strictly smaller exponent, i.e. <math>f(x) \in O(n^c)</math> such that <math>c < c_{crit}</math>. |
|||
For example, if <math>log_b a</math> were 1/2, then all <math>f(x) \in O(n^c)</math> where <math>c<1/2</math> would fall into this regime. |
|||
| The splitting work can be ignored, and the total is asymptotically dominated by the leaves. |
|||
⚫ | |||
|- |
|||
! 2 |
|||
| Work to split/recombine the problem is comparable to the work of further recursive subproblems. |
|||
| This occurs when <math>f(n)</math> is asymptotically rangebound by exactly the critical polynomial, times any number of optional logs <math>\Theta(n^{c_{crit}} log^{k} n)</math> for any <math>k>0</math>. |
|||
For example, if <math>f(x) \in \Theta(n^{c_{crit}})</math> (k is 0), or if <math>f(x) \in \Theta(n^{c_{crit}} log n)</math> (k is 1), etc. |
|||
| The <math>log(n)</math> term gets augmented by a single power. Whereas the splitting term previously was <math>f(x) \in \Theta(n^{c_{crit}} log^{k} n)</math>, <math>T(n)</math> now becomes: |
|||
⚫ | |||
|- |
|||
! 3 |
|||
| Work to split/recombine the problem dominates the work of further recursive subproblems. |
|||
i.e. the recursion tree is root-heavy. |
|||
| This occurs when <math>f(x)</math> is asymptotically lower-bounded by a greater polynomial in <math>\Omega\left( n^{c_{crit}+\epsilon} \right)</math>, greater than the critical polynomial. That is, any polynomial with a strictly larger exponent, i.e. <math>f(x) \in \Omega(n^c)</math> such that <math>c > c_{crit}</math>. |
|||
| This doesn't necessarily tell us anything. If we furthermore know that |
|||
⚫ | |||
then we know that the total is asymptotically dominated by the splitting term <math>f(x)</math>: |
|||
⚫ | |||
|} |
|||
⚫ | |||
⚫ | |||
:<math>T(n) = 8 T\left(\frac{n}{2}\right) + 1000n^2</math> |
:<math>T(n) = 8 T\left(\frac{n}{2}\right) + 1000n^2</math> |
||
Line 68: | Line 114: | ||
(indeed, the exact solution of the recurrence relation is <math>T(n) = 1001 n^3 - 1000 n^2</math>, assuming <math>T(1) = 1</math>). |
(indeed, the exact solution of the recurrence relation is <math>T(n) = 1001 n^3 - 1000 n^2</math>, assuming <math>T(1) = 1</math>). |
||
=== Case 2 === |
==== Case 2 Example ==== |
||
==== Generic form ==== |
|||
If it is true, for some constant ''k'' ≥ 0, that: |
|||
:<math>f(n)= \Theta\left( n^{c} \log^{k} n \right)</math> where <math>c = \log_b a</math> |
|||
then: |
|||
⚫ | |||
==== Example ==== |
|||
<math>T(n) = 2 T\left(\frac{n}{2}\right) + 10n</math> |
<math>T(n) = 2 T\left(\frac{n}{2}\right) + 10n</math> |
||
Line 96: | Line 132: | ||
(This result is confirmed by the exact solution of the recurrence relation, which is <math>T(n) = n + 10 n\log_2 n</math>, assuming <math>T(1) = 1</math>.) |
(This result is confirmed by the exact solution of the recurrence relation, which is <math>T(n) = n + 10 n\log_2 n</math>, assuming <math>T(1) = 1</math>.) |
||
=== Case 3 === |
==== Case 3 Example ==== |
||
==== Generic form ==== |
|||
If it is true that: |
|||
:<math>f(n)= \Omega\left( n^{c} \right)</math> where <math>c > \log_b a</math> |
|||
and if it is also true that: |
|||
⚫ | |||
then: |
|||
⚫ | |||
==== Example ==== |
|||
:<math>T(n) = 2 T\left(\frac{n}{2}\right) + n^2</math> |
:<math>T(n) = 2 T\left(\frac{n}{2}\right) + n^2</math> |
||
Revision as of 05:37, 12 June 2017
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 p( 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 p(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 . The total amount of work done by the entire tree 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 , can be expressed by the recurrence relation , where in the above procedure. This recursive relation can be successively substituted into itself and expanded to obtain an 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 sometimes gives us asymptotically tight bounds to recurrences of the following form, commonly found in many divide-and-conquer algorithms:
- , where is the problem size, is the subproblem size, and splitwork is the work required to split the problem into problems of size
More formally:
- where
- 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.
The Master Theorem says the following: Whenever we have a recurrence of this form, we can consider it as occurring in one of three regimes, based on how the work to split/recombine the problem relates to the special quantity , which we can call the critical exponent . (Below we use standard big O notation.)
Case | Description | Condition on in relation to , i.e. | Master Theorem bound |
---|---|---|---|
1 | Work to split/recombine the problem is dwarfed by the work of further recursive subproblems.
i.e. the recursion tree is leaf-heavy |
This occurs when is asymptotically upper-bounded by a lesser polynomial in , less than the critical polynomial. That is, any polynomial with a strictly smaller exponent, i.e. such that .
For example, if were 1/2, then all where would fall into this regime. |
The splitting work can be ignored, and the total is asymptotically dominated by the leaves.
|
2 | Work to split/recombine the problem is comparable to the work of further recursive subproblems. | This occurs when is asymptotically rangebound by exactly the critical polynomial, times any number of optional logs for any .
For example, if (k is 0), or if (k is 1), etc. |
The term gets augmented by a single power. Whereas the splitting term previously was , now becomes:
|
3 | Work to split/recombine the problem dominates the work of further recursive subproblems.
i.e. the recursion tree is root-heavy. |
This occurs when is asymptotically lower-bounded by a greater polynomial in , greater than the critical polynomial. That is, any polynomial with a strictly larger exponent, i.e. such that . | This doesn't necessarily tell us anything. If we furthermore know that
then we know that the total is asymptotically dominated by the splitting term : |
Examples
Case 1 Example
As one can see from the formula above:
- , so
- , where
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 , assuming ).
Case 2 Example
As we can see in the formula above the variables get the following values:
- where
Next, we see if we satisfy the case 2 condition:
- , and therefore,
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 , assuming .)
Case 3 Example
As we can see in the formula above the variables get the following values:
- , where
Next, we see if we satisfy the case 3 condition:
- , and therefore, yes,
The regularity condition also holds:
- , choosing
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 , assuming .)
Inadmissible equations
The following equations cannot be solved using the master theorem:[2]
-
- a is not a constant; the number of subproblems should be fixed
-
- non-polynomial difference between f(n) and (see below)
-
- cannot have less than one sub problem
-
- f(n), which is the combination time, is not positive
-
- case 3 but regularity violation.
In the second inadmissible example above, the difference between and can be expressed with the ratio . It is clear that for any constant . Therefore, the difference is not polynomial and the Master Theorem does not apply.
See also
Application to common algorithms
Algorithm | Recurrence Relationship | Run time | Comment |
---|---|---|---|
Binary search | Apply Master theorem case , where [3] | ||
Binary tree traversal | Apply Master theorem case where [3] | ||
Optimal Sorted Matrix Search | Apply the Akra–Bazzi theorem for and to get | ||
Merge Sort | Apply Master theorem case , where |
Notes
- ^ Duke University, "Big-Oh for Recursive Functions: Recurrence Relations", http://www.cs.duke.edu/~ola/ap/recurrence.html
- ^ Massachusetts Institute of Technology (MIT), "Master Theorem: Practice Problems and Solutions", http://www.csail.mit.edu/~thies/6.046-web/master.pdf
- ^ a b 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.