Recursion Tree _ Solving Recurrence Relations _ Gate Vidyalay
Recursion Tree _ Solving Recurrence Relations _ Gate Vidyalay
Recursion Tree-
Like Master’s Theorem, Recursion Tree is another method for solving the recurrence relations.
A recursion tree is a tree where each node represents the cost of a certain recursive sub-problem.
We sum up the values in each node to get the cost of the entire algorithm.
Step-01:
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.
···
Problem-01:
T(n) = 2T(n/2) + n
···
Solution-
Step-01:
···
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:
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:
n / 2x = 1
2x = n
xlog2 = logn
x = log2n
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:
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.
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
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-
···
(4/5)xn = 1
(4/5)x = 1/n
xlog(4/5) = log(1/n)
···
x = log5/4n
Step-04:
···
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:
Solution-
Step-01:
(Here, we have directly drawn a recursion tree representing the cost of sub problems)
Step-02:
Step-03:
n/4x = 1
4x = n
xlog4 = logn
x = log4n
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-
On solving, we get-
= O(n2)
To gain better understanding about Recursion Tree,
Get more notes and other study material of Design and Analysis of Algorithms.
Summary
Publisher Logo