Dynamic programming example: Fibonacci numbers [00QH]
- October 27, 2026
We return to the Fibonacci numbers as a simple first example. Although dynamic programming is hardly necessary to compute Fibonacci numbers in reality, they still make for a nice illustrative example.
Naïve recursive Fibonacci [00QI]
- October 27, 2026
Recall the definition of the Fibonacci numbers:
The Fibonacci numbers are the sequence F_n defined by the recurrence
\begin {align*}
F_0 &= 0 \\
F_1 &= 1 \\
F_n &= F_{n-1} + F_{n-2}.
\end {align*}Definition Fibonacci numbers [00OZ]
# Fibonacci #1: naive recursive algorithm
def fib1(n):
if n <= 1:
return n
else:
return fib1(n-1) + fib1(n-2)Why is it so slow? If we draw the recursion call tree for, say, fib1(5), we can see the problem:
fib1(5) calls fib1(4) and fib1(3); but fib1(4) again calls fib1(3); and so on. fib1(1) ends up being called five times! If we were to start with a bigger number, it would be even worse.
The problem, then, is that fib1 spends a lot of time redundantly recomputing values that have already been computed before.
Memoization [00QJ]
- October 27, 2026
This is the core problem of dynamic programming: repeated recursive calls on the same input. The solution is memoization (note: not "memorization"!): creating some kind of data structure to remember previously computed outputs corresponding to given inputs, so that if the input is ever seen again the output can just be looked up without having to recompute it.
Dynamic programming technique 1: recursion with a memo table [00QK]
- October 27, 2026
The most direct thing we can do, therefore, is to keep our recursive function, but create a table in which we can save previously computed outputs. Every time the recursive function is called, we can check whether the answer for that input has already been computed and saved. If so, return it; if not, compute it recursively, save it, in the table, and return it.
# Fibonacci #2: recursion with memoization
# Global table of Fibonacci numbers; assume no one will
# ever call fib2(n) with n >= 1000
fib_table = [-1] * 1000
def fib2(n):
if n <= 1:
fib_table[n] = n
elif fib_table[n] == -1:
fib_table[n] = fib2(n-1) + fib2(n-2)
return fib_table[n]Even the first time it is called, fib2(n) runs in \Theta (n) time: each value in the table is computed at most once, and from then on it is simply looked up as needed without triggering any repeated recursive calls.
This approach is simple to code, and we don't have to worry about the right order in which to fill in the table; it will simply be filled in as needed. However, since we are just filling in a table anyway, one might wonder what the point is of using a bunch of recursion...
Dynamic programming technique II: filling in memo table directly [00QL]
- October 27, 2026
Once we recognize that the code in the previous section is essentially just filling in entries in a table, we can dispense with the recursion and simply use a loop to fill in the table directly.
# Fibonacci #3: explicitly filling in a table with a loop
def fib3(n):
fibs = [0] * (n+1) # Create initial array of all 0s
fibs[1] = 1
for i in range(2, n+1):
fibs[i] = fibs[i-1] + fibs[i-2] # Each fibs[i] is the sum of the previous two
return fibs[n]fib3(n) obviously runs in \Theta (n) time: it does \Theta (1) amount of work to fill in each entry in a table of size n.
This approach can be much more efficient in practice than using recursion; but we have to be careful to fill in the table in the correct order, so that we fill in each value only after all the values it depends on have been filled in.