This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: HOMEWORK 3  SOLUTIONS March 20, 2008 Problem 1: a) void coinchange(int X, int &x1, int &x2, int &x3) //x1 is the number of dimes, x2 is the number of nickels, //and x3 is the number of pennies { x1 = X/10; X %= 10; x2 = X/5; X %= 5; x3 = X; } b) Proof that the greedy solution is optimal: Notice that the optimal solution would never have more than 4 pennies (5 pennies can be replaced by one nickel) and also never has more than 1 nickel(2 nickels can be replaced with one dime). Therefore we can say that any solution which satisifies the above condition is an optimal solution. Now we can prove that this is true for the greedy program’s solution. The number of nickels is x 2 = (( X %10) / 5). Since X %10 ≤ 9, x 2 ≤ 1. Similarly, x 3 ≤ 4. Therefore the solution returned by the greedy algorithm is optimal. c) The solution is not optimal if we have coins worth 4 cents. We can prove this using a simple counterexample. For X = 8, the greedy algorithm would 1 give 1 nickel and 3 cents, a total of 4 coins while the optimal solution is 2...
View
Full
Document
This note was uploaded on 04/11/2008 for the course EECS 281 taught by Professor Jag during the Winter '08 term at University of Michigan.
 Winter '08
 Jag

Click to edit the document details