0% found this document useful (0 votes)
68 views7 pages

Paradigm: Dynamic Programming: Computing A Fibonacci Number

This document discusses using dynamic programming to compute the nth Fibonacci number. It describes how the naive recursive solution takes exponential time due to overlapping subproblems. Two dynamic programming strategies are presented: memoization and iteration. Memoization avoids recomputing values by storing previously computed Fibonacci numbers in a table, allowing them to be looked up in constant time. Iteration computes the values in order from F(2) to F(n) in linear time using a single pass over the input. Both strategies improve the time complexity to O(n) while using O(n) space, compared to the exponential time of naive recursion.

Uploaded by

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

Paradigm: Dynamic Programming: Computing A Fibonacci Number

This document discusses using dynamic programming to compute the nth Fibonacci number. It describes how the naive recursive solution takes exponential time due to overlapping subproblems. Two dynamic programming strategies are presented: memoization and iteration. Memoization avoids recomputing values by storing previously computed Fibonacci numbers in a table, allowing them to be looked up in constant time. Iteration computes the values in order from F(2) to F(n) in linear time using a single pass over the input. Both strategies improve the time complexity to O(n) while using O(n) space, compared to the exponential time of naive recursion.

Uploaded by

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

Paradigm: Dynamic Programming

Computing a Fibonacci Number

R. Inkulu
http://www.iitg.ac.in/rinkulu/

(Computing a Fibonacci Number) 1/7


Problem

Compute the nth Fibonacci number.

(Computing a Fibonacci Number) 2/7


Unwinding recursion
f5

f3
f4

f2 f2 f1
f3

f2 f1 f1 f0 f0
f1

f1 f0

• At every internal node v, addition between two integers is computed


when the DFT visits v for the last time.
• Takes exponential time to compute fn .
Computing fn involves fn+1 − 1 additions. – proof by induction

(Computing a Fibonacci Number) 3/7


Overlapping Subproblems

f5

f3
f4

f2 f2 f1
f3

f2 f1 f1 f0 f0
f1

f1 f0

identify repetition of computations

(Computing a Fibonacci Number) 4/7


DP Strategy: Memoization
f5

f3
f4

f2
f3

f2 f1

f1 f0

• Avoid recomputations by saving previously computed values and make


them available through table lookup.
• Correctness: immediate from the recurrence; proof by induction

• Takes O(n) time and O(n) space.


(Computing a Fibonacci Number) 5/7
DP Strategy: Iteration

• Compute f (2), f (3), . . . , f (n) in that order — takes O(n) time and O(1)
space.

• A value that is being computed relies only on the values available in


tableau. In other words, no recurrence calls involved: yields better
performance in practice even if asymptotically time/space complexity
same as memoization.

(Computing a Fibonacci Number) 6/7


Bit complexity in additions

• nth Fibonacci number is O(φn ) hence the number of digits in it are


O(n lg φ)

• Even though the number of additions are O(n) via DP, in adding two
integers bit complexity needs to be taken into account for the asymptotic
complexity to be more realistic.

(Computing a Fibonacci Number) 7/7

You might also like