Docsity

Money Changing Problem: Finding Optimal Coins for a Given Amount - Prof. Zhaojun Bai, Study notes of Computer Science

Toggle sidebar
bg1
ECS122A Handout Money Changing
1. The money changing problem starts with a given set of positive integers called denomi-
nations x1, x2,...,xn(think of them as the integers 1, 5, 10, and 25) and an integer A,
we want to find nonnegative integers a1,...,an≥0 such that
A=
n
X
i=1
aixi
.
2. First, we note that Acan be expressed as a linear combination of the xiif and only if
xi= 1 for some i. Here is a proof.
If one of your denominations xiis 1, you will certainly be able to express every integer
Aas Pn
i=1 aixifor some nonnegative integers a1,···, an. Conversely, in order to express
A= 1 as a linear combination, you must have xi= 1 for some i.
3. In general (not necessarily satisfying the condition in the first part), a necessary condi-
tion that A=Pn
i=1 aixiis that g=gcd(x1, ..., xn) divides A. In fact, g|Aturns out to
be both necessary and sufficient for A≥Xfor some (large) X. Here is a proof.
From the extended Euclidean algorithm we know we can write g=Pn
i=1 gixiwith some
possibly negative gi. Now let G=Pn
i=1 |gi|xi,xmin = minixi,k=xmin/g, and X=kG.
First note that the kconsecutive multiples of gin the set S={kG, kG +g, kG +
2g, . . . , kG + (k−1)g}all have nonnegative coefficients when written as Pn
i=1 aixi. The
next multiple of gis kG+kg =kG +xmin, which has even larger nonnegative coefficients
than kG. The next k−1 multiples of gconsequently also have nonnegative coefficients
until we get to kG + 2xmin, and so on.
Note that the coefficients are not necessarily unique (all the xicould be identical), but
we have shown that there is at least one set of nonnegative coefficients for all multiple
of gat least equal to X.
4. Optimal money changing problem is that for a given A, find the nonnegative ai’s that
satisfy A=Pn
i=1 aixi, and such that the sum of all ai’s is minimal —that is, you use the
smallest possible number of coins. Here is a greedy algorithm for solving this problem:
Order your denominations such that x1> x2>··· > xn. Then the greedy algorithm
for this problem would be: Given A, let a1be the largest integer such that a1x1≤A.
If A−a1x1>0, let a2be the largest integer such that a2x2≤A−a1x1. If you have
nothing left over after doing this for i= 1,···, n, then A=Pn
i=1 aixi.
pf2

Partial preview of the text

Download Money Changing Problem: Finding Optimal Coins for a Given Amount - Prof. Zhaojun Bai and more Study notes Computer Science in PDF only on Docsity!

ECS122A Handout Money Changing

  1. The money changing problem starts with a given set of positive integers called denomi- nations x 1 , x 2 ,... , xn (think of them as the integers 1, 5, 10, and 25) and an integer A, we want to find nonnegative integers a 1 ,... , an ≥ 0 such that

A =

∑^ n i=

aixi

.

  1. First, we note that A can be expressed as a linear combination of the xi if and only if xi = 1 for some i. Here is a proof. If one of your denominations xi is 1, you will certainly be able to express every integer A as

∑n i=1 aixi^ for some nonnegative integers^ a^1 ,^ · · ·^ , an. Conversely, in order to express A = 1 as a linear combination, you must have xi = 1 for some i.

  1. In general (not necessarily satisfying the condition in the first part), a necessary condi- tion that A =

∑n i=1 aixi^ is that^ g^ =^ gcd(x^1 , ..., xn) divides^ A. In fact,^ g|A^ turns out to be both necessary and sufficient for A ≥ X for some (large) X. Here is a proof. From the extended Euclidean algorithm we know we can write g =

∑n i=1 gixi^ with some possibly negative gi. Now let G =

∑n i=1 |gi|xi,^ xmin^ = mini^ xi,^ k^ =^ xmin/g, and^ X^ =^ kG. First note that the k consecutive multiples of g in the set S = {kG, kG + g, kG + 2 g,... , kG + (k − 1)g} all have nonnegative coefficients when written as

∑n i=1 aixi. The next multiple of g is kG+kg = kG+xmin, which has even larger nonnegative coefficients than kG. The next k − 1 multiples of g consequently also have nonnegative coefficients until we get to kG + 2xmin, and so on. Note that the coefficients are not necessarily unique (all the xi could be identical), but we have shown that there is at least one set of nonnegative coefficients for all multiple of g at least equal to X.

  1. Optimal money changing problem is that for a given A, find the nonnegative ai’s that satisfy A =

∑n i=1 aixi, and such that the sum of all^ ai’s is minimal —that is, you use the smallest possible number of coins. Here is a greedy algorithm for solving this problem: Order your denominations such that x 1 > x 2 > · · · > xn. Then the greedy algorithm for this problem would be: Given A, let a 1 be the largest integer such that a 1 x 1 ≤ A. If A − a 1 x 1 > 0, let a 2 be the largest integer such that a 2 x 2 ≤ A − a 1 x 1. If you have nothing left over after doing this for i = 1, · · · , n, then A =

∑n i=1 aixi.

  1. Let us show that the greedy algorithm finds the optimum ai’s in the case of the denom- inations 1, 5, 10, and 25. Here is a proof. Since 1 divides 5 and 5 divides 10, it is clear that if we have a case in which the greedy algorithm would not find the optimal solution, it must involve 25, i.e. A must be greater than 25. Assume the greedy algorithm does not find the optimal solution for A, A > 25. Then A =

∑ 4 i=1 aixi^ =^

∑ 4 i=1 bixi^ and^

∑ 4 i=1 ai^ >^

∑ 4 i=1 bi, where the^ ai^ were determined by the greedy algorithm and the bi are optimal in that

∑ 4 i=1 bi^ is minimal. W.l.o.g.^ a^4 =^ b^4 [since a 4 ≤ 4 any change of the number of 1 cent coins must occur in 5 unit steps to give the same sum-this is obviously worse than changing b 3 ], in addition to that note that a 3 ≤ 1. By the above considerations we must have a 1 > b 1. Let x := a 1 −b 1. We have three cases to consider: a 2 = b 2 , a 2 > b 2 and a 2 < b 2. If we set y := a 2 − b 2 then we can compute b 3 = 5x + 2y + a 3. Thus the number of coins changes by

∑ 4 i=1 bi^ −^

∑ 4 i=1 ai^ = 4x^ +^ y. If we can show that this number is positive, this is a contradiction and we are done. In cases 1 and 2, x and y are ≥ 0. Therefore 4x + y is clearly positive. In case 3, y is negative. But, as we have to ensure that b 3 = 5x + 2y + a 3 is ≥ 0 and we know that a 3 is at most 1, we have y ≥ −^52 x − 12. Hence 4x + y ≥ 32 x − 12 and it is again positive.

  1. You can extend this problem and ask “What are good necessary and sufficient conditions on a currency such that the greedy algorithm always gives the minimum amount of coins.” This problem is still open. Partial answers and a light–hearted discussion give:

(a) M. J. Magazine, G. L. Nemhauser, L. E. Trotter Jr., When the Greedy Solution Solves a Class of Knapsack Problems, Operations Research 23 (1975), p. 207 – 217 (b) John Dewey Jones, Orderly Currencies, American Mathematical Monthly 101 (1994), p. 36 – 38 (c) Stephen B. Maurer, Disorderly Currencies, American Mathematical Monthly 101 (1994), p. 419.