Algorithms · Dynamic Programming Basics
Fibonacci Series Recursion and Exponential Time
Overview
The Fibonacci series is the classic example that shows why dynamic programming matters.
Its naive recursive implementation recomputes the same subproblems many times, making the running time exponential even though each individual call costs only . Storing computed values in a table avoids this waste.
Key concepts
- Fibonacci series: defined recursively as if , if , and otherwise. The series begins with 0 and 1 as initial values; each subsequent term is the sum of the previous two terms.
- Order of recursive calls: write down the recursion tree, then traverse it top-down, left-to-right. That is the order in which the recursive calls are made (the same principle as in merge sort and quick sort).
- Recomputation: the same subproblems are computed many times over. For example, F(2) is computed three times while finding F(5).
- Exponential blow-up: if all calls are made, the number of calls is with . Each call costs , so the total time complexity is , which is — exponential.
- The fix (motivating dynamic programming): whenever a value is computed, save it in a table, since it is known the value will be of use again later.
Before calling the function, check whether the value is already computed; if it is stored in the table, use the stored value instead of recomputing.
How it works
The naive recursive program for Fibonacci:
Fibonacci(n):
if n == 0:
return 0
else if n == 1:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
Tracing the calls for F(5):
- F(5) calls F(4) and F(3).
- F(4) calls F(3) and F(2).
- F(3) calls F(2) and F(1).
- F(2) calls F(1) and F(0).
- When , it simply returns 1.
- When , it returns 0.
- F(2) adds these two results and returns the value upward.
- F(3) then calls F(1) again — but F(1) was already computed earlier. It is recomputed anyway.
- F(4) then calls F(2) — but F(2) was already computed earlier. It is recomputed again, which is completely a waste.
Formulas
- for ; , — is the index of the Fibonacci term, is the value of the -th term.
- Time per function call = .
- Total time complexity of naive recursion (exponential; more tightly with ), since the number of calls is .
Worked examples
Building the Fibonacci series term by term:
| n | F(n) | How computed |
|---|---|---|
| 0 | 0 | base case |
| 1 | 1 | base case |
| 2 | 1 | F(1) + F(0) = 1 + 0 |
| 3 | 2 | F(2) + F(1) = 1 + 1 |
| 4 | 3 | F(3) + F(2) = 2 + 1 |
Recursion tree for F(5):
- F(5) → F(4), F(3)
- F(4) → F(3), F(2)
- F(3) → F(2), F(1)
- F(2) → F(1), F(0)
- F(3) (second occurrence) → F(2), F(1)
- F(2) (second occurrence) → F(1), F(0)
- F(2) (third occurrence) → F(1), F(0)
Execution order (top-down, left-to-right):
- F(5) calls F(4); F(4) calls F(3); F(3) calls F(2); F(2) calls F(1) → returns 1.
- F(2) calls F(0) → returns 0.
- F(2) adds 1 + 0 and returns the result upward.
- F(3) calls F(1) again — already computed before, recomputed anyway → returns 1.
- F(3) adds the two results and returns the value upward.
- F(4) calls F(2) again — already computed before, recomputed again (completely a waste).
- The same repeated recomputation continues for the remaining branches.
The repeated computation of F(1), F(2), F(3) at multiple places in the tree is the source of the exponential blow-up: the number of calls is with , each costing , so total time is — exponential.
Revision summary
- Fibonacci recurrence: , with and .
- The naive recursive program recomputes the same subproblems many times (e.g., F(2) is computed three times while finding F(5)).
- Recursive call order = traverse the recursion tree top-down, left-to-right.
- Each function call costs , but the number of calls is with , so total time is (tightly ) — exponential.
- The fix that motivates dynamic programming: store each computed value in a table; before calling the function, check the table and reuse the stored value if it is already computed.
Previous-year exam insights
- The exact number of calls made by the naive recursive Fibonacci is 2F(n+1) − 1, which is Θ(φ^n) where φ = (1+√5)/2 ≈ 1.618 (the golden ratio);
the recursion tree has F(n+1) leaves. - The space complexity of naive recursive Fibonacci is due to the maximum recursion-stack depth, not .
- Memoized (top-down) and tabulated (bottom-up) Fibonacci both run in time; bottom-up uses space (or with two variables), while memoization uses space for the table plus stack.
- The closed form (Binet's formula) is F(n) = (φ^n − ψ^n)/√5 with φ = (1+√5)/2 and ψ = (1−√5)/2, so F(n) grows as Θ(φ^n).
- Dynamic programming requires optimal substructure and overlapping subproblems; Fibonacci has overlapping subproblems (repeated identical subproblems), which is exactly what memoization/tabulation eliminates.
- The recursion tree for F(n) has F(n+1) leaves and F(n+1) − 1 internal nodes; the total number of nodes (calls) is 2F(n+1) − 1.