Data Structure and Algorithm
Complexity
Recursion Tree Method

Recursion Tree Method for solving Recurrences

The Recursion Tree Method is a visual and intuitive way to solve recurrence relations. It involves breaking down the recurrence into a tree structure, where each node represents the cost at a particular level of recursion. By summing up the costs at all levels, we can determine the overall time complexity.


Steps to Solve Recurrence Relations Using Recursion Tree Method

  1. Draw the Recursive Tree:
    • Represent the recurrence relation as a tree.
    • Each node corresponds to a recursive call, and its children represent smaller subproblems.
  2. Calculate Cost at Each Level:
    • Compute the work done (cost) at each level of the tree.
  3. Count the Total Number of Levels:
    • Determine the height of the tree, which corresponds to the number of levels.
  4. Sum Up the Costs:
    • Add the costs of all levels to find the total work done.

Example: Solving $T(n) = 2T(n/2) + cn$

Step 1: Draw the Recursive Tree

  • The root node represents $T(n)$.
  • Each level splits into 2 subproblems of size $n/2$.
  • The tree continues until the base case is reached (e.g., $T(1)$).

Step 2: Calculate Cost at Each Level

  • Level 0: $cn$ (root level).
  • Level 1: $2 \times c(n/2) = cn$.
  • Level 2: $4 \times c(n/4) = cn$.
  • ...
  • Level $k$: $2^k \times c(n/2^k) = cn$.

Step 3: Count the Total Number of Levels

  • The height of the tree is $\log_2 n$ (since the problem size halves at each level).

Step 4: Sum Up the Costs

  • Each level contributes $cn$ work.
  • Total work = $cn \times \log_2 n$.
  • Time Complexity: $\Theta(n \log n)$.

Conclusion

The Recursion Tree Method is a powerful tool for solving recurrence relations and analyzing the time complexity of recursive algorithms. By breaking down the problem into levels and summing up the costs, we can determine the overall efficiency of the algorithm. In the example $T(n) = 2T(n/2) + cn$, the time complexity is $\Theta(n \log n)$.