Many algorithms are naturally written so that a procedure calls itself on a smaller input. Their running time is described by a recurrence relation.
The Idea of Recursion
A recursive algorithm solves a problem by solving smaller instances of the same problem. It needs a base case that is solved directly (so the calls stop) and a recursive case that reduces the problem towards the base. The factorial is the standard example: n! = n × (n − 1)! with 0! = 1. Each call is placed on the call stack, and when the base is reached the results are combined on the way back. A recursive algorithm that lacks a base case, or does not move towards it, never terminates.
Recurrence Relations
The time T(n) of a recursive algorithm is written as an equation involving T of smaller sizes, called a recurrence. For factorial, T(n) = T(n − 1) + 1 with T(0) = 1, because one multiplication is done after the recursive call. For binary search, T(n) = T(n/2) + 1. For merge sort, T(n) = 2T(n/2) + n, since two halves are sorted and a linear merge follows. Writing the correct recurrence is the first and most important step.
Solving by Substitution and Expansion
In the substitution (iteration) method we expand the recurrence repeatedly until a pattern appears. For T(n) = T(n − 1) + 1: T(n) = T(n − 2) + 2 = ... = T(n − k) + k. Setting k = n gives T(n) = T(0) + n = n + 1, so T(n) = Θ(n). For T(n) = T(n/2) + 1: after k steps T(n) = T(n/2ᵏ) + k; with 2ᵏ = n, k = log₂ n, so T(n) = Θ(log n). The recursion tree method draws the calls as a tree, writes the work at each node, and adds up the work level by level; it is helpful for T(n) = 2T(n/2) + n, where every level costs n and there are log₂ n levels, giving Θ(n log n).
Fibonacci: A Warning
The naive recursion F(n) = F(n − 1) + F(n − 2) gives the recurrence T(n) = T(n − 1) + T(n − 2) + 1, which grows exponentially, about 1.618ⁿ, because the same sub-problems are recomputed again and again. For F(30) the naive method makes over 2.6 million calls. Storing results (dynamic programming, Unit III) reduces the time to Θ(n). Recursion is elegant but must be analysed carefully.
Definition: Recurrence relation. An equation that expresses the running time T(n) of a recursive algorithm in terms of its running time on smaller inputs, together with a base case.
Table 1.5: Common recurrences and solutions
| Recurrence | Example algorithm | Solution |
|---|---|---|
| T(n) = T(n−1) + 1 | Factorial, linear recursion | Θ(n) |
| T(n) = T(n/2) + 1 | Binary search | Θ(log n) |
| T(n) = 2T(n/2) + n | Merge sort | Θ(n log n) |
| T(n) = T(n−1) + n | Recursive selection sort | Θ(n²) |
| T(n) = T(n−1) + T(n−2) + 1 | Naive Fibonacci | Θ(1.618ⁿ) |
Example 1.5: Solving by expansion
Problem: Solve T(n) = T(n − 1) + n with T(1) = 1.
Solution:
- Expand: T(n) = T(n − 2) + (n − 1) + n = T(n − 3) + (n − 2) + (n − 1) + n.
- After n − 1 steps: T(n) = T(1) + 2 + 3 + ... + n = 1 + 2 + ... + n.
- The sum is n(n + 1)/2.
- Check n = 4: T(4) = 4 + 3 + 2 + 1 = 10 = 4·5/2.
Result: T(n) = n(n + 1)/2 = Θ(n²).
def calls_fib(n):
return 1 if n < 2 else calls_fib(n-1) + calls_fib(n-2) + 1
def T(n):
return 1 if n == 1 else T(n - 1) + n
print("T(4) =", T(4), " formula =", 4 * 5 // 2)
for n in (10, 20, 25):
print("naive fib calls for n =", n, ":", calls_fib(n))
Output
T(4) = 10 formula = 10
naive fib calls for n = 10 : 177
naive fib calls for n = 20 : 21891
naive fib calls for n = 25 : 242785
Common mistakes
- Forgetting the base case, so the recursion never ends.
- Writing T(n) = T(n − 1) for a call that also does extra work after returning.
- Ignoring the stack space used by recursion: depth n means O(n) space.
- Using naive recursion for Fibonacci and expecting linear time.
Key takeaways
- Recursion needs a base case and a reduction towards it.
- Time of a recursive algorithm is given by a recurrence relation.
- Expansion and recursion trees solve recurrences.
- T(n) = T(n/2) + 1 is Θ(log n); T(n) = 2T(n/2) + n is Θ(n log n).
Practice questions
- Write the recurrence for binary search.
- Why is naive recursive Fibonacci slow?
- What is a base case?
Answers to the practice questions
- T(n) = T(n/2) + 1, which solves to Θ(log n).
- It recomputes the same sub-problems, giving exponential time.
- The smallest instance, solved directly, which stops the recursion.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit I
