0% found this document useful (0 votes)
243 views

PPT5 - Dynamic Programming (Fibonacci Sequence Problem & Coin Change Problem)

The document discusses dynamic programming concepts and how to solve the Fibonacci sequence and coin change problems using dynamic programming. It explains how the Fibonacci sequence can be solved recursively but is inefficient due to redundant calculations. Dynamic programming solves this by using memoization to store previously calculated values. For coin change problems, it describes calculating the minimum number of coins needed to make a given amount using a bottom-up dynamic programming approach.

Uploaded by

Aditya wahyu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

PPT5 - Dynamic Programming (Fibonacci Sequence Problem & Coin Change Problem)

The document discusses dynamic programming concepts and how to solve the Fibonacci sequence and coin change problems using dynamic programming. It explains how the Fibonacci sequence can be solved recursively but is inefficient due to redundant calculations. Dynamic programming solves this by using memoization to store previously calculated values. For coin change problems, it describes calculating the minimum number of coins needed to make a given amount using a bottom-up dynamic programming approach.

Uploaded by

Aditya wahyu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

COMP6127 - Algorithm Design and Analysis

Week 5
Session 6
Dynamic Programming: Fibonacci Sequence
Problem & Coin Change Problem
The General Objectives

 Able to implements dynamic programming in solving problem


 Understand how to solve Fibonacci Sequence problem using
dynamic programming concept
 Understand how to solve Coin Change problem using dynamic
programming concept

2
Outline

 Concept of Dynamic Programming


 Fibonacci Sequence Problem
 Solving of Fibonacci Sequence Problem
 Coin Change Problem
 Solving of Coin Chance Problem

3
Concept of Dynamic Programming

 Dynamic Programming is a method of solving a problem that can


be used if the solution of a problem can be viewed as a sequence
of several decisions.
 Dynamic Programming vs. Greedy Method
- Dynamic Programming generate the optimal solution, because it does not

use local optimum like Greedy Method.


 Dynamic Programming vs. Naive Method
- Naive method: counting all Feasible Solutions.
- Dynamic Programming is not necessary to calculate all possible, saving
much time and more efficient.

4
Concept of Dynamic Programming :
Dynamic Programming Applications

 Fibonacci Sequence Problem


 Coin Change Problem
 Multistage Graph Problem
 Travening Salesman Problem
 0/1 Knapsac Problem

5
Fibonacci Sequence Problem

 Fibonacci Sequence is a sequence in which each element is the


sum of two previous elements.
 Suppose that we have Fibonacci sequence
- f(1),f(2),f(3),f(4),...
- f(3) is f(1)+f(2),
- f(4) is f(2)+f(3),
- and so on.

6
Fibonacci Sequence Problem :
Using Naïve Method

1 module fibo(n) 1 for i=1 to 100 do


2 if (n=0) or (n=1) then 2 display fibo(i),” ”
3 result=n 3 end for
4 else
5 result=fibo(n-1)+fibo(n-2)
6 end if
7 end module

7
Fibonacci Sequence Problem :
Recursive Call Tree

8
Fibonacci Sequence Problem :
Recursive Call Tree

9
Fibonacci Sequence Problem :
Number of Function Calls

N Number of cunction calls


1 1
2 3
3 5
4 9
5 15
6 25
7 41
8 67
... ...
100 1.146.295.688.027.634.168.201
10
Fibonacci Sequence Problem :
Redundancy

11
Solving of Fibonacci
Sequence Problem :
Memoization

• Memoization is a technique of saving values that have


already been calculated.
12
Solving of Fibonacci
Sequence Problem :
Number of Function Calls

N Number of function calls


• After adding memoization
1 1
2 3
3 5
4 7
5 9
6 11
7 13
8 15
... ...
100 199

13
Solving of Fibonacci
Sequence Problem :
Fibonacci using Memoization

1 module fibo(n) 1 for 1=1 to 100 do


2 if (n=0) or (n=1) then 2 F[i]=0
3 result=n 3 end for
4 else 4 for i=1 to 100 do
5 if F[n]=0 then 5 display fibo(i),” ”
6 F[n]=fibo[n1]+fibo[n-2] 6 end for
7 end if
8 result=F[n]
9 end if
10 end module

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

 Refund worth 750 can be obtained from:


- 1 piece 500, 2 piece 100, 1 piece 50
- 7 piece 100, 1 piece 50
- 5 piece 100, 3 piece 50
- and many other combinations.

 How about 835?


16
Coin Change Problem :
Variation Coin Change Problem

 Suppose that in a country there are only fractional coins


C1, C2 and C3.

 Then there are 3 variations Coin Change Problem:


- Is the change X can be formed from these fragments?
- There is a minimum number of coins to form X?
- How many combinations of ways to form the value of X?

17
Coin Change Problem :
Cases

 In a country there are only coin worth 3, 5 and 12.


1. Is the return 23 can be formed from these
fragments?
2. How many coins to form the return value of 23?
3. How many combinations of ways to form a value of
23?

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

1 iNilaiCari=23 13 for n=1 to iMaxCari do


2 iMaxCari=25 14 for j=1 to iJumKoin do
3 iJumKoin=3 15 if (n-koin[j])>=1 then
4 koin[1]=3 16 if C[n-koin[j]]=”B”
5 koin[2]=5 then
6 koin[3]=12 17 C[n]=”B”
7 for i=1 to iMaxCari do 18 end if
8 C[i]=”X” 19 end if
9 end for 20 end for
10 for j=1 to iJumKoin do 21 end for
11 C[koin[j]]=”B” 22 if C[iNilaiCari]=”B” then
12 end for 23 bBisaDicari=true
24 else
25 bBisaDicari=false
26 end if

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

• Column 23 marked “B”, means coins change value of


23 can be formed using coins 3, 5, and 12.

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

• Column 23 is 4, means coins change value of 23 can


be formed using coins 3, 5, and 12 with maximum 4
coins.

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)

 Number of combinations to form value of X are combinations that


can be formed value of X-3, X-5, and X-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

• Column 23 is 24, means coins change value of 23 can


be formed using coins of 3, 5, and 12 with 24
probability of combinations.

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

You might also like