[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

1303.0. "Change Question" by CSC32::S_JOHNSON (Lifetime Member of Aye Phelta Thi) Mon Oct 01 1990 13:47

Hi,

I have a problem I'd like to solve.

I have a table of values where each element represents a coin value.  The table
is filled in a sorted order such that a[i-1] > a[i].  I came up with an
algorithm to scan thru the table from 1 to n, and which makes change for a given
amount.  When I consider a[i], I can get as many coins of this type to return.
Will this always create a minimal total number of coins for a requested amount?
If it will, then how can I prove this?  If it will not, then for what values
will this not work?

The algorithm is as follows:

a[n] = {50,25,10,5,1};  a[i] table.
num_of_a[5];  Table to hold number of a[i] coins.

  initialize i;
  while (change not equal to 0) do
    while (change is greater or equal to a[i]) do
      increment num_of_a[i];
      change = change - a[i]; 
    end do
    increment i;
  enddo

Thanks for any and all replies.

scott

T.RTitleUserPersonal
Name
DateLines
1303.1GUESS::DERAMODan D'EramoMon Oct 01 1990 15:2714
>Will this always create a minimal total number of coins for a requested amount?
>If it will, then how can I prove this?  If it will not, then for what values
>will this not work?

	I can't think of a counter-example for this particular table
	of coin values.  But there are tables for which this process
	is not minimal.  For example, with a[n] = {10,6,1} you will
	use three coins making change for 12 (i.e., 10, 1, and 1)
	instead of the minimal two coins (6 and 6).

	Can you prove anything in general if each coin has at least
	twice the value of the next coin?

	Dan
1303.2CSC32::S_JOHNSONLifetime Member of Aye Phelta ThiMon Oct 01 1990 16:2819
    Dan,
    
    Thanks for the specific instance of where the process fails.
    
<	Can you prove anything in general if each coin has at least
<	twice the value of the next coin?
    
    Supposedly, if this is the case, the number of coins generated will be
    the minimal amount.  How can it be proved?   
    
    I imagine the following might have something to do with it.  If 
    
      a[n] = {2^k-1, 2^k-2, 2^k-3...,2^1,2^0}
    
    Let c be the amount to represent using the coins in A[n].
    Then 0 <= c <= (2^k)-1 if each coin is used only once.  
    
    scott
1303.3False.CADSYS::COOPERTopher CooperMon Oct 01 1990 17:0528
RE: .2 (scot re: .1 Dan)

><	Can you prove anything in general if each coin has at least
><	twice the value of the next coin?
>    
>    Supposedly, if this is the case, the number of coins generated will be
>    the minimal amount.  How can it be proved?

    It cannot because it is not true.  Let the coins be {21, 10, 1}.  Each
    coin in the sequence is at least twice the next.  Given a request for
    30� the algorithm will produce:

		1*21� + 9*1� = 10 coins

    instead of:

		3*10� = 3 coins

    So what are the characteristics of a coin sequence which will make the
    algorithm optimal?

    A sufficient, but I suspect not necessary, condition is that each coin
    be evenly divisible by the next smaller coin.  The proof is straight-
    forward.

    By the way, algorithms of this form are known as "greedy algorithms".

						Topher
1303.4what if...CSC32::S_JOHNSONLifetime Member of Aye Phelta ThiMon Oct 01 1990 17:445
    
    What if a(n) is {k^n-1, k^n-2, k^n-3,...,k,k^0}?  Will the number of
    coins generated be the minimal number?
    
    scott
1303.5for being easy, this ain't easyTRACE::GILBERTOwnership ObligatesMon Oct 01 1990 20:0018
.4> What if a(n) is {k^n-1, k^n-2, k^n-3,...,k,k^0}?  Will the number of
.4> coins generated be the minimal number?

    For this one, yes.  For any amount of money, the min-change solution
    will contain fewer than k of any denomination (except perhaps the
    k^(n-1) denomination); if it contained k or more coins, k of them could
    be exchanged for the next higher denomination.  Given this constraint,
    the min-change solution is unique (think of it as a radix-k number).

    Now it's easy to see that the greedy algorithm yields the same solution,
    though the highest denomination must be handled with some care:
    The k^(n-2) thru k^0 denominations contribute at most k^(n-1)-1 to the
    sum, thus the min-change solution must use as many k^(n-1) coins as
    possible, which is just what the greedy algorithm does.

.3> A sufficient, but I suspect not necessary, condition is that each coin
.3> be evenly divisible by the next smaller coin.  The proof is straight-
.3> forward.
1303.6TRACE::GILBERTOwnership ObligatesMon Oct 01 1990 20:1932
.0> Will this always create a minimal total number of coins for a requested amount?
.0> a[n] = {50,25,10,5,1};  a[i] table.

Yes, it will.  Suppose the amount is N.  The number of "1"s in the change
is N mod 5 -- if there were more than 5, replace 5 of them with a "5" coin.

As for the other coins, in a min-change solution, there may only be 0 or 1
"25"s, 0 to 2 "10"s, and 0 or 1 "5"s.

	25 10  5   Sum
	-- -- --   ---
	 0  0  0    0
	 0  0  5    5
	 0 10  0   10
	 0 10  5   15
	 0 20  0   20
	 0 20  5   25	("25" is better)
	25  0  0   25
	25  0  5   30
	25 10  0   35
	25 10  5   40
	25 20  0   45
	25 20  5   50

Thus, we see that it's never best to give fewer than the maximum number of
"50" coins -- the min-change solution couldn't possibly absorb them (only
one entry above is for 50� or more, and that one is clearly not part of a
min-change solution).

Now all the other choices for min-change are forced, except for the two
possibilities for 25�.  And for that, the greedy algorithm gives the
optimal change.
1303.7how greed failsTRACE::GILBERTOwnership ObligatesTue Oct 02 1990 20:0815
.3>  So what are the characteristics of a coin sequence which will make the
.3>  [greedy] algorithm optimal?

Consider the three coin sequence {c,b,1}, with c > b > 1.  The greedy
algorithm can fail iff the b and c satisfy:

	(c-1) div b + (c-1) mod b < (b-1)

This is a surprisingly pretty result!  ...which I discovered via la computer
after tiring of the by-hand approach.  FWIW, the by-hand approach reached:

The greedy algorithm can fail for {c,b,1} iff there are k and m satisfying:

        (m-1)*b/k < c <= m*(b-1)/k,	0 < k < m < b < c.