MHB Problem of making change for n cents

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Change
Click For Summary
The discussion centers on developing a greedy algorithm to make change for n cents using the fewest coins possible, specifically quarters, dimes, nickels, and pennies. The algorithm operates by selecting the highest value coin available, subtracting its value from the total, and repeating this process until no amount remains. To demonstrate that this approach yields an optimal solution, two properties are established: optimal substructure and the greedy choice property. The optimal substructure shows that removing the highest value coin from an optimal solution still allows for an optimal solution for the remaining amount, while the greedy choice property ensures that each choice leads to fewer coins overall. This confirms that the greedy algorithm is effective for making change optimally.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I am looking at the following exercise.

Consider the problem of making change for $n$ cents using the fewest number of
coins. Assume that each coin’s value is an integer.
Describe a greedy algorithm to make change consisting of quarters, dimes,
nickels, and pennies. Prove that your algorithm yields an optimal solution.Could you give me a hint what we could do? (Thinking)
 
Technology news on Phys.org
evinda said:
Hello! (Wave)

I am looking at the following exercise.

Consider the problem of making change for $n$ cents using the fewest number of
coins. Assume that each coin’s value is an integer.
Describe a greedy algorithm to make change consisting of quarters, dimes,
nickels, and pennies. Prove that your algorithm yields an optimal solution.Could you give me a hint what we could do? (Thinking)

The algorithm works by choosing the coin with the highest value, subtract that value from the amount to change, and repeat until the amount to change is 0. To prove this greedy algorithm yields an optimal solution, we must show optimal substructure and the greedy choice property. Optimal substructure:
Assume to make changes for n cents, the first coin chosen by the greedy algorithm is a c1 coin with value of n1. Let C be the optimal solution to change n cents. Then C – {c1} is an optimal solution to change n-n1 cents. Otherwise, let the optimal solution be B. Then B U{c1} contains fewer coins than C and yet both makes up n cents, so C cannot be the optimal solution making change for n cents, which is a contradiction. Greedy choice property:
If our first choice is a penny, then the amount to change is at most 4 cents. Optimal choice cannot make the change without using a penny, since any other coin makes too much change. If our first choice is a nickel, then the amount to change is between 5 cents and 9 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins, contracting that it is an optimal solution. Otherwise, the optimal choice can only change 4 cents, not enough to make the change. If our first choice is a dime, then the amount to change is between 10 cents and 24 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins. Otherwise, if it uses 2 nickels, we can turn it to a dime. Otherwise, it can only make change for at most 9 cents, not enough to make the change. If our first choice is a quarter, then the amount to change is at least 25 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins. Otherwise, if the optimal solution uses at most 2 dimes, it must use enough pennies to make up at least 25 cents, and we can turn it to a quarter and use fewer coins. Otherwise, the optimal solution uses at least 3 dimes, we can turn it to a quarter and a nickel and use fewer coins.
Therefore the greedy choice property is satisfied.
 
Last edited:
phymat said:
The algorithm works by choosing the coin with the highest value, subtract that value from the amount to change, and repeat until the amount to change is 0. To prove this greedy algorithm yields an optimal solution, we must show optimal substructure and the greedy choice property. Optimal substructure:
Assume to make changes for n cents, the first coin chosen by the greedy algorithm is a c1 coin with value of n1. Let C be the optimal solution to change n cents. Then C – {c1} is an optimal solution to change n-n1 cents. Otherwise, let the optimal solution be B. Then B U{c1} contains fewer coins than C and yet both makes up n cents, so C cannot be the optimal solution making change for n cents, which is a contradiction.

I see... (Nod)

phymat said:
Greedy choice property:
If our first choice is a penny, then the amount to change is at most 4 cents. Optimal choice cannot make the change without using a penny, since any other coin makes too much change. If our first choice is a nickel, then the amount to change is between 5 cents and 9 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins, contracting that it is an optimal solution. Otherwise, the optimal choice can only change 4 cents, not enough to make the change. If our first choice is a dime, then the amount to change is between 10 cents and 24 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins. Otherwise, if it uses 2 nickels, we can turn it to a dime. Otherwise, it can only make change for at most 9 cents, not enough to make the change. If our first choice is a quarter, then the amount to change is at least 25 cents. If the optimal choice uses at least 5 pennies, we can turn it to a nickel and use fewer coins. Otherwise, if the optimal solution uses at most 2 dimes, it must use enough pennies to make up at least 25 cents, and we can turn it to a quarter and use fewer coins. Otherwise, the optimal solution uses at least 3 dimes, we can turn it to a quarter and a nickel and use fewer coins.
Therefore the greedy choice property is satisfied.

Could you explain to me what we are supposed to show? :confused:
Why do we say how high the amount to change is, based on out first choise? :confused:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
9K