You are on page 1of 10

DESGIN AND ANALYSIS

ALGORITM
TOPIC: MASTER THEOREM
MASTER THEOREM

Master’s theorem is one of the many methods that are applied to


calculate time complexities of algorithms. In analysis, time
complexities are calculated to find out the best optimal logic of
an algorithm. Master’s theorem is applied on recurrence
relations.
But before we get deep into the master’s theorem, let us first
revise what recurrence relations are −
Recurrence relations are equations that define the sequence of
elements in which a term is a function of its preceding term. In
algorithm analysis, the recurrence relations are usually formed
when loops are present in an algorithm.
Master Theorem
Limitations
The master theorem cannot be used if:
•T(n) is not monotone. eg. T(n) = sin n
•f(n) is not a polynomial. eg. f(n) = 2n
•a is not a constant. eg. a = 2n
•a < 1
What is the general formula of master theorem?
The master theorem is a formula for solving recurrences of the form T(n) = aT(n/b)+f(n), where a ≥ 1
and b > 1 and f(n) is asymptotically positive. (Asymptotically positive means that the function is
positive for all sufficiently large n
When can master theorem not be used?
Master's Theorem can't be applied to every recurrence relation of the form T(n) = aT(n/b) + f(n) It can't be applied when a is less than 1, and a is
not a constant, i.e. a= x. It can't be applied when f(n ) is not a polynomial and T(n) is not monotonic
What are the 3 cases of the master theorem?
There are three cases. (a) If f(n) = O(nlogb a−ϵ), for some ϵ > 0, then T(n) = Θ(nlogba). (b) If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log n). (c) If f(n) = Ω(nlogb a+ϵ) for some ϵ > 0, and af(n/b) ≤ cf(n), for some c < 1 and for all n greater than some value n ,
Then T(n) = Θ(f(n)
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

(This result is confirmed by the exact solution of the


recurrence relation, which 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, c and   are equal
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  ).
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   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  .)
Example of Master Theorem
Example of Master Theorem
T(n) = 3T(n/2) + n2
Here,
a=3
n/b = n/2
f(n) = n2

logb a = log2 3 ≈ 1.58 < 2

ie. f(n) < nlogb a+ϵ , where, ϵ is a constant.

Case 3 implies here.

Thus, T(n) = f(n) = Θ(n2)

You might also like