ps5sol - CS161: Design and Analysis of Algorithms Summer...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: CS161: Design and Analysis of Algorithms Summer 2004 Problem Set #5 Solutions General Notes Regrade Policy: If you believe an error has been made in the grading of your problem set, you may resubmit it for a regrade. If the error consists of more than an error in addition of points, please include with your problem set a detailed explanation of which problems you think you deserve more points on and why. We reserve the right to regrade your entire problem set, so your final grade may either increase or decrease. 1. [25 points] Coin Changing Consider a currency system with k 1 distinct (integer) denominations d 1 < . . . < d k . Assume that d 1 = 1 so that any integer amount greater than 0 can be formed. Throughout the problem, assume there are always coins of every denomination available when needed (i.e. you cannot run out of any denomination). Our goal will be to determine how to make change using the fewest number of coins. Some parts of this problem are similar to problem 16-1 on page 402 of CLRS. You may wish to read the statement of that problem to see how similar questions are presented. (a) [2 points] Describe a greedy strategy for making change using as few coins as possible. This strategy should work for the US coin system which uses denominations { 1 , 5 , 10 , 25 } . You do not need to prove the correctness of this strategy for this set of denominations. Answer: The greedy strategy consists of always taking the largest denomination that we can at the time. We repeat this until we have the correct amount of change. MAKE-CHANGE( M ) 1 for i = k downto 1 2 count [ i ] floorleft M/d i floorright 3 M M- count [ i ] d i Since for each denomination we calculate the number of coins we can take until we would make more than the amount of change asked for, this operation takes time O ( k ). We can also write this in a slightly less efficient manner that will be useful when analyzing the correctness of the greedy strategy. In this case, we simply take one coin at a time of the largest denomination possible. MAKE-CHANGE-SLOW( M ) 1 for i = k downto 1 2 while M d i 3 count [ i ] count [ i ] + 1 4 M M- d i This runs in time O ( C + k ) where C is the number of coins in the greedy solution. Problem Set #5 Solutions 2 (b) [5 points] Suppose that d i = c i- 1 for some integer c > 1, i.e. the denominations are 1 , c, c 2 , . . ., c k- 1 . Prove that the greedy strategy from part (a) is optimal. Answer: Well represent a change solution by a series of counts, one for each coin denomination. The counts must obey the property k i =1 count [ i ] d i = M . The number of coins in this solution is k i =1 count [ i ]. Note that the counts for each coin denomination d i < d k must be strictly less than c in an optimal solution....
View Full Document

Page1 / 14

ps5sol - CS161: Design and Analysis of Algorithms Summer...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online