cs 312: algorithm design & analysis lecture #23: making optimal change with dynamic programming...

31
CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warn This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License .

Upload: bruno-conley

Post on 13-Jan-2016

213 views

Category:

Documents


0 download

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

Assignment

HW #15

Read 6.3

Read Project #5 Instructions