History of Cs141 Home/Hwk2Soln

Cs141 Home | recent changes | Preferences


Revision 3 . . February 14, 2005 1:06 am by Neal
Revision 2 . . February 14, 2005 12:49 am by Neal
Revision 1 . . February 14, 2005 12:44 am by Neal
  

Difference (from prior major revision) (no other diffs)

Changed: 50,51c50,59
Let greedy(T) = {X} union greedy(T') be the set produced by greedy algorithm,
where X is quarter, dime, nickel, or penny (depending on T) and T' is T-value(X).
Suppose the coin that greedy adds before recursing is a quarter.
Suppose S also has a quarter in it. Let S' be the set S, minus the
quarter. S' must be a best-possible way of making change T-25
(otherwise S would not be a best-possible way of making change T,
because S' could be replaced in S by a smaller set with the same total).
So, by induction, we can assume greedy(T-25) = S'.
But then greedy(T) = S, because
::greedy(T) = {quarter} ∪ greedy(T-25) = {quarter} ∪ S' = {quarter} ∪ (S-{quarter}) = S.
So, if greedy is adding a quarter before recursing,
and S also has a quarter, we get greedy(T) = S.

Changed: 53,58c61,62
CASE 0: Suppose S also has an X in it (where X is the coin added by greedy, above).
Since S-{X} must be an optimal way of making change for T',
by induction, we know that greedy(T')=S-{X}. Thus,
:: greedy(T) = {X} ∪ greedy(T') = {X} ∪ (S-{X}) = S.
That is,
:: greedy(T) = S.
By the same reasoning, if greedy is adding a dime, nickel, or penny before recursing,
and S also has that same coin in it, we can show using induction as above that greedy(T) = S.

Changed: 60c64,65
So, if S has an X in it too, then greedy(T) produces S, and we are done.
Otherwise, S does not have the same coin, that greedy is adding.
We consider the possible cases and show that none of them can happen.

Changed: 62,65c67
Otherwise, S has no X in it. We consider the possible cases,
and show that none of them can happen.

CASE 1: T=1,2,3,4 (so X is a penny), and S has no penny in it.
CASE 1: T=1,2,3,4 (so greedy is adding a penny, and S has no penny in it).

Changed: 69c71
CASE 2: T=5,6,7,8,9 (so X is a nickel), and S has no nickel in it.
CASE 2: T=5,6,7,8,9, so greedy is adding a nickel, and S has no nickel in it.

Changed: 78c80
CASE 3: T is at least 10 and at most 24 (so X is a dime), and S has no dime in it.
CASE 3: T is at least 10 and at most 24, so greedy is adding a dime, and S has no dime in it.

Changed: 87c89
CASE 4: T is at least 25 (so X is a quarter), and S has no quarter in it.
CASE 4: T is at least 25, so greedy is adding a quarter, but S has no quarter in it.

Changed: 93,97c95,98
S cannot have two dimes and a nickel --- they could be replaced by a quarter.

So S has less than 25 cents worth of dimes and nickels, and the rest in pennies,
totalling 25. But then the dimes and nickels, together with some pennies,
could be replaced by a quarter to give a smaller set. So this cannot happen either.
So S has at most 20 cents worth of dimes, and the rest in pennies and nickels,
totalling 25 or more. But then the dimes, together with some of the nickels and/or pennies,
must total 25. But these coins could be replaced by a quarter to give a smaller set.
So this cannot happen either.

Cs141 Home | recent changes | Preferences
Search: