cs 312: algorithm design & analysis lecture #23: making optimal change with dynamic programming...
TRANSCRIPT
![Page 1: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/1.jpg)
CS 312: Algorithm Design & Analysis
Lecture #23: Making Optimal Change with Dynamic
ProgrammingSlides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
![Page 2: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/2.jpg)
Announcements
Project #4: “Intelligent scissors” Due: today
Project #5: Gene Sequence Alignment Begin discussing main ideas on Wednesday
Reading: worth your time
Mid-term Exam: coming up next week
![Page 3: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/3.jpg)
Objectives
Use the Dynamic Programming strategy to solve another example problem: “the coins problem”
Extract the composition of a solution from a DP table
![Page 4: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/4.jpg)
The Coins Problem: Making Change
Problem: Given , what’s the smallest number of coins to make change for from denominations ? Have unlimited supply of each type of coin
Example: different denominations Denominations Need a volunteer Take one coin at a time.
That’s a greedy algorithm
![Page 5: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/5.jpg)
The Coins Problem: Making Change
In general, does a greedy algorithm always give the optimal answer? Consider: No. Greedy can fail.
Goal: Give optimal change for any balance using any currency system.
Let’s apply the Dynamic Programming strategy.
![Page 6: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/6.jpg)
DP Strategy: From Problemto Table to Algorithm
1. Start with a problem definition
2. Devise a minimal description (address) for any problem instance and sub-problem
3. Define recurrence to specify the relationship of problems to sub-problems i.e., Define the conceptual DAG on sub-problems
4. Embed the DAG in a table Use the address as indexes: E.g., 2-D case: index rows and columns
5. Two possible strategies1. Fill in the sub-problem cells, proceeding from the smallest to the largest.2. Draw the DAG in the table from the top problem down to the smallest sub-
problems; solve the relevant sub-problems in their table cells, from smallest to largest.
Equivalently: solve the top problem instance recursively, using the table as a memory function
![Page 7: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/7.jpg)
Making Optimal Change using DP
Problem: What’s the smallest number of coins to make change for balance from denominations ?
Define problem representation: Let minimum number of coins needed to make change for balance using coins of type 1
through .
Define sub-problem representation: Let minimum number of coins needed to make change for balance using coins of type 1
through .
Key ideas for relationship among these sub-problems as a recurrence relation: Take one coin at a time. If , then you cannot use a coin of value If , binary choice: either take a coin of value or do not take one.
If you do take one, then then make change for the remaining balance using coins of values through If you don’t take one, then make change for the unchanged balance using coins of values through
![Page 8: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/8.jpg)
Recurrence
if 1
if 1
if 1&( , )
if 1& 0
if 1 0&
i
i
i
i
i jC i j
i j
i j
d
d
Let’s assumethat always.
![Page 9: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/9.jpg)
Recurrence
0 if 1
/ if 1
( , ) min{ ( , if 1) 1, ( 1, ) &
( 1, ) if 1& 0
0 if 1&
}
0
i
i i
i
i
j d i
C i j C i j d i j
C
C
i j i j d
i j
i j d
![Page 10: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/10.jpg)
Next Steps Using DP
Design the table to contain the needed sub-problem results
Design the DP algorithm to walk the table and apply the recurrence relation
Solve an example by running the DP algorithm
Modify the resulting algorithm for space and time, if necessary
![Page 11: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/11.jpg)
Example
different denominations
Denominations
Look familiar?
![Page 12: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/12.jpg)
What’s the smallest number of “coins” to make change for balance ?
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2
shum=4
limnah=7
= min. number of “coins” to make change with coins .
j
i
0 if 1
/ if 1
( , ) min{ ( , if 1) 1, ( 1, ) &
( 1, ) if 1& 0
0 if 1&
}
0
i
i i
i
i
j d i
C i j C i j d i j
C
C
i j i j d
i j
i j d
![Page 13: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/13.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2
shum=4
limnah=7
= min. number of “coins” to make j change with coins .
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 14: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/14.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 ???
shum=4
limnah=7
How does one compute C(2,2)?
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 15: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/15.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1
shum=4
limnah=7
How does one compute C(2,2)?
j
i
+1
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 16: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/16.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2
shum=4
limnah=7
How does one compute C(2,3)?
j
i
+1
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 17: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/17.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2
shum=4
limnah=7
j
i
+1
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 18: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/18.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3
shum=4
limnah=7
j
i
+1
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 19: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/19.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2
limnah=7
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 20: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/20.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1
limnah=7
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 21: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/21.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1 2 2 3
limnah=7 0 1 1 2 1 2 2
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 22: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/22.jpg)
Making Change
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1 2 2 3
limnah=7 0 1 1 2 1 2 2 1
j
i
𝐶 (𝑖 , 𝑗 )=min {𝐶 (𝑖 , 𝑗−𝑑𝑖)+1 ,𝐶 (𝑖−1 , 𝑗)}
![Page 23: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/23.jpg)
Question
Which coins?
Extract the composition of a solution from the table.
![Page 24: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/24.jpg)
Extracting a Solution: C(4,7)
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1 2 2 3
limnah=7 0 1 1 2 1 2 2 1
j
i
C(4,7)=C(4,7-7)+1, so include a limnahMove to C(4,7-7).
![Page 25: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/25.jpg)
Extracting a Solution: C(3,7)
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1 2 2 3
limnah=7 0 1 1 2 1 2 2 1
j
i
not=
C(3,7)= C(3,7-4) +1give a shum move left.
C(2,3)= C(2,1)+1give a seonmove left.
C(1,1) =C(1,0)+1give a seninemove left.
Can you think of other ways to extract a solution?
![Page 26: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/26.jpg)
Efficiency
Amount 0 1 2 3 4 5 6 7
senine=1 0 1 2 3 4 5 6 7
seon=2 0 1 1 2 2 3 3 4
shum=4 0 1 1 2 1 2 2 3
limnah=7 0 1 1 2 1 2 2 1
j
i
![Page 27: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/27.jpg)
Algorithm in Pseudo-codefunction coins(d, J)Input: Array d[1..m] specifies the denominations; J is the balance for which to make changeOutput: Minimum number of coins needed to make change for J units using coins from d
array c[1..m,0..J]
for i=1 to m doc[i,0] = 0for j=1 to J do
if i<1 thenc[i,j] = 0
else if i =1 thenc[i,j] = j / d[i]
else if i > 1 && j >= d[i] then c[i,j] = min(c[i-1,j],1+c[i,j-d[i]])
else if i >1 && 0<j<d[i] thenc[i,j] = c[i-1,j]
else if i>1 && j<=0 thenc[i,j] = 0
return c[m,J]
Easy translation from table + recurrence to pseudo-code!
How much space? How much time?
![Page 28: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/28.jpg)
Pre-computing vs. on-the-fly
Eager: Pre-computing Fill the table bottom-up, then extract solution
Lazy: On-demand Build the DAG (in the table) top-down Solve only the necessary table entries (from
bottom-up or top-down)
![Page 29: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/29.jpg)
Making Change: Top Down
Amount 0 1 2 3 4 5 6 7
senine=1
seon=2
shum=4
limnah=7
Top down: start at bottom right, make recursive calls.Save time by storing every intermediate value.
j
i
![Page 30: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/30.jpg)
Comparison
How does DP algorithm for making change compare to the greedy algorithm? speed space correctness simplicity optimality
![Page 31: CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,](https://reader036.vdocuments.site/reader036/viewer/2022070410/56649ef25503460f94c03a81/html5/thumbnails/31.jpg)
Assignment
HW #15
Read 6.3
Read Project #5 Instructions