3

The Coin changing problem is making change for n cents using the fewest number of coins.

Can you give a set of coin denominations for which the greedy algorithm does not yield an optimal solution. The set should include a penny so that there is a solution for every n.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Anand
  • 43
  • 1
  • 8
  • The question resemble a school exercise for homework – Albert Lazaro de Lara Feb 14 '17 at 11:36
  • I won't add another answer but will mention that this can't happen in a good monetary system. This means that for systems like 1, 2, 5, 10, 20, 50 etc greedy algorithm is the best one. – xenteros Feb 14 '17 at 11:47
  • @xenteros the greedy algorithm may fail even in a good monetary system in cases where you do not have an infinite supply of coins or bills. See my example below. This definitely looks like a school homework problem. I was talking about this with my son, and it took a little thinking to come up with an example of a greedy solution not working. – AggieMan Jun 17 '20 at 20:27

3 Answers3

7

Well, given 10, 7, 1 coins change 15:

15 = 10 + 1 + 1 + 1 + 1 + 1 // greedy  (6 coins)
15 =  7 + 7 + 1             // optimal (3 coins)

You can easily generate a greedy solution as much inefficient as you want: just let available coins be 1, N-1, N and try to change 2 * N - 2:

 N, 1, 1, ..., 1 // greedy  (N - 1 coins)
 N-1, N-1        // optimal (2 coins)

Now, make N being large

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Nice answer, the first example is very clear! However, I think the last example would not yield the inefficient case, because a greedy algorithm would try: N + (N - 1) instead of N + 1 + 1 + ... + 1. Also, the optimal solution would be N + (N - 1), which is equal to 2 * N - 1. I think if we try to change X, the inefficient solution would be choosing amongst the coins: `floor(X/2) + 2, floor(X/2), 1`. E.g.: X = 123, the coins would be: 63, 61, 1. The greedy solution would be: 63 + 1 + 1 + ... + 1, and the optimal solution would be: 61 + 61 + 1. – mateleco Sep 24 '21 at 19:01
  • 1
    @mateleco: I'm sorry for the typo: the sum to exchange should have been `2 * N - 2`. – Dmitry Bychenko Sep 24 '21 at 19:14
  • 1
    Okey, after your edition it now makes sense. However, as a side note, your general solution leads to decimal coins if we are trying to change an odd amount of money. But, I don't mean to be picky, just to point it out for the record. Thanks for the answer! :) – mateleco Sep 24 '21 at 19:43
3

Coins: 1, 5, 8
Amount: 10
Greedy solution: 8, 1, 1 (3 coins)
Optimal: 5, 5 (2 coins)


To expand on @xenteros comment have a look at wikipedia (where btw. you would have found an example):

For the so-called canonical coin systems, like the one used in US and many other countries, a greedy algorithm of picking the largest denomination of coin which is not greater than the remaining amount to be made will produce the optimal result. This is not the case for arbitrary coin systems, though: if the coin denominations were 1, 3 and 4, then to make 6, the greedy algorithm would choose three coins (4,1,1) whereas the optimal solution is two coins (3,3).

dingalapadum
  • 2,077
  • 2
  • 21
  • 31
0

The greedy algorithm will not always work when the number of coins available is finite. Lets say you have the following in a cash register:

  • $50: 3 bills
  • $10: 11 bills
  • $5: 6 bills
  • $1: 9 bills
  • Quarters: 55 coins
  • Dimes: 47 coins
  • Nickels: None
  • Pennies: 4 coins

A customer comes in and buys candy worth 69 cents, pays with a $1 bill and needs to be given 31 cents in change.

The greedy algorithm will pick a quarter first and will then need to pick 6 cents more. There are no nickels and there are only 4 pennies. So it will fail to find the correct change. The correct change (3 dimes and 1 penny) can be found using dynamic programming.

AggieMan
  • 84
  • 8