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

07 DP Coin Change Problem

The document discusses the coin change problem in dynamic programming. It describes the coin change problem as finding the minimum number of coins needed to make change for a given amount using a set of coin denominations. It provides examples and explains how to model the problem using a recursive solution and a dynamic programming solution. The dynamic programming solution avoids recomputing subproblems by storing results in a table for lookup, making its runtime linear in the money amount and number of denominations.

Uploaded by

assd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
356 views

07 DP Coin Change Problem

The document discusses the coin change problem in dynamic programming. It describes the coin change problem as finding the minimum number of coins needed to make change for a given amount using a set of coin denominations. It provides examples and explains how to model the problem using a recursive solution and a dynamic programming solution. The dynamic programming solution avoids recomputing subproblems by storing results in a table for lookup, making its runtime linear in the money amount and number of denominations.

Uploaded by

assd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Algorithms:

Dynamic Programming

Coin Change Problem

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem

Given unlimited amounts of coins of denominations c1 > … > cd ,


give change for amount M with the least number of coins.

Example: c1 = 25, c2 =10, c3 = 5, c4 = 1 and M = 48


Greedy solution: 25*1 + 10*2 + 1*3 = c1 + 2c2 + 3c4

Greedy solution is
 optimal for any amount and “normal’’ set of denominations

 may not be optimal for arbitrary coin denominations

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Choice Principles
 Suppose you want to count out a certain
amount of money, using the fewest
possible coins.
 At each step, take the largest possible
coin that does not overshoot.

 Example: To make Tk. 157/-, you,


 Choose a Tk. 100/- note,
 Choose a Tk. 50/- note,
 Choose a Tk. 5/- coin,
 Choose a Tk. 2/- coin.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Greedy Choice Principles: Failure
 To find the minimum number of US coins to make any amount,
the greedy method always works
 At each step, just choose the largest coin that does not overshoot the
desired amount: 31¢ = (25+5+1)

 The greedy method would not work if we did not have 5¢ coins
 For 31 cents, the greedy method gives seven coins (25+1+1+1+1+1+1),
but we can do it with four (10+10+10+1)

 The greedy method also would not work if we had a 21¢ coin
 For 63 cents, the greedy method gives six coins (25+25+10+1+1+1), but
we can do it with three (21+21+21)

 The greedy algorithm results in a solution, but always not in an


optimal solution
 How can we find the minimum number of coins for any given
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 1 1

Only one coin is needed to make change for the


values 1, 3, and 5

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 2 2

However, two coins are needed to make change for


the values 2, 4, 6, 8, and 10.

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Example
Given the denominations 1, 3, and 5, what is the
minimum number of coins needed to make change
for a given value?

Value 1 2 3 4 5 6 7 8 9 10
Min # of coins 1 2 1 2 1 2 3 2 3 2

Lastly, three coins are needed to make change for


the values 7 and 9

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Recurrence

This example is expressed by the following


recurrence relation:

minNumCoins(M-1) + 1
min minNumCoins(M-3) + 1
minNumCoins(M) =
of
minNumCoins(M-5) + 1

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Recurrence
Given the denominations c: c1, c2, …, cd, the
recurrence relation is:

minNumCoins(M-c1) + 1

min minNumCoins(M-c2) + 1
minNumCoins(M) =
of …
minNumCoins(M-cd) + 1

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: A Recursive Algorithm

1. RecursiveChange(M, c, d)
2. if M = 0
3. return 0
4. bestNumCoins  infinity
5. for i  1 to d
6. if M ≥ ci
7. numCoins  RecursiveChange(M – ci , c, d)
8. if numCoins + 1 < bestNumCoins
9. bestNumCoins  numCoins + 1
10. return bestNumCoins

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


RecursiveChange is not Efficient

 It recalculates the optimal coin combination for a given


amount of money repeatedly
 i.e., M = 77, c = (1, 3, 7):
 Optimal coin for 70 cents is computed 9 times!

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


The RecursiveChange Tree

77

76 74 70

75 73 69 73 71 67 69 67 63

747268 686662 706864 686662 626056


727066 727066 666460 666460

... ..
70 70 70 70 70
.
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
We Can Do Better

 We are re-computing values in our algorithm more than


once

 Save results of each computation for 0 to M

 This way, we can do a reference call to find an already


computed value, instead of re-computing each time

 Running time M*d, where M is the value of money and d


is the number of denominations

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem: Dynamic Programming

1. DPChange(M, c, d)
2. bestNumCoins0  0 Running time: O(M*d)
3. for m  1 to M
4. bestNumCoinsm  infinity
5. for i  1 to d
6. if m ≥ ci
7. if bestNumCoinsm – c + 1 < bestNumCoinsm
i

8. bestNumCoinsm  bestNumCoinsm – c + 1
i

9. return bestNumCoinsM

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


DPChange: Example

0 0 1 2 1 2 3 2
0 0 1 2 3 4 5 6

0 1
0 1 0 1 2 1 2 3 2 1
0 1 2 3 4 5 6 7
0 1 2
0 1 2
0 1 2 1 2 3 2 1 2
0 1 2 1 0 1 2 3 4 5 6 7 8
0 1 2 3
0 1 2 1 2 3 2 1 2 3
0 1 2 1 2 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4

0 1 2 1 2 3
c = (1, 3, 7)
0 1 2 3 4 5 M=9

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


Coin Change Problem

Goal: Convert some amount of money M into given


denominations, using the fewest possible number of coins

Input: An amount of money M, and an array of d


denominations c = (c1, c2, …, cd), in a decreasing order of
value (c1 > c2 > … > cd)

Output: A list of d integers i1, i2, …, id such that


c1i1 + c2i2 + … + cdid = M and i1 + i2 + … + id is minimal

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


  

 …

 … + bn
 ... [1-0.5^i<1]

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET


  dnc(A, l, h)
 if (l==h){
 if (A[l]>0) return 1;
 else return 0;
 }
 mid =
 c1 = dnc(A,l,mid);
 c2 = dnc(A,mid+1,h);
 return c1+c2;

Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET

You might also like