Dynamic programming · Algorithm 13 of 15
Fibonacci by Dynamic Programming
Replace repeated recursive work with stored subproblem answers. Citation needed
The whole, before the parts
This path uses Fibonacci numbers to show how saving earlier answers avoids repeating work. The sequence gives a small example of a broader method: build a result from smaller results that are already known. Citation needed
Terminology and core concepts
Dynamic programming solves overlapping subproblems once and reuses their answers. Fibonacci numbers start with F(0) = 0 and F(1) = 1, then F(n) = F(n - 1) + F(n - 2). Naive recursion recalculates the same values many times. Bottom-up tabulation fills a table from the base cases toward the requested index, so each addition uses answers already available. The table is deliberately kept here to make the subproblems visible. Citation needed
Learning path
Trace an example
index 0 1 2 3 4 5 6 stored value 0 1 1 2 3 5 8 F(4) reads F(3)=2 and F(2)=1, then stores 3 F(5) reads F(4)=3 and F(3)=2, then stores 5 F(6) reads F(5)=5 and F(4)=3, then stores 8
Implement it in Python
Read the loop alongside the trace, then run this self-contained example with Python 3. The assertions show expected results. Inputs use ordinary Python lists and dictionaries; comparisons and numeric operations are assumed to behave normally. Citation needed
def fibonacci(n):
if not isinstance(n, int) or n < 0:
raise ValueError('n must be a non-negative integer')
if n < 2:
return n
table = [0] * (n + 1)
table[1] = 1
for i in range(2, n + 1):
table[i] = table[i - 1] + table[i - 2]
return table[n]
assert fibonacci(6) == 8
assert fibonacci(0) == 0
assert fibonacci(1) == 1Citation needed
Analyze the cost
The table uses O(n) additions and O(n) stored numbers. Under the constant-cost arithmetic model this is O(n) time and space. Python integers grow with n: F(n) has Θ(n) bits, so this implementation takes O(n²) bit operations and O(n²) bits of table storage. Keeping just the last two values reduces the number of stored integers to O(1). Citation needed
A mistake to watch for
A cache is useful only if each stored state has a precise meaning. Here the index fully identifies the subproblem. Do not confuse linear additions with constant-sized integer arithmetic for very large n. Citation needed
Practice and recall
-
What two values are sufficient to compute F(7) after F(6)?
Work it out before opening the solution. Citation needed
Show the reasoning
- Keep F(5) = 5 and F(6) = 8.
- Add them to obtain F(7) = 13.
- For the next step retain 8 and 13; earlier values are no longer needed.
Citation needed
- What repeated work does the table eliminate?
- Recomputing the same Fibonacci subproblem. Citation needed
To check your understanding, reproduce the implementation without looking, explain why its main step is safe, and test an edge case before moving to the next algorithm. Citation needed
Sources
- MIT OpenCourseWare: Introduction to Algorithms — lecture notes — further study of the underlying algorithm and analysis.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms — correctness arguments and complexity analysis.