Recursion Tree Method
Recursion Tree Method
● 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 3 :Add cost of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation.
Problem 1:
T(n) = 2T(n/2) + n
Solution
● 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 2:
● 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.
n / 2x = 1
2x = n
xlog2 = logn
x = log2n
Step 3:Add costs of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation-
= θ (nlog2n)
Problem 2:
Solution
● 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-
Step2:
***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
***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 3:Add costs of all the levels of the recursion tree and simplify the expression so obtained in
terms of asymptotic notation-
T(n)=nlog5/4n + θ(nlog5/42)
= θ(nlog5/4n)