Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,

Similar presentations


Presentation on theme: "CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,"— Presentation transcript:

1 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

2 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

3 Objectives  Use the Dynamic Programming strategy to solve another example problem: “the coins problem”  Extract the composition of a solution from a DP table

4 The Coins Problem: Making Change

5

6 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

7 Making Optimal Change using DP

8 Recurrence

9

10 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

11 Example

12 Amount01234567 senine=101234567 seon=2 shum=4 limnah=7 j i

13 Making Change Amount01234567 senine=101234567 seon=2 shum=4 limnah=7 j i

14 Making Change Amount01234567 senine=101234567 seon=201??? shum=4 limnah=7 How does one compute C(2,2)? j i

15 Making Change Amount01234567 senine=101234567 seon=2011 shum=4 limnah=7 How does one compute C(2,2)? j i +1

16 Making Change Amount01234567 senine=101234567 seon=20112 shum=4 limnah=7 How does one compute C(2,3)? j i +1

17 Making Change Amount01234567 senine=101234567 seon=201122 shum=4 limnah=7 j i +1

18 Making Change Amount01234567 senine=101234567 seon=2011223 shum=4 limnah=7 j i +1

19 Making Change Amount01234567 senine=101234567 seon=201122334 shum=40112 limnah=7 j i

20 Making Change Amount01234567 senine=101234567 seon=201122334 shum=401121 limnah=7 j i

21 Making Change Amount01234567 senine=101234567 seon=201122334 shum=401121223 limnah=70112122 j i

22 Making Change Amount01234567 senine=101234567 seon=201122334 shum=401121223 limnah=701121221 j i

23 Question  Which coins?  Extract the composition of a solution from the table.

24 Extracting a Solution: C(4,7) Amount01234567 senine=101234567 seon=201122334 shum=401121223 limnah=701121221 j i C(4,7)=C(4,7-7)+1, so include a limnah Move to C(4,7-7).

25 Extracting a Solution: C(3,7) Amount01234567 senine=101234567 seon=201122334 shum=401121223 limnah=701121221 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?

26 Efficiency Amount01234567 senine=101234567 seon=201122334 shum=401121223 limnah=701121221 j i

27 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?

28 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)

29 Making Change: Top Down Amount01234567 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

30 Making Change: Top Down Amount01234567 senine=1 seon=2 shum=4 limnah=7 How much space and time did this require? j i

31 Comparison  How does DP algorithm for making change compare to the greedy algorithm?  speed  space  correctness  simplicity  optimality

32 Assignment  HW #15  Read 6.3  Read Project #5 Instructions


Download ppt "CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,"

Similar presentations


Ads by Google