Recurssion Tree
Recurssion Tree
Step-01:
Draw a recursion tree based on the given recurrence relation.
Step-02:
Determine-
Step-03:
Add cost of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation.
Following problems clearly illustrates how to apply these steps.
Problem-01:
Solve the following recurrence relation using recursion tree method-
T(n) = 2T(n/2) + n
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation.
The given recurrence relation shows-
The given recurrence relation shows-
The cost of dividing a problem of size n into its 2 sub-problems and then
combining its solution is n.
The cost of dividing a problem of size n/2 into its 2 sub-problems and then
combining its solution is n/2 and so on.
This is illustrated through following recursion tree where each node represents the cost
of the corresponding sub-problem-
Step-02:
Determine cost of each level-
Cost of level-0 = n
Cost of level-1 = n/2 + n/2 = n
Cost of level-2 = n/4 + n/4 + n/4 + n/4 = n and so on.
Step-03:
Determine total number of levels in the recursion tree-
Step-04:
Determine number of nodes in the last level-
Step-05:
Determine cost of last level-
Cost of last level = n x T(1) = θ(n)
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:
Solve the following recurrence relation using recursion tree method-
T(n) = T(n/5) + T(4n/5) + n
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation.
The given recurrence relation shows-
A problem of size n will get divided into 2 sub-problems- one of size n/5 and
another of size 4n/5.
Then, sub-problem of size n/5 will get divided into 2 sub-problems- one of size
n/52 and another of size 4n/52.
On the other side, sub-problem of size 4n/5 will get divided into 2 sub-problems-
one of size 4n/52 and another of size 42n/52 and so on.
At the bottom most layer, the size of sub-problems will reduce to 1.
This is illustrated through following recursion tree-
The given recurrence relation shows-
The cost of dividing a problem of size n into its 2 sub-problems and then
combining its solution is n.
The cost of dividing a problem of size n/5 into its 2 sub-problems and then
combining its solution is n/5.
The cost of dividing a problem of size 4n/5 into its 2 sub-problems and then
combining its solution is 4n/5 and so on.
This is illustrated through following recursion tree where each node represents the cost
of the corresponding sub-problem-
Step-02:
Determine cost of each level-
Cost of level-0 = n
Cost of level-1 = n/5 + 4n/5 = n
Cost of level-2 = n/52 + 4n/52 + 4n/52 + 42n/52 = n
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-
Step-04:
Determine number of nodes in the last level-
Step-05:
Determine cost of last level-
Cost of last level = 2log5/4n x T(1) = θ(2log5/4n) = θ(nlog5/42)
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)
Problem-03:
Solve the following recurrence relation using recursion tree method-
T(n) = 3T(n/4) + cn2
Solution-
Step-01:
Draw a recursion tree based on the given recurrence relation-
(Here, we have directly drawn a recursion tree representing the cost of sub problems)
Step-02:
Determine cost of each level-
Step-03:
Determine total number of levels in the recursion tree-
Size of sub-problem at level-0 = n/40
Size of sub-problem at level-1 = n/41
Size of sub-problem at level-2 = n/42
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/4i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
n/4x = 1
4x = n
Taking log on both sides, we get-
xlog4 = logn
x = log4n
∴ Total number of levels in the recursion tree = log4n + 1
Step-04:
Determine number of nodes in the last level-
Step-05:
Determine cost of last level-
Cost of last level = nlog43 x T(1) = θ(nlog43)
Step-06:
Add costs of all the levels of the recursion tree and simplify the expression so obtained
in terms of asymptotic notation-
= cn2 { 1 + (3/16) + (3/16)2 + ……… } + θ(nlog43)
Now, { 1 + (3/16) + (3/16)2 + ……… } forms an infinite Geometric progression.
On solving, we get-
= (16/13)cn2 { 1 – (3/16)log4n } + θ(nlog43)
= (16/13)cn2 – (16/13)cn2 (3/16)log4n + θ(nlog43)
= O(n2)
To gain better understanding about Recursion Tree,
Watch this Video Lecture
Next Article- DFS Algorithm
Get more notes and other study material of Design and Analysis of Algorithms.
Watch video lectures by visiting our YouTube channel LearnVidFun.
Summary
Article Name
Recursion Tree | Solving Recurrence Relations
Description
Like Master's theorem, recursion tree method is another method for solving recurrence relations. A
recursion tree is a tree where each node represents the cost of a certain recursive sub-problem. We
will follow the following steps for solving recurrence relations using recursion tree method.
Author
Akshay Singhal
Publisher Name
Gate Vidyalay
Publisher Logo
Master Theorem-
Master’s theorem solves recurrence relations of the form-
Here, a >= 1, b > 1, k >= 0 and p is a real number.
Case-01:
If a > bk, then T(n) = θ (nlogba)
Case-02:
If a = bk and
Case-03:
If a < bk and
Problem-01:
Solve the following recurrence relation using Master’s theorem-
T(n) = 3T(n/2) + n2
Solution-
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Then, we have-
a=3
b=2
k=2
p=0
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:
Solve the following recurrence relation using Master’s theorem-
T(n) = 2T(n/2) + nlogn
Solution-
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Then, we have-
a=2
b=2
k=1
p=1
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:
Solve the following recurrence relation using Master’s theorem-
T(n) = 2T(n/4) + n0.51
Solution-
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Then, we have-
a=2
b=4
k = 0.51
p=0
Now, a = 2 and bk = 40.51 = 2.0279.
Clearly, a < bk.
So, we follow case-03.
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n0.51log0n)
Thus,
T(n) = θ (n0.51)
Problem-04:
Solve the following recurrence relation using Master’s theorem-
T(n) = √2T(n/2) + logn
Solution-
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Then, we have-
a = √2
b=2
k=0
p=1
Now, a = √2 = 1.414 and bk = 20 = 1.
Clearly, a > bk.
So, we follow case-01.
So, we have-
T(n) = θ (nlogba)
T(n) = θ (nlog2√2)
T(n) = θ (n1/2)
Thus,
T(n) = θ (√n)
Problem-05:
Solve the following recurrence relation using Master’s theorem-
T(n) = 8T(n/4) – n2logn
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:
Solve the following recurrence relation using Master’s theorem-
T(n) = 3T(n/3) + n/2
Solution-
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-
T(n) = θ (loglog2n)
To gain better understanding about Master’s Theorem,
Watch this Video Lecture
Next Article- Recursion Tree
Get more notes and other study material of Design and Analysis of Algorithms.
Watch video lectures by visiting our YouTube channel LearnVidFun.
Summary
Article Name
Master Theorem | Master Theorem Examples
Description
Master Theorem is a popular method for solving the recurrence relations. Master Theorem Examples
are discussed. Master Theorem Cases are explained. Problems based on Master Theorem.
Author
Akshay Singhal
Publisher Name
Gate Vidyalay
Publisher Logo