How To Draw A Recursion Tree
The Recursion Tree Method is a way of solving recurrence relations. In this method, a recurrence relation is converted into recursive trees. Each node represents the cost incurred at various levels of recursion. To detect the total cost, costs of all levels are summed upwardly.
Steps to solve recurrence relation using recursion tree method:
- Draw a recursive tree for given recurrence relation
- Calculate the cost at each level and count the total no of levels in the recursion tree.
- Count the total number of nodes in the terminal level and summate the cost of the last level
- Sum up the cost of all the levels in the recursive tree
Allow us see how to solve these recurrence relations with the help of some examples:
Question ane: T(n) = 2T(n/2) + c
Solution:
- Step 1: Draw a recursive tree
- Footstep 2: Calculate the work done or cost at each level and count total no of levels in recursion tree
Count the full number of levels –
Choose the longest path from root node to leafage node
due north/20 -→ n/21 -→ north/2ii -→ ……… -→ north/2k
Size of problem at last level = n/2k
At last level size of problem becomes 1
n/2yard = 1
iithou = north
k = log2(northward)
Total no of levels in recursive tree = k +1 = logii(n) + one
- Step iii: Count total number of nodes in the last level and summate cost of last level
No. of nodes at level 0 = 20 = 1
No. of nodes at level 1 = 2i = 2
………………………………………………………
No. of nodes at level logii(n) = 2log 2 (due north) = nlog 2 (2) = due north
Cost of sub problems at level logtwo(n) (terminal level) = nxT(1) = nx1 = n
- Step 4: Sum upward the toll all the levels in recursive tree
T(n) = c + 2c + 4c + —- + (no. of levels-1) times + concluding level cost
= c + 2c + 4c + —- + log2(northward) times + Θ(northward)
= c(ane + 2 + 4 + —- + log2(n) times) + Θ(n)
1 + two + 4 + —– + log2(n) times –> ii0 + twoi + two2 + —– + log2(n) times –> Geometric Progression(Yard.P.)
= c(n) + Θ(due north)
Thus, T(northward) = Θ(n)
Question 2: T(n) = T(n/x) + T(9n/ten) + n
Solution:
- Step one: Draw a recursive tree
- Step 2: Calculate the work washed or price at each level and count total no of levels in recursion tree
Count the total number of levels –
Choose the longest path from root node to leaf node
(9/ten)0n –> (9/x)1n –> (nine/x)2n –> ……… –> (nine/10)1000n
Size of trouble at last level = (9/10)mn
At last level size of problem becomes 1
(9/x)kn = 1
(9/10)1000 = 1/due north
k = log10/9(n)
Total no of levels in recursive tree = k +one = log10/nine(n) + one
- Pace 3: Count full number of nodes in the last level and calculate price of final level
No. of nodes at level 0 = ii0 = 1
No. of nodes at level one = ii1 = 2
………………………………………………………
No. of nodes at level log10/9(n) = iilog 10/9 (n) = nlog 10/ix (2)
Cost of sub problems at level log2(n) (last level) = nlog 10/9 (2) x T(1) = northwardlog 10/9 (2) 10 1 = nlog 10/9 (2)
- Step iv: Sum upwards the toll all the levels in recursive tree
T(n) = n + n + due north + —- + (no. of levels – 1) times + concluding level toll
= n + n + n + —- + logx/9(n) times + Θ(northlog ten/9 (2))
= nlog10/9(due north) + Θ(nlog x/ix (2))
Thus, T(n) = Θ(nlog ten/9 (n))
Source: https://www.geeksforgeeks.org/how-to-solve-time-complexity-recurrence-relations-using-recursion-tree-method/
Posted by: lenoxnembee.blogspot.com
0 Response to "How To Draw A Recursion Tree"
Post a Comment