Unformatted text preview: ns 25,20,10,5,1
• Let’s work towards a correct recursive
approach Change: An Example Change: An Example (cont’d) Given the denominations 1, 3, and 5, what is
the minimum number of coins needed to
make change for a given value? Given the denominations 1, 3, and 5, what is
the minimum number of coins needed to
make change for a given value? Value
Min # of coins 1 2 3 4 5 6 7 8 9 10
1 1 1 Value
Min # of coins 1 2 3 4 5 6 7 8 9 10
121212 2 2 Only one coin is needed to make change for
the values 1, 3, and 5 two coins are needed to make change for
the values 2, 4, 6, 8, and 10. Change: An Example (cont’d) Change: Recurrence Given the denominations 1, 3, and 5, what is
the minimum number of coins needed to
make change for a given value? This example is expressed by the following
recurrence relation:
minNumCoins(M1) + 1 Value
Min # of coins 1 2 3 4 5 6 7 8 9 10
121212323 2 minNumCoins(M) = min
of minNumCoins(M3) + 1
minNumCoins(M5) + 1 three coins are needed to make change for
the values 7 and 9 3 Change: Recurrence (cont’d) Change: A Recursive Algorithm Given the denominations c=(c1,c2, …, cd), the
recurrence relation is: RecursiveChange(M, c, d)
if M = 0
return 0
bestNumCoins infinity
for i 1 to d
if M ≥ c i
numCoins RecursiveChange(M – ci , c, d)
if numCoins + 1 < bestNumCoins
bestNumCoins numCoins + 1
return bestNumCoins minNumCoins(Mc1) + 1
min
minNumCoins(M) =
of minNumCoins(Mc2) + 1
…
minNumCoins(Mcd) + 1 RecursiveChange is not efﬁc...
View
Full
Document
This note was uploaded on 02/10/2014 for the course CS 548 taught by Professor Asabenhur during the Spring '12 term at Colorado State.
 Spring '12
 AsaBenHur
 Algorithms

Click to edit the document details