Recursion trees: Fibonacci and repeated work
Some recursive functions call themselves more than once, forming a tree of calls. Naive Fibonacci repeats the same subtrees again and again.
Before you start: Recursion: base case, recursive case, the call stack.
The idea
Each Fibonacci number is the sum of the two before it. So fib(n) needs two smaller answers.
The definition fib(n) = fib(n - 1) + fib(n - 2) turns straight into code that calls itself twice.
Intuition
A call that makes two calls branches. Drawn out, the calls form a tree, not a chain.
Each node is one call. Its children are the calls it makes. The leaves are base cases.
In fib(4), fib(2) shows up twice. The highlighted copy works out its whole subtree from scratch again.
Nothing remembers an answer between calls. Each copy redoes its whole subtree.
Complexity
Each extra n multiplies the calls by about 1.6. Time is exponential: O(2^n) as a simple bound.
calls(n) = 1 + calls(n - 1) + calls(n - 2), which grows like fib(n) itself, about 1.618^n.
Only one path from the root is on the stack at a time. Space is O(n), the depth of the tree.
When a call returns, its frame is gone. The stack never holds more than the longest path, n frames.
Pitfalls
The waste explodes: fib(40) makes over 331 million calls for one number.
Most of those calls recompute small values like fib(2) millions of times.
fib needs two base cases. With only n = 1, fib(2) calls fib(0), which calls fib(-1) forever.
Variations
Store each answer the first time. Later calls look it up, and fib(5) needs only 9 calls.
Counting ways to climb stairs, 1 or 2 at a time, grows the same tree shape.
Code
Pseudocode
function fib(n)
if n < 2: return n
return fib(n - 1) + fib(n - 2)
Python
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
JavaScript
function fib(n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
Java
static int fib(int n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
Practice problems (7)
Calls in fib(5) Warm-up
Naive fib(5) calls itself again and again. Before the tree grows, predict how many calls it makes in total, the first call included.
Fibonacci, the naive way Warm-up
Return the n-th Fibonacci number: fib(0) = 0, fib(1) = 1 and fib(n) = fib(n - 1) + fib(n - 2). Write it as a function that calls itself twice.
Order of calls Standard
fib(4) has just started. Put these calls in the order they start. Each call finishes its left call, fib(n - 1), before it starts its right call, fib(n - 2).
Repeated subproblems Standard
The tree of fib(5) is complete. Some calls recompute a fib(2) or fib(3) that an earlier call already worked out. Click every such repeated call.
Count the calls of fib Standard
Return how many calls the naive fib(n) makes in total, the first call included. fib(0) and fib(1) make 1 call each.
Solve it with a recurrence on the counts, not by running fib.
Climbing stairs Standard
You climb a staircase of n stairs, taking 1 or 2 stairs per move. Return the number of different move sequences that reach the top. A staircase of 0 stairs has 1 way: do nothing.
Count leaves of the call tree Challenge
A leaf of the call tree is a call that returns without calling again: fib(1) or fib(0). Before the tree of fib(6) grows, work out how many leaves it will have.
Related topics
Next on the path: Linear search