PPT5 - Dynamic Programming (Fibonacci Sequence Problem & Coin Change Problem)
PPT5 - Dynamic Programming (Fibonacci Sequence Problem & Coin Change Problem)
Week 5
Session 6
Dynamic Programming: Fibonacci Sequence
Problem & Coin Change Problem
The General Objectives
2
Outline
3
Concept of Dynamic Programming
4
Concept of Dynamic Programming :
Dynamic Programming Applications
5
Fibonacci Sequence Problem
6
Fibonacci Sequence Problem :
Using Naïve Method
7
Fibonacci Sequence Problem :
Recursive Call Tree
8
Fibonacci Sequence Problem :
Recursive Call Tree
9
Fibonacci Sequence Problem :
Number of Function Calls
11
Solving of Fibonacci
Sequence Problem :
Memoization
13
Solving of Fibonacci
Sequence Problem :
Fibonacci using Memoization
14
Solving of Fibonacci
Sequence Problem :
Top-Down vs Bottom-Up
Top-Down
- Trying to achieve the final result by calculating the components
making up these results.
- Usually, this is a recursive function.
Bottom-Up
- Trying to form a solution by calculating from the beginning, in a
structured way towards a solution.
- Usually, using looping control.
15
Coin Change Problem : Coins
Fractional coins
25. 50. 100. 200. 500, 1000
17
Coin Change Problem :
Cases
18
Coin Change Problem :
Case 1
Mathematical models:
- f(3) = true
- f(5) = true
- f(12) = true
- f(x) = false for x <0
- f(n) = f(n-3) OR f(n-5) OR f(n-12)
Pull Method
- If we want to know whether X can be formed, we need to know if X-3 or
X-5 or X-12 can be formed.
Push Method
- If the value of X can be formed, then the value of X +3, X+5 and X+12 can
be formed.
19
Coin Change Problem :
Pseudocode of Case 1
20
Coin Change Problem :
Results of Case 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
X X B X B B X B B B B B B B B B B B B B B B B B B
21
Coin Change Problem :
Case 2
Mathematical models:
- f(23) = min { f(20), f(18), f(11) } + 1
-f(n) = min { f(n-3), f(n-5), f(n-12) } + 1
Pull Method
-Minimum amount of coins to form the X values obtained from 1
plus the minimum number of coins to form the X-3 or X-5 or X-
12
(made smaller).
Push Method
-If the value of X can be formed with Y coin, then the value of
X+3, X+5, X+12 can be formed with Y+1 coins.
22
Coin Change Problem :
Case 2
1 iNilaiCari=23
2 iMaxCari=25
3 iJumKoin=3
4 koin[1]=3
5 koin[2]=5
6 koin[3]=12
7 for i=1 to iMaxCari do
8 C[i]=0
9 end for
10 for j=1 to iJumKoin do
11 C[koin[j]]=1
12 end for
13 for n=1 to iMaxCari do
14 for j=1 to iJumKoin do
15 if ((n-koin[j])>=1) AND (C[n-koin[j]]>0) then
16 if (C[n]=0) OR (C[n]>(1+C[n-koin[j]])) then
17 C[n]=1+C[n-koin[j]]
18 end if
19 end if
20 end for
21 end for
22 if C[iNilaiCari]>0 then
23 bBisaDicari=true
24 iJumKoinMinimal=C[iNilaiCari]
25 else
26 bBisaDicari=false
27 end if
23
Coin Change Problem :
Result of Case 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 0 1 0 1 2 0 2 3 2 3 1 3 4 2 4 2 3 5 3 4 3 4 2 4
24
Coin Change Problem :
Case 3
Mathematical models:
- f(23) = f(20) + f(18) + f(11)
- f(n) = f(n-3) + f(n-5) + f(n-12)
25
Coin Change Problem :
Pseudocode of Case 3
1 iNilaiCari=23
2 iMaxCari=25
3 iJumKoin=3
4 koin[1]=3
5 koin[2]=5
6 koin[3]=12
7 for i=1 to iMaxCari do
8 C[i]= 0
9 end for
10 for j=1 to iJumKoin do
11 C[koin[j]]=1
12 end for
13 for n=1 to iMaxCari do
14 for j=1 to iJumKoin do
15 if (n-koin[j])>=1 then
16 C[n]=C[n]+C[n-koin[j]]
17 end if
18 end for
19 end for
20 if C[iNilaiCari]>0 then
21 bBisaDicari=true
22 iJumKemungkinan=C[iNilaiCari]
23 else
24 bBisaDicari=false
25 end if
26
Coin Change Problem :
Pseudocode of Case 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 0 1 0 1 1 0 2 1 1 3 2 3 4 4 6 7 8 10 13 15 18 24 27 34
27
Exercise
1. Fibonacci with memoization pseudocode contained in
the explanation of this meeting using Top-Down
approach. Make a version of its Bottom-Up!
2. Explain how it works!
3. All pseudocodes of Coin Change Problem in this
session use pull method. Write pseudocodes using
push method for all cases!
28
Thank You