Fibonacci Numbers—Iterative Approach
Let's solve the Fibonacci numbers using the iterative approach with memoization.
We'll cover the following...
Once we see how the array is filled, we can replace the memoized recurrence with a simple for
loop that intentionally fills the array in that order instead of relying on a more complicated recursive algorithm to do it for us accidentally.
Algorithm
Implementation
def iter_fibo(n):F = [0] * (n + 1)F[0] = 0F[1] = 1for i in range(2, n + 1):F[i] = F[i - 1] + F[i - 2]return F[n]print(iter_fibo(6))
Explanation
-
Line 2: We declare a vector named
F
of sizen + 1
with the initial value of0
for all elements. -
Lines 5–6: We iterate from
i = 2
toi = n + 1
and calculate the Fibonacci number for each indexi
in the arrayF
using the previously calculated valuesF[i - 1]
andF[i - 2]
. The calculated Fibonacci number forn
is returned. -
Lines 10: We print the Fibonacci number to the console.
Now the time analysis is immediate: clearly uses and stores integers.
This is our first explicit dynamic programming algorithm. The dynamic programming paradigm was formalized and popularized by Richard Bellman in the mid-1950s while working at the RAND Corporation, although he was far from the first to use the technique. In particular, this iterative algorithm for Fibonacci numbers was already proposed by and later Sanskrit prosodists in the 12th century and again by Fibonacci at the turn of the 13th century!
Many years afterward, Bellman claimed that he deliberately chose the name “dynamic programming” to hide the mathematical character of his work from his military bosses, who were actively hostile toward anything resembling mathematical research. The word “programming” does not refer to writing code but rather to the older sense of planning or scheduling, typically by filling in a table. For example, sports programs and theater programs are schedules of important events (with ads); television programming involves filling each available time slot with a show (and ads); degree programs are schedules of classes to be taken (with ads). The Air Force funded Bellman and others to develop methods for constructing training and logistics schedules or, as they called them, “programs.” The word “dynamic” was not only a reference to the multistage, time-varying processes that Bellman and his colleagues were attempting to optimize but also a marketing buzzword that would resonate with the futuristic can-do Zeitgeist of post-WWII America. Thanks in part to Bellman’s proselytizing, dynamic programming is now a standard tool for multistage planning in economics, robotics, control theory, and several other disciplines.
Reduction in space requirements
In many dynamic programming algorithms, it is not necessary to retain all intermediate results throughout the computation. For example, we can significantly reduce the space requirements of our algorithm by maintaining only the two newest elements of the array:
dAlgorithm
...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy