CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides 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.Creative Commons Attribution-Share Alike 3.0 Unported License
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
Objectives Use the Dynamic Programming strategy to solve another example problem: “the coins problem” Extract the composition of a solution from a DP table
The Coins Problem: Making Change
DP Strategy: From Problem to 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 strategies 1.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
Making Optimal Change using DP
Recurrence
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
Example
Amount senine= seon=2 shum=4 limnah=7 j i
Making Change Amount senine= seon=2 shum=4 limnah=7 j i
Making Change Amount senine= seon=201??? shum=4 limnah=7 How does one compute C(2,2)? j i
Making Change Amount senine= seon=2011 shum=4 limnah=7 How does one compute C(2,2)? j i +1
Making Change Amount senine= seon=20112 shum=4 limnah=7 How does one compute C(2,3)? j i +1
Making Change Amount senine= seon= shum=4 limnah=7 j i +1
Making Change Amount senine= seon= shum=4 limnah=7 j i +1
Making Change Amount senine= seon= shum=40112 limnah=7 j i
Making Change Amount senine= seon= shum= limnah=7 j i
Making Change Amount senine= seon= shum= limnah= j i
Making Change Amount senine= seon= shum= limnah= j i
Question Which coins? Extract the composition of a solution from the table.
Extracting a Solution: C(4,7) Amount senine= seon= shum= limnah= j i C(4,7)=C(4,7-7)+1, so include a limnah Move to C(4,7-7).
Extracting a Solution: C(3,7) Amount senine= seon= shum= limnah= j i not= C(3,7)= C(3,7-4) +1 give a shum move left. C(2,3)= C(2,1)+1 give a seon move left. C(1,1) =C(1,0)+1 give a senine move left. Can you think of other ways to extract a solution?
Efficiency Amount senine= seon= shum= limnah= j i
Algorithm in Pseudo-code function coins(d, J) Input: Array d[1..m] specifies the denominations; J is the balance for which to make change Output: 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 do c[i,0] = 0 for j=1 to J do if i<1 then c[i,j] = 0 else if i =1 then c[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] then c[i,j] = c[i-1,j] else if i>1 && j<=0 then c[i,j] = 0 return c[m,J] Easy translation from table + recurrence to pseudo-code! How much space? How much time?
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)
Making Change: Top Down Amount 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
Making Change: Top Down Amount senine=1 seon=2 shum=4 limnah=7 How much space and time did this require? j i
Comparison How does DP algorithm for making change compare to the greedy algorithm? speed space correctness simplicity optimality
Assignment HW #15 Read 6.3 Read Project #5 Instructions