0% found this document useful (0 votes)
5 views

Recursion Tree Method

The recursion tree method is a technique for solving recurrence relations by visualizing the costs of recursive sub-problems as nodes in a tree. For the recurrence relation T(n) = 2T(n/2) + n, the total cost is calculated by summing the costs at each level, leading to a final complexity of θ(n log2 n). The method involves determining the number of levels, nodes, and costs at each level of the tree.

Uploaded by

hibanahm12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Recursion Tree Method

The recursion tree method is a technique for solving recurrence relations by visualizing the costs of recursive sub-problems as nodes in a tree. For the recurrence relation T(n) = 2T(n/2) + n, the total cost is calculated by summing the costs at each level, leading to a final complexity of θ(n log2 n). The method involves determining the number of levels, nodes, and costs at each level of the tree.

Uploaded by

hibanahm12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

RECURSION TREE METHOD

● 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.
Steps to solve recurrence relation using recursion tree method:
1. Draw a recursive tree for given recurrence relation
2. Calculate the cost at each level and count the total no of levels in the
recursion tree.
3. Count the total number of nodes in the last level and calculate the cost of
the last level
4. Sum up the cost of all the levels in the recursive tree
Solve the following recurrence relation using recursion tree method-
T(n) = 2T(n/2) + n
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 of size n/2.
● Then, each sub-problem of size n/2 will get divided into 2 sub-problems of size n/4 and so on.
● At the bottom most layer, the size of sub-problems will reduce to 1.
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-


● Size of sub-problem at level-0 = n/20
● Size of sub-problem at level-1 = n/21
● Size of sub-problem at level-2 = n/22

Continuing in similar manner, we have-


Size of sub-problem at level-i = n/2i
Suppose at level-x (last level), size of sub-problem becomes 1. Then-
n / 2x = 1
2x = n
Taking log on both sides, we get-
xlog2 = logn
x = log2n

∴ Total number of levels in the recursion tree = log2n + 1


Step-04:
Determine number of nodes in the last level-
● Level-0 has 20 nodes i.e. 1 node
● Level-1 has 21 nodes i.e. 2 nodes
● Level-2 has 22 nodes i.e. 4 nodes

Continuing in similar manner, we have-


Level-log2n has 2log2n nodes i.e. n nodes
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)

You might also like