0% found this document useful (0 votes)
9 views11 pages

23a-FAZ-Fibonacci Sequence - Road To Dynamic Programming

The document discusses the Fibonacci sequence and different approaches to calculating Fibonacci numbers, including a naïve recursive implementation, a top-down memoized approach, and a bottom-up iterative approach. It analyzes the time complexities of each approach and explains how the bottom-up method demonstrates dynamic programming principles.

Uploaded by

taruna nasution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

23a-FAZ-Fibonacci Sequence - Road To Dynamic Programming

The document discusses the Fibonacci sequence and different approaches to calculating Fibonacci numbers, including a naïve recursive implementation, a top-down memoized approach, and a bottom-up iterative approach. It analyzes the time complexities of each approach and explains how the bottom-up method demonstrates dynamic programming principles.

Uploaded by

taruna nasution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Fibonacci Sequence:

Road to Dynamic Programming

Design and Analysis of Algorithms


(CS3024)

09/05/2006

CS3024-FAZ 1
Fibonacci Sequence
The numbers: 0,1,1,2,3,5,8,13,21,34,…
Recurrence:
fib(0) = 0, fib(1) = 1
fib(n) = fib(n-1) + fib(n-2), for n ≥ 2

CS3024-FAZ 2
The Naïve Implementation

function fib(n)
if n = 0 or n = 1
return n
else
return fib(n − 1) + fib(n − 2)

CS3024-FAZ 3
Call tree for fib(5)

CS3024-FAZ 4
Naïve = Exponential Time!
How many times was fib(2) calculated
from scratch?
Is it really necessary?

What do you suggest?

CS3024-FAZ 5
Alt1: Let’s save them
Suppose we have a simple map object, m,
which maps each value of fib that has
already been calculated to its result

var m := map(0 → 0, 1 → 1)
function fib(n)
if n not in keys(m)
m[n] := fib(n − 1) + fib(n − 2)
return m[n]
CS3024-FAZ 6
Evaluation
The resulting function requires only O(n)
time instead of exponential time
Let’s try for fib(5):
How many times was fib(2) calculated?

CS3024-FAZ 7
Top-down Approach
We first break the problem into
subproblems and then calculate and store
values
Saving values that have already been
calculated = “Memoization”

CS3024-FAZ 8
Alt2: Bottom-Up Approach

CS3024-FAZ 9
Bottom-up function
function fib(n)
var previousFib := 0, currentFib := 1
repeat n − 1 times
var newFib := previousFib +

currentFib
previousFib := currentFib
currentFib := newFib
return currentFib
CS3024-FAZ 10
Bottom-Up Approach
Calculates smaller values of fib first, then
build larger values from them

This bottom-up version is close to the


simple imperative loop that might be used
to compute the Fibonacci function in an
introductory computer science course 
we have been using dynamic
programming!
CS3024-FAZ 11

You might also like