sol3 - CMPT 307 - Data Structures and Algorithms: Solutions...

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: CMPT 307 - Data Structures and Algorithms: Solutions to Problem Set 3 November 5, 2008 1. Making change You’re given an unlimited supply of coins of denominations d 1 ,d 2 ,...,d k (where each d i is a positive integer), and a positive integer number n (all in binary). Your goal is to give the amount n of money, using in total as few coins as possible. More formally, you need to design an algorithm for the following problem. Given: positive integers d 1 ,d 2 ,...,d k and n (in binary). Find: a sequence of nonnegative integers v 1 ,v 2 ,...,v k (where each v i is the number of coins of denomination d i ) so that ∑ k i =1 v i d i = n , and ∑ k i =1 v i is minimized. (For example, for denominations 100 , 25 , 10 , 5 , 1, the value 57 is best given using coins 25 + 25 + 5 + 1 + 1, for the total of 5 coins.) The following is a natural greedy algorithm for the problem. GreedyChange ( d 1 ,...,d k ,n ) sort the coin denominations so that d 1 ≥ d 2 ≥ ...d k ; r ← n ; v [1 ..k ] ← 0; for i = 1 ..k if d i ≤ r then v i ← b r/d i c ; r ← r- v i d i ; endif endfor (a) Show that the GreedyChange algorithm does not always produce an optimal so- lution, even when it is able to produce a solution. (That is, given an example of coin denominations and an amount n , where the algorithm fails to produce an optimal solution.) Solution: Say the denominations are 10, 7, and 1, and the amount to be given in coins is 14. Then the greedy solution is 10 + 1 + 1 + 1 + 1 using 5 coins, but the optimal solution is 7 + 7, using only 2 coins. 1 (b) Prove that the GreedyChange algorithm is always correct for the case of Canadian coin denominations d 1 = 200, d 2 = 100, d 3 = 25, d 4 = 10, d 5 = 5, and d 6 = 1. Solution: We imagine that the Greedy algorithm is as follows: In each iteration, it chooses a coin of the largest denomination which is less than the amount n to be paid, and adds that coin to the partial solution. (The difference from the Greedy algorithm in the statement of the problem is that here we add only one coin in each iteration, but it is easy to see that the actual collection of coins produced by both algorithms on the same input are the same.) We say that a partial solution S i after iteration i is promising if it can be extended to an optimal solutions using the coins of denominations which are the same or smaller than the denomination of the coin used in iteration i . We will show that the greedy solution after each iteration is promising. The base case: i = 0; S = ∅ . Obviously S is promising....
View Full Document

This note was uploaded on 11/23/2009 for the course CS 307 taught by Professor A.bulatov during the Spring '09 term at Simon Fraser.

Page1 / 7

sol3 - CMPT 307 - Data Structures and Algorithms: Solutions...

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