Professional Documents
Culture Documents
Master Theorem-
Case-01:
Case-02:
If a = bk and
Case-03:
If a < bk and
Problem-01:
T(n) = 3T(n/2) + n2
Solution-
Then, we have-
a=3
b=2
k=2
p=0
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n2log0n)
Thus,
T(n) = θ (n2)
Problem-02:
Solution-
a=2
b=2
k=1
p=1
Clearly, a = bk.
Since p = 1, so we have-
T(n) = θ (nlogba.logp+1n)
T(n) = θ (nlog22.log1+1n)
Thus,
T(n) = θ (nlog2n)
Problem-03:
Solution-
Then, we have-
a=2
b=4
k = 0.51
p=0
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n0.51log0n)
Thus,
T(n) = θ (n0.51)
Problem-04:
Solution-
Then, we have-
a = √2
b=2
k=0
p=1
So, we have-
T(n) = θ (nlogba)
T(n) = θ (nlog2√2)
T(n) = θ (n1/2)
Thus,
T(n) = θ (√n)
Problem-05:
Solution-
The given recurrence relation does not correspond to the general form of Master’s theorem.
So, it can not be solved using Master’s theorem.
Problem-06:
Solution-
Then, we have-
a=3
b=3
k=1
p=0
Clearly, a = bk.
Since p = 0, so we have-
T(n) = θ (nlogba.logp+1n)
T(n) = θ (nlog33.log0+1n)
T(n) = θ (n1.log1n)
Thus,
T(n) = θ (nlogn)
Problem-07:
Form a recurrence relation for the following code and solve it using Master’s theorem-
A(n)
{
if(n<=1)
return 1;
else
return A(√n);
}
Solution-
Let-
n = 2m ……(1)
Then-
T(2m) = T(2m/2) + 1
So, we have-
S(m) = S(m/2) +1
Then, we have-
a=1
b=2
k=0
p=0
Clearly, a = bk.
Since p = 0, so we have-
S(m) = θ (mlogba.logp+1m)
S(m) = θ (mlog21.log0+1m)
S(m) = θ (m0.log1m)
Thus,
Now,
S(m) = θ(loglog2n)
Or
T(n) = θ (loglog2n)
1) Substitution Method: We make a guess for the solution and then we use
mathematical induction to prove the guess is correct or incorrect.
For example consider the recurrence T(n) = 2T(n/2) + n
We need to prove that T(n) <= cnLogn. We can assume that it is true
for values smaller than n.
T(n) = 2T(n/2) + n
<= 2cn/2Log(n/2) + n
= cnLogn - cnLog2 + n
= cnLogn - cn + n
<= cnLogn
2) Recurrence Tree Method: In this method, we draw a recurrence tree and
calculate the time taken by every level of tree. Finally, we sum the work done at
all levels. To draw the recurrence tree, we start from the given recurrence and
keep drawing till we find a pattern among levels. The pattern is typically a
arithmetic or geometric series.
For example consider the recurrence relation
T(n) = T(n/4) + T(n/2) + cn 2
cn2
/ \
T(n/4) T(n/2)
If we further break down the expression T(n/4) and T(n/2),
we get following recursion tree.
cn 2
/ \
c(n2)/16 c(n2)/4
/ \ / \
T(n/16) T(n/8) T(n/8) T(n/4)
Breaking down further gives us following
cn 2
/ \
c(n2)/16 c(n 2)/4
/ \ / \
c(n2)/256 c(n2)/64 c(n2)/64 c(n2)/16
/ \ / \ / \ / \
Notes:
1) It is not necessary that a recurrence of the form T(n) = aT(n/b) + f(n) can be
solved using Master Theorem. The given three cases have some gaps between
them. For example, the recurrence T(n) = 2T(n/2) + n/Logn cannot be solved
using master method.
2) Case 2 can be extended for f(n) = Θ(n cLogkn)
If f(n) = Θ(ncLogkn) for some constant k >= 0 and c = Log ba, then T(n) =
Θ(ncLogk+1n)
.