Value min of coins 1 2 3 4 5 6 7 8 9 10 1 1 1 value

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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(M-1) + 1 Value Min # of coins 1 2 3 4 5 6 7 8 9 10 121212323 2 minNumCoins(M) = min of minNumCoins(M-3) + 1 minNumCoins(M-5) + 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(M-c1) + 1 min minNumCoins(M) = of minNumCoins(M-c2) + 1 … minNumCoins(M-cd) + 1 RecursiveChange is not effic...
View Full Document

This note was uploaded on 02/10/2014 for the course CS 548 taught by Professor Asaben-hur during the Spring '12 term at Colorado State.

Ask a homework question - tutors are online