2

I'm reading Competitive Programmer’s Handbook. They take the Euro to perform analysis on a greedy algorithm.

For example, if the coins are the euro coins (in cents) {1,2,5,10,20,50,100,200} and n (the sum we want to construct)= 520, we need at least four coins. The optimal solution is to select coins 200+200+100+20 whose sum is 520.

Then the following explanation is given as to why this always works for euros:

First, each coin 1, 5, 10, 50 and 100 appears at most once in an optimal solution, because if the solution would contain two such coins, we could replace 57 them by one coin and obtain a better solution. For example, if the solution would contain coins 5+5, we could replace them by coin 10.

In the same way, coins 2 and 20 appear at most twice in an optimal solution, because we could replace coins 2+2+2 by coins 5+1 and coins 20+20+20 by coins 50 + 10. Moreover, an optimal solution cannot contain coins 2 + 2 + 1 or 20+20+10, because we could replace them by coins 5 and 50.

Using these observations, we can show for each coin x that it is not possible to optimally construct a sum x or any larger sum by only using coins that are smaller than x. For example, if x = 100, the largest optimal sum using the smaller coins is 50+20+20+5+2+2 = 99. Thus, the greedy algorithm that always selects the largest coin produces the optimal solution.

My question stems from the last paragraph. Just because we cannot find an optimal solution for x = 100, why is it the conclusion that the greedy algorithm is the best in this case?

Take the coin set {1, 3, 4}. There are no optimal solutions using smaller coins for 1 and 3, but for 4: 4 = 1+3

1+3 = 4 is an optimal solution because 1 and 3 cannot be replaced by some other coin (whereas 2+2+1 = 5 for euros). But this, to me does not justify greedy algorithm, and indeed it does not work in this case. (6 = 4 + 1 + 1 vs 3 + 3)

What is the argument that the author is trying to make in this case? Am I misunderstanding something?

Yai0Phah
  • 10,031
herophant
  • 149

1 Answers1

1

The greedy algorithm is not optimal for any set of coins; it is optimal for the Euro coins sets.

Actually there is a definition of a canonical coin system that is, if the optimal solution of any change-making instance is the one returned by the greedy algorithm.

Please find some literature here : https://arxiv.org/pdf/0809.0400.pdf

  • I know this. My question is what the author wanted to prove in their ad hoc proof for the Euro coins only. – herophant Jul 10 '19 at 11:53
  • I was pointing out that your counter-example does not contradict the author. However, if, only using coins smaller than $x$, you can't create an optimal sum whose value is $\geq x$, then the greedy algorithm is optimal. Indeed, if from a total $N$ you take off the biggest coin possible, as you cannot remove that quantity using a smaller number of coin (by the virtue of the previous remark), then you are ensure to use the smallest numbers of coins. – Florian Ingels Jul 10 '19 at 12:01
  • "However, if, only using coins smaller than x, you can't create an optimal sum whose value is ≥x, then the greedy algorithm is optimal." Can you edit your answer to show why this is true? – herophant Jul 10 '19 at 12:03
  • 1
    Because that means that if the sum is at least $x$, the optimum solution must contain $x$, which is the greedy algorithm. – saulspatz Jul 10 '19 at 13:29
  • @saulspatz I fail to see why this is true... Consider the euro coins described in the problem. Say x = 5. 2+2+1 = 5. Here we used coins smaller than x to create something $ \geq x$, since we are not allowed to use $x=5$ in the condition that I quoted form Florian. By this condition, the greedy algorithm should not apply in this case, which is obviously false since it does for Euros. – herophant Jul 10 '19 at 14:22
  • I don't understand what you are saying. What "condition" did you quote? The greedy solution is to use a $5$-Euro coin. – saulspatz Jul 10 '19 at 14:24
  • @saulspatz "However, if, only using coins smaller than x, you can't create an optimal sum whose value is ≥x, then the greedy algorithm is optimal." Here, we are using coins smaller than 5, 2, 2 and 1, to create something that is equal to 5. The solution is optimal because none of the smaller coins can be replaced by other smaller coins. If you're saying to replace 2+2+1 with 5, then you can replace 1+3 by 4, which would prove the coin set {1, 3, 4} is optimal, which is not the case. – herophant Jul 10 '19 at 14:31
  • An optimal solution is one which uses the fewest possible coins. Using coins less than $5$, you can't create an optimal solution for $5$. The best you can do is $3$ coins, and the optimal solution uses $1$ coin. Your $1,3,4$ example doesn't make sense to me. $3$ and $4$ aren't Euro coins. – saulspatz Jul 10 '19 at 14:35
  • @saulspatz Does this prove that the greedy algorithm works for Euros, because we can use 1 coin instead of 3? My counterexample is the coinset {1, 3, 4}. In this coinset, you can have 1 + 3 = 4. The optimal solution would obviously be the 1 coin, 4, so the same thing holds, as you said. But this set does not work with greedy algorithm since 6 = 4 + 1 + 1 if we use greedy, but optimal is 6 = 3 + 3 – herophant Jul 10 '19 at 14:42
  • @saulspatz The 1, 3, 4 coinset is supposed to be a different coinset from the Euro. I was trying to show that the same thing that holds for the Euro also holds for this new coinset, but the greedy algorithm is not optimal for the new coinset of {1, 3, 4} – herophant Jul 10 '19 at 14:46
  • Okay, I misunderstood you entirely. I need to think about it. – saulspatz Jul 10 '19 at 14:47
  • Your example doesn't contradict the author's claim. He says, "However, if, only using coins smaller than $x,$ you can't create an optimal sum whose value is $\geq x,$ then the greedy algorithm is optimal." In your example, $x=4.$ You have shown that using coins $<4$ you can construct an optimal sum of $6$, which is $\ge 4.$ The author doesn't say the greedy algorithm is optimal in this case, and as your example shows, it need not be. (It's difficult to believe there is an example where the condition fails and the greedy algorithm is optimal.) – saulspatz Jul 10 '19 at 15:09
  • @saulspatz yes, I figured it out. Thank you for your help. Also BTW you can use the "at" symbol like I have done with your name to notify others that you have commented, otherwise only the post author (Florian) gets the notification. Thanks again – herophant Jul 10 '19 at 15:48