![]() |
VOOZH | about |
The recursion tree method is used to analyze the time complexity of recursive algorithms by visually representing the recurrence as a tree. Each node of the tree represents the work done in a single recursive call, and each level represents one stage of the recursion. Below are the steps used to find time complexity using recursion tree method.
Note: If summing up all the levels becomes complex, we can find an upper bound by considering a perfectly full tree and / or an infinite geometrical series (the ratio is typically less than 1).
Let us take the below example code to understand the steps discussed above
The function makes two recursive calls, each on a subproblem of size n/2. After both recursive calls return, it performs a loop that prints "GFG" exactly n times, which is linear work for each function call. The recursion stops when n ≤ 1.
Each function call makes two recursive calls of size n/2 and performs linear work after the recursion.
Therefore, the recurrence relation is: T(n) = 2T(n/2) + O(n)
To find T(n), we analyze the recursion tree and sum the cost at each level.
Level 0: The root contributes a cost of cn.
Level 1: The subproblems are of sizes n/2 and n/2.
Their total cost is: c(n/2)+ c(n/2) = cnLevel 2: There are four subproblems of size n/4.
Continuing this way, the work at each level reamains cn.
The input size halves at each level, and the recursion stops when the size becomes constant.
Hence, the height of the recursion tree is logn.
Since each level contributes a cost of cn, the total work is:
T(n) = cn + cn + ....... + cn (log n levels)
T(n) = cn log n
Final Result: T(n) = O(n log n)
Let us see the below code as another recursive code to be analyzed using recursion tree method.
n/4n/2"GFG" for every pair (i, j).n ≤ 1.The recursive calls contribute T(n/4) and T(n/2). The nested loops perform Θ(n²) work. Therefore, the recurrence relation is: T(n) = T(n/4) + T(n/2) + O(n2)
In recursion trees where the problem does not split into equal-sized subproblems, the height of the recursion tree is not uniform across all branches. In the given example, one recursive call reduces the problem size to n/2, while the other reduces it to n/4. As a result, some branches of the recursion tree terminate earlier, while others continue deeper.
Because of this imbalance:
When we apply the geometric progression (GP) method to sum the cost across levels, we implicitly assume that all subproblems continue until the same depth. This assumption is not strictly true for recurrences with unequal subproblem sizes.
However, to simplify the analysis and still obtain a valid asymptotic bound, we take an upper-bound approach:
To find T(n), we analyze the recursion tree and sum the cost at each level.
- Level 0: The root contributes a cost of cn2.
- Level 1: The subproblems are of sizes n/2 and n/4.
Their total cost is: c(n/2)2 + c(n/4)2 = cn2 (1/4 + 1/16) = cn2 ⋅ 5/16- Level 2: The same pattern repeats, and the total cost becomes
cn2 (5/16)2 = cn2 ⋅ 25/256
Continuing this way, the total work forms the geometric series:
T(n) = cn2 (1 + 5/16 + (5/16)2 + ...... )
This is a geometric progression with common ratio 5/16 < 1 so it converges.
Summing the infinite series given an upper bound proportional to n2.
Final Result: T(n) = O(n2)