# Master theorem (analysis of algorithms)

Master theorem (analysis of algorithms) For other theorems called Master theorem, see Master theorem.

In the analysis of algorithms, the master theorem for divide-and-conquer recurrences provides an asymptotic analysis (using Big O notation) for recurrence relations of types that occur in the analysis of many divide and conquer algorithms. The approach was first presented by Jon Bentley, Dorothea 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 with the use of 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 8 External links 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 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 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 {displaystyle T(n)} , can be expressed by the recurrence relation {displaystyle T(n)=a;Tleft({frac {n}{b}}right)+f(n),} where {displaystyle 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 {displaystyle T(n)} denotes the total time for the algorithm on an input of size {displaystyle n} , and {displaystyle 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: {displaystyle T(n)=a;T!left({frac {n}{b}}right)+f(n)} Here {displaystyle n} is the size of an input problem, {displaystyle a} is the number of subproblems in the recursion, and {displaystyle b} is the factor by which the subproblem size is reduced in each recursive call (b>1). The theorem below also assumes that, as a base case for the recurrence, {displaystyle T(n)=Theta (1)} when {displaystyle n} is less than some bound {displaystyle kappa >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 {displaystyle f(n)} relates to the critical exponent {displaystyle c_{operatorname {crit} }=log _{b}a} . (The table below uses standard big O notation.) {displaystyle c_{operatorname {crit} }=log _{b}a=log(#{text{subproblems}})/log({text{relative subproblem size}})} Case Description Condition on {displaystyle f(n)} in relation to {displaystyle c_{operatorname {crit} }} , i.e. {displaystyle log _{b}a} 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 {displaystyle f(n)=O(n^{c})} where {displaystyle cc_{operatorname {crit} }} (lower-bounded by a greater-exponent polynomial) ... this doesn't necessarily yield anything. Furthermore, if {displaystyle afleft({frac {n}{b}}right)leq kf(n)} for some constant {displaystyle k<1} and sufficiently large {displaystyle n} (often called the regularity condition) then the total is dominated by the splitting term {displaystyle f(n)} : {displaystyle Tleft(nright)=Theta left(f(n)right)} If {displaystyle b=a^{2}} and {displaystyle f(n)=Omega (n^{1/2+epsilon })} and the regularity condition holds, then {displaystyle T(n)=Theta (f(n))} . A useful extension of Case 2 handles all values of {displaystyle k} :[3] Case Condition on {displaystyle f(n)} in relation to {displaystyle c_{operatorname {crit} }} , i.e. {displaystyle log _{b}a} Master Theorem bound Notational examples 2a When {displaystyle f(n)=Theta (n^{c_{operatorname {crit} }}log ^{k}n)} for any {displaystyle k>-1} ... then {displaystyle T(n)=Theta left(n^{c_{operatorname {crit} }}log ^{k+1}nright)} (The bound is the splitting term, where the log is augmented by a single power.) If {displaystyle b=a^{2}} and {displaystyle f(n)=Theta (n^{1/2}/log ^{1/2}n)} , then {displaystyle T(n)=Theta (n^{1/2}log ^{1/2}n)} . 2b When {displaystyle f(n)=Theta (n^{c_{operatorname {crit} }}log ^{k}n)} for {displaystyle k=-1} ... then {displaystyle T(n)=Theta left(n^{c_{operatorname {crit} }}log log nright)} (The bound is the splitting term, where the log reciprocal is replaced by an iterated log.) If {displaystyle b=a^{2}} and {displaystyle f(n)=Theta (n^{1/2}/log n)} , then {displaystyle T(n)=Theta (n^{1/2}log log n)} . 2c When {displaystyle f(n)=Theta (n^{c_{operatorname {crit} }}log ^{k}n)} for any {displaystyle k<-1} ... then {displaystyle T(n)=Theta left(n^{c_{operatorname {crit} }}right)} (The bound is the splitting term, where the log disappears.) If {displaystyle b=a^{2}} and {displaystyle f(n)=Theta (n^{1/2}/log ^{2}n)} , then {displaystyle T(n)=Theta (n^{1/2})} . Examples Case 1 example {displaystyle T(n)=8Tleft({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)=Oleft(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)=Theta left(n^{log _{b}a}right)=Theta left(n^{3}right)} (This result is confirmed by the exact solution of the recurrence relation, which is {displaystyle T(n)=1001n^{3}-1000n^{2}} , assuming {displaystyle T(1)=1} ).

Case 2 example {displaystyle T(n)=2Tleft({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}nright)} 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, c and {displaystyle log_{b}a} are equal So it follows from the second case of the master theorem: {displaystyle T(n)=Theta left(n^{log _{b}a}log ^{k+1}nright)=Theta left(n^{1}log ^{1}nright)=Theta left(nlog nright)} 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+10nlog _{2}n} , assuming {displaystyle T(1)=1} .) Case 3 example {displaystyle T(n)=2Tleft({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 2left({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 Tleft(nright)=Theta left(f(n)right)=Theta left(n^{2}right).} Thus the given recurrence relation {displaystyle T(n)} was in {displaystyle Theta (n^{2})} , that complies with the {displaystyle 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} .) Inadmissible equations The following equations cannot be solved using the master theorem:[4] {displaystyle T(n)=2^{n}Tleft({frac {n}{2}}right)+n^{n}} a is not a constant; the number of subproblems should be fixed {displaystyle T(n)=2Tleft({frac {n}{2}}right)+{frac {n}{log n}}} non-polynomial difference between {displaystyle f(n)} and {displaystyle n^{log _{b}a}} (see below; extended version applies) {displaystyle T(n)=0.5Tleft({frac {n}{2}}right)+n} {displaystyle a<1} cannot have less than one sub problem {displaystyle T(n)=64Tleft({frac {n}{8}}right)-n^{2}log n} {displaystyle f(n)} , which is the combination time, is not positive {displaystyle T(n)=Tleft({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}{nlog n}}={frac {1}{log n}}} . It is clear that {displaystyle {frac {1}{log n}}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 {displaystyle T(n)=Theta (nlog log n)} .

Application to common algorithms Algorithm Recurrence relationship Run time Comment Binary search {displaystyle T(n)=Tleft({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} [5] Binary tree traversal {displaystyle T(n)=2Tleft({frac {n}{2}}right)+O(1)} {displaystyle O(n)} Apply Master theorem case {displaystyle c

Si quieres conocer otros artículos parecidos a Master theorem (analysis of algorithms) puedes visitar la categoría Asymptotic analysis.

Subir

Utilizamos cookies propias y de terceros para mejorar la experiencia de usuario Más información