Trang chủ » blog » Why does the greedy coin change algorithm not work for some coin sets?

# Why does the greedy coin change algorithm not work for some coin sets?

This is a recurrence trouble. Given a set of coins {Cn, Cn-1,. . ., 1}, such that for 1 <= k <= n, Ck > Ck-1, the Greedy Algorithm will yield the minimum number of coins if Ck > Ck-1 + Ck-2 and for the rate V=(Ck + Ck-1) - 1, applying the Greedy Algorithm to the subset of coins {Ck, Ck-1,. . ., 1}, where Ck <= V, results in fewer coins than the phone number resulting from applying the Greedy Algorithm to the subset of coins {Ck-1, Ck-2,. . ., 1} .
The test is childlike : for `1 < = k < = nitrogen test the number of coins the Greedy Algorithm yields for a value of Ck + Ck-1 - 1. Do this for coin set { Ck, Ck-1 ,. .., 1 } and { Ck-1, Ck-2 ,. .., 1 }. If for any k, the latter yields fewer coins than the erstwhile, the Greedy Algorithm will not work for this mint rig . For example, with n=4, consider the coin set { C4, C3, C2, 1 } = { 50,25,10,1 }. Start with k=n=4, then V = Cn + Cn-1 - 1 = 50+25-1 = 74 as test value. For V=74, G { 50,25,10,1 } = 7 coins. G { 25, 10, 1 } = 8 coins. therefore far, thus dear. nowadays let k=3. then V=25+10-1=34. G { 25, 10, 1 } = 10 coins but G { 10, 1 } = 7 coins. therefore, we know that that the Greedy Algorithm will not minimize the number of coins for the mint set { 50,25,10,1 }. On the early hired hand, if we add a nickle to this mint put, G { 25, 10, 5, 1 } = 6 and G { 10, 5, 1 } = 7. Likewise, for V=10+5-1=14, we get G { 10, 5, 1 } = 5, but G { 5,1 } = 6. thus, we know, Greedy works for { 50,25,10,5,1 } . That begs the motion : what should be the appellation of coins, satisfying the Greedy Algorithm, which results in the smallest worst case count of coins for any prize from 1 to 100 ? The answer is quite bare : 100 coins, each with a different value 1 to 100. arguably this is not very useful since it linear search of coins with every transaction. not to mention the expense of minting therefore many different denominations and tracking them.

now, if we want to primarily minimize the number of denominations while secondarily minimizing the resulting number of coins for any value from 1 to 100 produced by Greedy, then coins in denominations of powers of 2 : { 64, 32, 16, 8, 4, 2, 1 } consequence in a utmost of 6 coins for any value 1:100 ( the maximal number of 1 's in a seven bit count whose measure is less than decimal 100 ). But this requires 7 denominations of coin. The worst case for the five denominations { 50, 25, 10, 5, 1 } is 8, which occurs at V=94 and V=99. Coins in powers of 3 { 1, 3, 9, 27, 81 } besides require only lone 5 denominations to be serviceable by Greedy but besides yield a bad case of 8 coins at values of 62 and 80. last, using any the five denomination subset of { 64, 32, 16, 8, 4, 2, 1 } which can not exclude '1 ', and which satisfy Greedy, will besides result in a maximum of 8 coins. So there is a analogue tradeoff. Increasing the number of denominations from 5 to 7 reduces the utmost count of coins that it takes to represent any rate between 1 and 100 from 8 to 6, respectively .
On the other hand, if you want to minimize the number of coins exchanged between a buyer and a seller, assuming each has at least one mint of each denomination in their pocket, then this problem is equivalent to the fewest weights it takes to balance any slant from 1 to N pounds. It turns out that the fewest number of coins exchanged in a buy is achieved if the coin denominations are given in powers of 3 : {1, 3, 9, 27,. . .} .
See hypertext transfer protocol : //puzzling.stackexchange.com/questions/186/whats-the-fewest-weights-you-need-to-balance-any-weight-from-1-to-40-pounds .

reference : https://leowiki.com
Category : Economy