Recursion tree and master thm
Recursion tree and master thm
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.
Step-01:
Step-02:
Determine-
● Cost of each level
● Total number of levels in the recursion tree
● Number of nodes in the last level
● Cost of the last level
Step-03:
Add cost of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation.
Problem-01:
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation.
This is illustrated through following recursion tree where each node represents the cost of
the corresponding sub-problem-
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation-
= n x log2n + θ (n)
= nlog2n + θ (n)
= θ (nlog2n)
Problem-02:
Solution-
Step-01:
This is illustrated through following recursion tree where each node represents the cost of
the corresponding sub-problem-
Step-02:
Step-03:
Determine total number of levels in the recursion tree. We will consider the rightmost sub
tree as it goes down to the deepest level-
● Size of sub-problem at level-0 = (4/5)0n
● Size of sub-problem at level-1 =(4/5)1n
● Size of sub-problem at level-2 =(4/5)2n
Step-04:
Step-05:
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation-
= nlog5/4n + θ(nlog5/42)
= θ(nlog5/4n)
Master Theorem-
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Case-01:
Case-02:
If a = bk and
● If p < -1, then T(n) = θ (nlogba)
● If p = -1, then T(n) = θ (nlogba.log2n)
● If p > -1, then T(n) = θ (nlogba.logp+1n)
Case-03:
If a < bk and
● If p < 0, then T(n) = O (nk)
● If p >= 0, then T(n) = θ (nklogpn)
Problem-01:
Now, a = 3 and bk = 22 = 4.
Clearly, a < bk.
So, we follow case-03.
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n2log0n)
Thus,
T(n) = θ (n2)
Problem-02:
Solution-
Now, a = 2 and bk = 21 = 2.
Clearly, a = bk.
So, we follow case-02.
Since p = 1, so we have-
T(n) = θ (nlogba.logp+1n)
T(n) = θ (nlog22.log1+1n)
Thus,
T(n) = θ (nlog2n)
Problem-03:
Solution-
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n0.51log0n)
Thus,
T(n) = θ (n0.51)
Problem-04:
Solution-
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-
● We write the given recurrence relation as T(n) = 3T(n/3) + n.
● This is because in the general form, we have θ for function f(n) which hides constants in
it.
● Now, we can easily apply Master’s theorem.
Now, a = 3 and bk = 31 = 3.
Clearly, a = bk.
So, we follow case-02.
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-
● We write a recurrence relation for the given code as T(n) = T(√n) + 1.
● Here 1 = Constant time taken for comparing and returning the value.
● We can not directly apply Master’s Theorem on this recurrence relation.
● This is because it does not correspond to the general form of Master’s theorem.
● However, we can modify and bring it in the general form to apply Master’s theorem.
Let-
n = 2m ……(1)
Then-
T(2m) = T(2m/2) + 1
So, we have-
S(m) = S(m/2) +1
Now, we can easily apply Master’s Theorem.
Since p = 0, so we have-
S(m) = θ (mlogba.logp+1m)
S(m) = θ (mlog21.log0+1m)
S(m) = θ (m0.log1m)
Thus,
S(m) = θ(logm) ……(2)
Now,
● From (1), we have n = 2m.
● So, logn = mlog2 which implies m = log2n.
T(n) = θ (loglog2n)