Puneet Varma (Editor)

Master theorem

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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.

Contents

Introduction

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

procedure T( 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 T(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 f ( n ) . The total amount of work done by the entire tree is the sum of the work performed by all the nodes in the tree.

Algorithms such as above can be represented as a recurrence relation T ( n ) = a T ( n b ) + f ( n ) , where f ( n ) = d ( n ) + c ( n ) in the above procedure. This recursive relation can be successively substituted into itself and expanded to obtain expression for total amount of work done.

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 concerns recurrence relations of the form:

T ( n ) = a T ( n b ) + f ( n ) where a 1 b > 1

In the application to the analysis of a recursive algorithm, the constants and function take on the following significance:

  • 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.
  • It is possible to determine an asymptotic tight bound in these three cases:

    Generic form

    If f ( n ) = O ( n c ) where c < log b a (using big O notation)

    then:

    T ( n ) = Θ ( n log b a )

    Example

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

    As one can see from the formula above:

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

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

    log b a = log 2 8 = 3 > c .

    It follows from the first case of the master theorem that

    T ( n ) = Θ ( n log b a ) = Θ ( n 3 )

    (indeed, the exact solution of the recurrence relation is T ( n ) = 1001 n 3 1000 n 2 , assuming T ( 1 ) = 1 ).

    Generic form

    If it is true, for some constant k ≥ 0, that:

    f ( n ) = Θ ( n c log k n ) where c = log b a

    then:

    T ( n ) = Θ ( n c log k + 1 n )

    Example

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

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

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

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

    log b a = log 2 2 = 1 , and therefore, c = log b a

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

    T ( n ) = Θ ( n log b a log k + 1 n ) = Θ ( n 1 log 1 n ) = Θ ( n log n )

    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 T ( n ) = n + 10 n log 2 n , assuming T ( 1 ) = 1 .)

    Generic form

    If it is true that:

    f ( n ) = Ω ( n c ) where c > log b a

    and if it is also true that:

    a f ( n b ) k f ( n ) for some constant k < 1 and sufficiently large n (often called the regularity condition)

    then:

    T ( n ) = Θ ( f ( n ) )

    Example

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

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

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

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

    log b a = log 2 2 = 1 , and therefore, yes, c > log b a

    The regularity condition also holds:

    2 ( n 2 4 ) k n 2 , choosing k = 1 / 2

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

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

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

    Inadmissible equations

    The following equations cannot be solved using the master theorem:

  • T ( n ) = 2 n T ( n 2 ) + n n a is not a constant; the number of subproblems should be fixed
  • T ( n ) = 2 T ( n 2 ) + n log n non-polynomial difference between f(n) and n log b a (see below)
  • T ( n ) = 0.5 T ( n 2 ) + n a < 1 cannot have less than one sub problem
  • T ( n ) = 64 T ( n 8 ) n 2 log n f(n), which is the combination time, is not positive
  • T ( n ) = T ( n 2 ) + n ( 2 cos n ) case 3 but regularity violation.
  • In the second inadmissible example above, the difference between f ( n ) and n log b a can be expressed with the ratio f ( n ) n log b a = n / log n n log 2 2 = n n log n = 1 log n . It is clear that 1 log n < n ϵ for any constant ϵ > 0 . Therefore, the difference is not polynomial and the Master Theorem does not apply.

    References

    Master theorem Wikipedia