Loading AI tools
Tool for analyzing divide-and-conquer algorithms From Wikipedia, the free encyclopedia
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.
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
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 . 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 , can be expressed by the recurrence relation
where 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.
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 denotes the total time for the algorithm on an input of size , and 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:
Here is the size of an input problem, is the number of subproblems in the recursion, and is the factor by which the subproblem size is reduced in each recursive call (). Crucially, and must not depend on . The theorem below also assumes that, as a base case for the recurrence, when is less than some bound , 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 relates to the critical exponent . (The table below uses standard big O notation).
Case | Description | Condition on in relation to , i.e. | 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 where
(upper-bounded by a lesser exponent polynomial) |
... then
(The splitting term does not appear; the recursive tree structure dominates.) |
If and , then . |
2 | Work to split/recombine a problem is comparable to subproblems. | When for a
(rangebound by the critical-exponent polynomial, times zero or more optional s) |
... then
(The bound is the splitting term, where the log is augmented by a single power.) |
If and , then .
If and , then . |
3 | Work to split/recombine a problem dominates subproblems.
i.e. the recursion tree is root-heavy. |
When where
(lower-bounded by a greater-exponent polynomial) |
... this doesn't necessarily yield anything. Furthermore, if
then the total is dominated by the splitting term : |
If and and the regularity condition holds, then . |
A useful extension of Case 2 handles all values of :[3]
Case | Condition on in relation to , i.e. | Master Theorem bound | Notational examples |
---|---|---|---|
2a | When for any | ... then
(The bound is the splitting term, where the log is augmented by a single power.) |
If and , then . |
2b | When for | ... then
(The bound is the splitting term, where the log reciprocal is replaced by an iterated log.) |
If and , then . |
2c | When for any | ... then
(The bound is the splitting term, where the log disappears.) |
If and , then . |
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
(This result is confirmed by the exact solution of the recurrence relation, which is , assuming ).
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 was in .
(This result is confirmed by the exact solution of the recurrence relation, which is , assuming ).
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 was in , that complies with the of the original formula.
(This result is confirmed by the exact solution of the recurrence relation, which is , assuming .)
The following equations cannot be solved using the master theorem:[4]
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 basic form of the Master Theorem does not apply. The extended form (case 2b) does apply, giving the solution .
Algorithm | Recurrence relationship | Run time | Comment |
---|---|---|---|
Binary search | Apply Master theorem case , where [5] | ||
Binary tree traversal | Apply Master theorem case where [5] | ||
Optimal sorted matrix search | Apply the Akra–Bazzi theorem for and to get | ||
Merge sort | Apply Master theorem case , where |
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.