Algorithm Master Theorem Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    8 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of Algorithm Master Theorem

Algorithm Master Theorem: A Comprehensive Guide

Overview

  1. Definition and Formula

    • Master Theorem is a formula that applies to recurrences of the form: [ T(n) = aT\left(\frac{n}{b}\right) + f(n) ] where ( n ) is the size of the problem, ( a \geq 1 ) is the number of subproblems in the recursion, ( b > 1 ) is the factor by which the subproblem size is reduced, and ( f(n) ) is the cost of the work done outside the recursive calls (often the work to combine the solutions).
  2. Conditions for Application

    • The Master Theorem applies when the recurrence relation meets the following conditions:
      • The problem can be divided into ( a ) subproblems, each of size ( n/b ).
      • The cost of dividing the problem and combining the results is ( f(n) ).
      • The subproblems are solved independently.
  3. Three Cases

    • Case 1: If ( f(n) = O(n^c) ) where ( c < \log_b{a} ), then ( T(n) = \Theta(n^{\log_b{a}}) ).
      • Example: ( T(n) = 2T(n/2) + n ) Here ( a = 2 ), ( b = 2 ), and ( f(n) = n ). Since ( \log_2{2} = 1 ) and ( f(n) = n ) is ( O(n^c) ) with ( c = 1 = \log_b{a} ), this does not fit Case 1 but fits Case 2.
    • Case 2: If ( f(n) = \Theta(n^c \log^kn) ) where ( c = \log_b{a} ), then ( T(n) = \Theta(n^c \log^{k+1}n) ).
      • Example: ( T(n) = 4T(n/2) + n^2 ) Here ( a = 4 ), ( b = 2 ), and ( f(n) = n^2 ). Since ( \log_2{4} = 2 ) and ( f(n) = n^2 ) is ( \Theta(n^c) ) with ( c = 2 = \log_b{a} ), this fits Case 2.
    • Case 3: If ( f(n) = \Omega(n^c) ) where ( c > \log_b{a} ) and if ( af(n/b) \leq kf(n) ) for some ( k < 1 ) and sufficiently large ( n ), then ( T(n) = \Theta(f(n)) ).
      • Example: ( T(n) = 2T(n/4) + n^3 ) Here ( a = 2 ), ( b = 4 ), and ( f(n) = n^3 ). Since ( \log_4{2} = 0.5 ) and ( f(n) = n^3 ) is ( \Omega(n^c) ) with ( c = 3 > \log_b{a} ), this fits Case 3.

Step-by-Step Application

  1. Identify ( a ), ( b ), and ( f(n) )

    • From the recurrence relation, identify the constants ( a ), ( b ), and the function ( f(n) ).
  2. Compute ( \log_b{a} )

    • Calculate ( \log_b{a} ) which is the exponent of the polynomial term in the recursive part of the recurrence.
  3. Compare ( f(n) ) with ( n^{\log_b{a}} )

    • Determine the relationship between ( f(n) ) and ( n^{\log_b{a}} ) to decide which case of the Masters Theorem applies.
  4. Determine the Solution Based on the Case

    • Apply the appropriate case of the Master Theorem to find the time complexity ( T(n) ).

Important Considerations

  • Regularity Condition in Case 3

    • In Case 3, the function ( f(n) ) must also satisfy the regularity condition ( af(n/b) \leq kf(n) ) for some constant ( k < 1 ) and for sufficiently large ( n ). This ensures that the non-recursive part dominates asymptotically.
  • Cases Where the Theorem Does Not Apply

    • The Master Theorem cannot be applied when ( f(n) ) does not fit the form ( n^c \log^k{n} ) or when the base case of the recursion does not affect the asymptotic behavior.
  • Practical Implications

    • Understanding and applying the Master Theorem can greatly simplify the analysis of recursive algorithms, making it easier to predict their performance and optimize them.

Examples and Practice

  • Example 1: Analyze ( T(n) = 4T(n/2) + n^3 )

    • Here, ( a = 4 ), ( b = 2 ), and ( f(n) = n^3 ). Since ( \log_2{4} = 2 ) and ( f(n) = n^3 ) is ( \Omega(n^c) ) with ( c = 3 > 2 = \log_b{a} ), this fits Case 3. Therefore, ( T(n) = \Theta(n^3) ).
  • Example 2: Analyze ( T(n) = 2T(n/3) + n )

    • Here, ( a = 2 ), ( b = 3 ), and ( f(n) = n ). Since ( \log_3{2} \approx 0.63 ) and ( f(n) = n ) is ( \Theta(n^c) ) with ( c = 1 ) and ( 1 > \log_3{2} ), this fits Case 3. Therefore, ( T(n) = \Theta(n) ).

Conclusion

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement Algorithm Master Theorem

Master Theorem Overview

The Master Theorem applies to recurrence relations of the form: [ T(n) = aT\left(\frac{n}{b}\right) + f(n) ]

Where:

  • ( n ) is the size of the problem.
  • ( a \geq 1 ) is the number of subproblems in the recursion.
  • ( b > 1 ) is the factor by which the subproblem size is reduced.
  • ( f(n) ) is the cost of the work done outside the recursive calls, such as the cost of dividing the problem and combining the results.

The Master Theorem provides the asymptotic behavior of the solution as follows:

  1. If ( f(n) = \Theta(n^c) ) where ( c < \log_b a ), then ( T(n) = \Theta(n^{\log_b a}) ).
  2. If ( f(n) = \Theta(n^c \log^k n) ) where ( c = \log_b a ), then ( T(n) = \Theta(n^c \log^{k+1} n) ).
  3. If ( f(n) = \Theta(n^c) ) where ( c > \log_b a ), then ( T(n) = \Theta(f(n)) ).

Here, ( \Theta ) is used to denote asymptotic tight bounds.

Step-by-Step Example

Example 1:

Consider the recurrence relation: [ T(n) = 2T\left(\frac{n}{2}\right) + n ]

Step 1: Identify the values of (a), (b), and (f(n))
  • (a = 2) (number of subproblems)
  • (b = 2) (factor by which subproblem size is reduced)
  • (f(n) = n) (cost of non-recursive work)
Step 2: Compute ( \log_b a )

[ \log_b a = \log_2 2 = 1 ]

Step 3: Compare (f(n)) with (n^{ \log_b a })

Here, (f(n) = n) and (n^{ \log_b a } = n^1 = n).

Notice that: [ f(n) = \Theta(n) = \Theta(n^{\log_b a}) ]

Step 4: Apply the Master Theorem

Since (f(n) = \Theta(n^c \log^k n)) and (c = \log_b a) holds with (k=0), [ T(n) = \Theta(n^c \log^{k+1} n) = \Theta(n^1 \log^{0+1} n) = \Theta(n \log n) ]

Thus, the time complexity of this recurrence relation is ( \Theta(n \log n) ).

Example 2:

Consider the recurrence relation: [ T(n) = 4T\left(\frac{n}{2}\right) + n^2 ]

Step 1: Identify the values of (a), (b), and (f(n))
  • (a = 4) (number of subproblems)
  • (b = 2) (factor by which subproblem size is reduced)
  • (f(n) = n^2) (cost of non-recursive work)
Step 2: Compute ( \log_b a )

[ \log_b a = \log_2 4 = 2 ]

Step 3: Compare (f(n)) with (n^{ \log_b a })

Here, (f(n) = n^2) and (n^{ \log_b a } = n^2).

Notice that: [ f(n) = \Theta(n^{ \log_b a }) ]

Step 4: Apply the Master Theorem

Since (f(n) = \Theta(n^c \log^k n)) and (c = \log_b a) holds with (k=0), [ T(n) = \Theta(n^{ \log_b a } \log^{k+1} n) = \Theta(n^2 \log^{0+1} n) = \Theta(n^2 \log n) ]

Thus, the time complexity of this recurrence relation is ( \Theta(n^2 \log n) ).

Example 3:

Consider the recurrence relation: [ T(n) = 3T\left(\frac{n}{4}\right) + n \log n ]

Step 1: Identify the values of (a), (b), and (f(n))
  • (a = 3) (number of subproblems)
  • (b = 4) (factor by which subproblem size is reduced)
  • (f(n) = n \log n) (cost of non-recursive work)
Step 2: Compute ( \log_b a )

[ \log_b a = \log_4 3 ]

Using change of base formula: [ \log_4 3 = \frac{\log_2 3}{\log_2 4} = \frac{\log_2 3}{2} \approx 0.792 ]

Step 3: Compare (f(n)) with (n^{ \log_b a })

Here, (f(n) = n \log n) and (n^{ \log_b a } = n^{0.792}).

Notice that: [ f(n) = \Theta(n \log n) ] [ n^{ \log_b a } = \Theta(n^{0.792}) ]

And since (c = 0.792) and (0.792 < 1): [ c < \log_b a ]

Step 4: Apply the Master Theorem

Since (f(n) = \Theta(n^c)) and (c < \log_b a), [ T(n) = \Theta(n^{\log_b a}) = \Theta(n^{0.792}) ]

Thus, the time complexity of this recurrence relation is ( \Theta(n^{0.792}) ).

Conclusion

You May Like This Related .NET Topic

Login to post a comment.