8
Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F 5 = 2+3 = 5; ……..

Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Embed Size (px)

Citation preview

Page 1: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Dynamic Programming

Fibonacci numbers-example- Defined by Recursion F0 = 0 F1 = 1

F n = Fn-1 + F n-2 n >= 2

F2 = 0+1; F3 = 1+1 =2; F 4= 1+2 = 3

F5 = 2+3 = 5; ……..

Page 2: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Calculating Fi

Option(1) --Directly implement Recursion int fib(int n) int f1,, f2 , f

If(n < 2) f = n; else f1 = fib(n-1)i

f2 = fib(n-2)i

f = f 1+ f2i

f = f I

very inefficient : how many calls ?

For example: for f6 ?

Page 3: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Dynamic programming / Fibonacci numbers Call structure of Recursion

4 7

23 2

2

1

6

1 1 0

5

1 0

4

0112

23

01

01

1st call

2nd

3rd

4th

5th

6th 7th

8th 10th 11th 14th 15th

16th

20th21st

24th 25th call

23rd

17th

18th

9th13th 19th 22nd

12th

Page 4: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Contd..

Very efficient: runtime at least (2n/2) calculates the same numbers several times.

IMPROVE: --- save previous results in a memory and recall when

needed. (n) calls

Full Binary Tree at least to depth n/2 2n/2 nodes.

recall computed values f[0] = 0; f[1] = 1; for (i=2; i< = n; i++) f[i] = f[i-1] + f[i-2];

Ω

Θ

Page 5: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Dynamic Programming Sub problem Graph

Decompose the main problem into smaller sub problems of the same kind.

Solve smaller problems recursively save the intermediate solution.

Combine the solutions.

Page 6: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Sub problem Graph

-- vertices are : instances

-- Directed edge : 1 3

1 directly calls 7

6 5 4 3 2 1 0

Structure of calls

Page 7: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Contd..

we can try to calculate in advance those instances which are needed for a given solution.

Those are exactly the nodes which are reachable from a given starting state(S).

The Depth-First search tree of the sub problem graph gives exactly those nodes which are reachable from S

perform dfs get reachable nodes Calculate sub problems and record solutions to dictionary

solution.

Page 8: Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F

Dynamic programming version of the Fibonacci function.

Example 10.3

fibDPwrap(n) Dict soln = create(n); return fibDP(soln, n);fibDP(soln, k)int fib, f1, f2;

If(k<2) fib = k;

else if(member(soln, k-1) = = false) f1 = fibDP(soln, k-1); else f1 = retrieve(soln, k-1);if(member(soln, k-2) = = false) f2 = fibDP(soln, k-2); else f2 = retrieve(soln, k-2); fib = f1 + f2;Store(soln, k, fib); return fib;