Sub topic 7
Multiple Recursion and Repeated Work
This chapter assumes you can read a recursive function, find its base case, and follow how calls stack up. It builds on the earlier chapters on the call stack and on recursion over lists and trees.
When a Function Calls Itself More Than Once
The recursive functions so far made one recursive call per step. Factorial calls itself once, so its calls form a straight chain. Multiple recursion, also called tree recursion, is when one call makes two or more recursive calls. The calls then branch out into a tree.
Fibonacci is the standard example. Each number is the sum of the two numbers before it:
def fib_naive(n):
if n < 2:
return n
return fib_naive(n - 1) + fib_naive(n - 2)
print(fib_naive(10)) # 55This code is correct and it matches the mathematical definition closely. The problem is how much work it does.
Seeing the Repeated Work
Draw the calls for fib_naive(4) and you see the same subproblems computed again and again.
These are overlapping subproblems: different branches of the tree ask for the same answer, and each branch recomputes it from scratch. Count the calls to see how bad this gets:
from collections import Counter
calls = Counter()
def fib_counted(n):
calls[n] += 1
if n < 2:
return n
return fib_counted(n - 1) + fib_counted(n - 2)
print(fib_counted(20)) # 6765
print(sum(calls.values())) # 21891 total calls
print(calls[1], calls[0]) # 6765 4181
print(calls[18]) # 2There are only 21 distinct subproblems (fib(0) through fib(20)), but the function makes 21,891 calls. fib(1) alone runs 6,765 times. The number of calls grows roughly by a factor of 1.6 each time n goes up by one, which is exponential growth.
Branching Without Overlap
Multiple recursion does not always waste work. What matters is whether the subproblems overlap.
| Algorithm | Calls per step | Subproblems overlap? | Time |
|---|---|---|---|
| Naive Fibonacci | 2 | Yes | Exponential, about O(1.6^n) |
| Binary tree traversal | 2 | No, each subtree is distinct | O(n) in nodes |
| Merge sort | 2 | No, halves are disjoint | O(n log n) |
A tree traversal visits the left and right subtrees, and those subtrees share no nodes. Merge sort splits a list into halves that never overlap. Both branch, and both are efficient. Naive Fibonacci is slow because its branches keep asking for the same values.
Spotting the Problem in Your Own Code
Ask two questions about any function that recurses more than once:
- Can two different branches reach the same argument values?
- Does the result depend only on those arguments?
If both answers are yes, you have overlapping subproblems. You can fix this by storing each answer the first time it is computed, or by building the answers up from the smallest case with a loop. Both techniques are covered in the chapter on memoization and bottom-up iteration.
Exercise
The number of calls fibnaive(n) makes is 2 * fib(n + 1) - 1. How many calls does fibnaive(6) make, and how many distinct subproblems are there?
Show answer
fib(7) is 13, so the function makes 2 * 13 - 1 = 25 calls. There are only 7 distinct subproblems, fib(0) through fib(6).
Exercise
A function counts paths through a grid by returning paths(r - 1, c) + paths(r, c - 1), stopping when r or c is 0. A second function counts the nodes in a binary tree with 1 + count(left) + count(right). Which one has overlapping subproblems, and why?
Show answer
The grid function overlaps. For example, paths(r - 1, c - 1) is reached both through paths(r - 1, c) and through paths(r, c - 1). The tree function does not overlap, because the left and right subtrees contain different nodes and each node is visited exactly once.