solution1 - CMPT 405/705 Design and Analysis of Algorithms...

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

View Full Document Right Arrow Icon
CMPT 405/705 — Design and Analysis of Algorithms Outline Solutions to Exercises on Graphs and Greedy Algorithms 1. Consider the problem of making change for n cents using the fewest number of coins. Assume each coin’s value is an integer. (a) Describe a greedy algorithm to make change consisting of quaters, dimes, nickels, and pennies. Prove that your algorithm is optimal. (b) Give a set of coins for which the greedy algorithm does not yield an optimal solution. Your set should include a penny so that there is a solution for every value of n . (a) A greedy algorithm to make change using quarters, dimes, nickels, and pennies works as follows: If n =0 then the optimal solution is to give no coins. If n> 0 then determine the largest coin whose value is less than or equal to n . Let this coin has value c . Give one such coin, and then recursively solve the subproblem of making change for n c cents. To prove that this algorithm yields an optimal solution, we first need to show that the greedy-choice property holds, that is, that some optimal solutionto making change for n cents includes one coin of value c , where c is the largest coin value such that c n . Consider some optimal solution. If it includes a coin of value c , the we are done. Otherwise this optimal solution does not include a coin of value c . We have four cases to consider. If 1 n< 5 then c =1 . A solution must consist only of pennies, and so it must contain the greedy choice. If 5 n< 10 then c =5 . By assumption this optimal solution does not contain a nickel, and so consists of only pennies. Replace five pennies by one nickel to give a solution with four fewer coins. If 10 n< 25 then c =10 . By assumption this optimal solution does not contain a dime, and so it contains only nickels and pennies. Some subset of nickels and pennies in this solution adds up to 10 cents, and so we can replace these nickels and pennies by a dime to give a solution with (between 1 and 9) fewer
Background image of page 1

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

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

This note was uploaded on 11/11/2009 for the course CS 405/705 taught by Professor Bulatov during the Fall '09 term at Simon Fraser.

Page1 / 2

solution1 - CMPT 405/705 Design and Analysis of Algorithms...

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

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