Upload
kaushik-nadikuditi
View
224
Download
0
Embed Size (px)
Citation preview
8/10/2019 Dyn Prog Change
1/6
The Change Problem
"The Change Store" was an old SNL skit (a pretty dumb
one...) where they would say things like, "ou need !hange ora #$% &e'll gie you two tens, or a ten and two ies, or our
ies, et!."
you are a dorky minded CS # student, you might ask
yoursel (ater you ask yoursel why those writers get paid so
mu!h or writing the !rap that they do), "*ien a !ertain
amount o money, how many dierent ways are there to make
!hange or that amount o money%"
Let us simpliy the problem as ollows+
*ien a positie integer n, how many ways !an we make
!hange or n !ents using pennies, ni!kels, dimes and uarters%
-e!ursiely, we !ould break down the problem as ollows+
To make !hange or n !ents we !ould+) *ie the !ustomer a uarter. Then we hae to make !hange
or n/#0 !ents
#) *ie the !ustomer a dime. Then we hae to make !hange or
n/$ !ents
1) *ie the !ustomer a ni!kel. Then we hae to make !hange
or n/0 !ents
2) *ie the !ustomer a penny. Then we hae to make !hange
or n/ !ents.
we let T(n) 3 number o ways to make !hange or n !ents, we
get the ormula
T(n) 3 T(n/#0)4T(n/$)4T(n/0)4T(n/)
8/10/2019 Dyn Prog Change
2/6
8/10/2019 Dyn Prog Change
3/6
2) d is , we !an simply return sin!e i you are only allowed
to gie pennies, you !an only make !hange in one way.
;lthough this seems uite a bit more !omple< than beore, the
!ode itsel isn't so long. Let's take a look at it+
publi! stati! int makeChange(int n, int d) @
i (n 5 $)
return $A
else i (n33$)
return A
else @ int sum 3 $A
swit!h (d) @
!ase #0+ sum43makeChange(n/#0,#0)A
!ase $+ sum43makeChange(n/$,$)A
!ase 0+ sum 43 makeChange(n/0,0)A
!ase + sum44A
B
return sumA B
B
There's a whole bun!h o stu going on here, but one o the
things you'll noti!e is that the larger n gets, the slower and
slower this will run, or maybe your !omputer will run out o
sta!k spa!e. urther analysis will show that many, many
method !alls get repeated in the !ourse o a single initial
method !all.
n dynami! programming, we want to ;87= these reo!!uring
!alls. To do this, rather than making those three re!ursie !alls
aboe, we !ould store the alues o ea!h o those in a two
dimensional array.
8/10/2019 Dyn Prog Change
4/6
7ur array !ould look like this
# 1 2 0 6 D E F $ # 1 2 0
0 # # # # # 1 1 1 1 1 2
$ # # # # # 2 2 2 2 2 6
#0 # # # # # 2 2 2 2 2 6
9ssentially, ea!h row label stands or the number o !ents we
are making !hange or and ea!h !olumn label stands or the
largest !oin alue allowed to make !hange.
(Note+ The lightly !olored suares with , # and are added to
!al!ulate the lightly !olored suare with 2, based on the
re!ursie algorithm.)
Now, let us try to write some !ode that would emulate building
this table by hand, rom let to right.
publi! stati! int makeChangedyn(int n, int d) @
GG Take !are o simple !ases.
i (n 5 $)
return $A
else i ((nH3$) II (n 5 0))
return A
GG Juild table here.
else @
intK denominations 3 @, 0, $, #0BA
intKK table 3 new intK2Kn4A
8/10/2019 Dyn Prog Change
5/6
GG nitialiMe table
or (int i3$A i5n4Ai44)
tableK$Ki 3 A
or (int i3$A i50A i44) @ tableKKi 3 A
tableK#Ki 3 A
tableK1Ki 3 A
B
or (int i30Ai5n4Ai44) @
tableKKi 3 $A
tableK#Ki 3 $A
tableK1Ki 3 $A B
GG ill in table, row by row.
or (int i3A i52A i44) @
or (int ?30A ?5n4A ?44) @
or (int k3$A k53iA k44) @
i ( ? H3 denominationsKk)
tableKiK? 43 tableKkK? / denominationsKkA B
B
B
return tableKlookup(d)KnA
B
B
;n alternate way to !ode this up is to realiMe that we =7N'T
need to add many dierent !ases up together. nstead, we note
that the number o ways to make !hange or n !ents using
denomination d !an be split up into !ounting two groups+
8/10/2019 Dyn Prog Change
6/6
) The number o ways to make !hange or n !ents using
denominations L9SS than d
#) The number o ways to make !hange or n !ents using atleast 7N9 !oin o denomination d.
The ormer is simply the alue in the table that is dire!tly
aboe the one we are trying to ill.
The latter is the alue on the table that is on the same row, by d
spots to the let.
8isually, !onsider ?ust adding two alues rom our preious
e