The Recursion Tree Method for Solving Recurrences
The Recursion Tree Method for Solving Recurrences
Introduction
Many Divide-and-Conquer algorithms produce recurrence relations describing their running time.
For example,
- Merge Sort
- Quick Sort
- Binary Search
- Matrix Multiplication
- Strassen's Algorithm
To determine the running time, we must solve these recurrences.
Several techniques are available:
- Substitution Method
- Recursion Tree Method
- Master Method
The Recursion Tree Method is one of the most intuitive techniques because it represents the recursive calls as a tree.
Recursion tree is often used to generate intuition and make a good guess for the solution, which can later be proved rigorously using the substitution method. If the tree is constructed carefully, it can also serve as a direct proof.
What is a Recursion Tree?
A recursion tree is a tree representation of the recursive calls made by an algorithm.
Each node in the tree represents
- one recursive subproblem
- and the work performed at that recursive call.
The total running time is obtained by
- computing the cost of every level,
- adding the costs of all levels.
3. General Procedure
Suppose the recurrence is
The recursion tree is constructed as follows.
Step 1
Draw the root node.
The root represents the original problem of size
with cost
Step 2
Expand the root.
It generates
children.
Each child has problem size
Step 3
Continue recursively.
Every child again produces
children.
Step 4
Stop when the base case is reached.
Step 5
Compute
- cost at each node
- cost at each level
- height of the tree
Step 6
Add all level costs.
4. Example
Consider the recurrence
where
is a constant.
Step 1: Draw the Root
The root corresponds to the original problem.
n Cost = cn²
Step 2: Expand the Root
The recurrence contains
Therefore,
- three recursive calls
- each of size
n Cost=cn² / | \ n/4 n/4 n/4
Each child contributes
Step 3: Expand Again
Each
problem again becomes
three problems of size
n / | \ n/4 n/4 n/4 / | \ / | \ / | \
Now there are
nodes.
Each has cost
5. Cost at Each Level
Level 0
One node.
Cost
Level 1
Number of nodes
Cost of each node
Total cost
Level 2
Number of nodes
Cost of each
Total
General Level
At depth ,
Number of nodes
Problem size
Cost of each node
Total level cost
6. Height of the Tree
Each level reduces the problem size by
After
levels,
problem size becomes
Recursion stops when
Therefore,
Taking logarithm,
Hence,
the tree height is
7. Cost of the Leaves
Number of leaves
Using the logarithm identity
we obtain
Each leaf costs
Therefore,
Leaf cost
8. Total Cost
Now sum all levels.
Notice
Therefore,
the first terms form a geometric series.
Using the geometric series formula,
we get
Since
Therefore,
Because the first recursive call already contributes , we also have the matching lower bound, giving
Why Does the Root Dominate?
Observe the level costs.
| Level | Cost |
|---|---|
| 0 | |
| 1 | |
| 2 | |
| 3 |
Every level becomes
times the previous one.
Hence,
the costs decrease geometrically.
The root contributes the largest cost.
This is why
Verification Using Substitution
Next verifies the guess by induction.
Assume
Substitute into the recurrence:
To satisfy
it is sufficient to choose
Thus,
The recursion tree provided the correct guess, and substitution proved it.
General Guidelines for the Recursion Tree Method
When solving any recurrence:
- Write the recurrence.
- Draw the recursion tree.
- Find the number of nodes at each level.
- Find the size of each subproblem.
- Compute the cost of each node.
- Compute the total cost at each level.
- Find the height of the tree.
- Sum all level costs.
- Simplify the resulting expression.
Advantages
- Easy to visualize recursive calls.
- Helps understand Divide-and-Conquer algorithms.
- Gives intuition for the solution.
- Useful for deriving the Master Theorem.
Limitations
- Can become cumbersome for complex recurrences.
- Summing level costs may require ingenuity.
- Usually provides an educated guess, which should ideally be verified using the substitution method.
- Less convenient than the Master Theorem when the recurrence fits the Master Theorem directly.
Key Observations
- Every node represents one recursive subproblem.
- The cost at a node is the non-recursive work performed at that call.
- The number of nodes increases according to the number of recursive calls.
- The subproblem size decreases according to the recurrence.
- The height depends on how quickly the problem size shrinks.
- The total running time is obtained by adding the costs of all levels.
- The recursion tree method is primarily an intuition-building tool and often serves as a bridge to the substitution method for a rigorous proof.
Comments
Post a Comment