CSC325 · 2078 · 5 marks
Explain the recursion tree method for solving the recurrence relation. Solve following recurrence relation using this method. T(n)=2T(n/2) +1 for n>1, T(n)=1 for n=1
Explain the recursion tree method for solving the recurrence relation. Solve following recurrence relation using this method. T(n)=2T(n/2) +1 for n>1, T(n)=1 for n=1 [5]
Recursion Tree Method for Solving Recurrence Relations
Explanation of the Recursion Tree Method
The recursion tree method is a technique for solving recurrence relations by expanding the recurrence into a tree. Each node represents the cost incurred at a subproblem, and the children of a node represent the recursive subproblems generated. The total cost is found by:
- Expanding the recurrence into a tree, level by level.
- Computing the cost contributed at each level.
- Finding the height (number of levels) of the tree by determining when the subproblem size reaches the base case.
- Summing the per-level costs across all levels.
- Expressing the total as a closed form / Big-Oh bound.
Given Data
- Recurrence: $T(n) = 2T(n/2) + 1$ for $n > 1$
- Base case: $T(1) = 1$
- Non-recursive (combine) cost per call: $1$
- Branching factor: $2$, subproblem size scaling: $n/2$
Step 1: Draw the Recursion Tree
Level 0: T(n) cost = 1
/ \
Level 1: T(n/2) T(n/2) cost = 2×1 = 2
/ \ / \
Level 2: T(n/4) T(n/4) T(n/4) T(n/4) cost = 4×1 = 4
...
Level k: 2^k nodes, each of size n/2^k cost = 2^k
Step 2: Cost at Each Level
| Level $i$ | Number of nodes | Cost per node | Level cost |
|---|---|---|---|
| 0 | $1$ | $1$ | $1$ |
| 1 | $2$ | $1$ | $2$ |
| 2 | $4$ | $1$ | $4$ |
| $i$ | $2^i$ | $1$ | $2^i$ |
Step 3: Height of the Tree
Recursion stops when subproblem size reaches the base case $n/2^k = 1$:
$$2^k = n \implies k = \log_2 n$$
So there are levels $0, 1, \dots, \log_2 n$, i.e. $\log_2 n + 1$ levels.
Note: at the last level, the $2^{\log_2 n} = n$ leaves each have $T(1) = 1$, contributing $n$, which is consistent with the internal-node accounting below.
Step 4: Sum the Total Cost
$$T(n) = \sum_{i=0}^{\log_2 n} 2^i$$
This is a geometric series with ratio $2$:
$$T(n) = \frac{2^{\log_2 n + 1} - 1}{2 - 1} = 2^{\log_2 n + 1} - 1 = 2\cdot 2^{\log_2 n} - 1$$
Since $2^{\log_2 n} = n$:
$$T(n) = 2n - 1$$
Step 5: Big-Oh Notation
$$\boxed{T(n) = 2n - 1 = O(n)}$$
Verification (Master Theorem)
For $T(n)=aT(n/b)+f(n)$ with $a=2,\ b=2,\ f(n)=1=\Theta(n^0)$: $$n^{\log_b a} = n^{\log_2 2} = n^1 = n$$ Since $f(n) = O(n^{1-\varepsilon})$, Case 1 applies, giving $T(n) = \Theta(n^{\log_b a}) = \Theta(n)$. This confirms $O(n)$.
Conclusion
Using the recursion tree method, $T(n) = 2T(n/2) + 1$ solves to $T(n) = 2n - 1 = O(n)$, matching the Master Theorem result.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.