The correctness proof of some greedy algorithm for changing coins problem

Source: Internet
Author: User

One, the problem introduction

Recently, we have been looking at the correctness of the greedy algorithm (how to prove that the solution obtained by the greedy algorithm must be the optimal solution), and feel that the "pruning" technique is used more. Another look at the "Introduction to the algorithm" in the greedy algorithm chapter inside a practice---find coins problem. This problem for some denominations of the coin is the best solution, so record some of the proof of ideas.

Consider the question of finding n cents with the fewest number of coins, assuming that the value of each coin is an integer.

If the unit of a convertible coin is a power of C, that is, c0,c1, ..., CK, where the integer c>1,k>=1

It is proved that greedy algorithm can always produce an optimal solution.

Second, a greedy strategy for changing coins

The greedy strategy here is easy to think of: always give preference to large-denomination coins to find . For example, existing 1 points, 5 points, 10 points, 25 cent of the coins can be used to change, now we need to find n=32 cents, how to find?

Preference for large denominations, that is the first choice of 25 points, after the election, but also to find 32-25=7, then choose 5 points, and finally the first 2 1 points. Can.

Here: 32=25+5+1+1, 4 coins were used altogether. So, here's the problem!!! Is there any other strategy that requires only 3 coins? or less of a coin?

Does this need to prove that the greedy strategy has no optimal solution? For this problem, if the value of the coin used to find a change satisfies a certain property , the greedy strategy has the best solution.

One of the properties here is that the unit of the convertible coin (or denomination) is the power of C, that is, c0,c1, ..., CK, which is the integer c>1,k>=1

The correctness proof is given below.

Third, the correctness of the greedy algorithm for changing coins is proved

Before proving, a property is mentioned: for the optimal solution, if a coin with a CI value is used to change the change, CI can only use c-1.

Since our goal is to use a minimum number of coins, for the best solution, if you use a coin with a CI value for change, CI can only use c-1.

Why? Assuming that C (more than C is also the same) value of ci-denominated coins to change, then why I do not use a face value of ci+1 coins to find it?

In this way, I can use less coin number AH (c>1). Therefore, in the optimal solution, if the use of the value of CI for the coin to change, then the face value of CI can only use the c-1 of a coin.

OK, the following again use the idea of pruning to prove the correctness of the greedy choice.

The general idea is to look at an optimal solution, and then prove that the solution can be modified to use greedy choice, the choice of the original problem into a similar, but "smaller" problem. ("Smaller" is an abstraction, not a smaller one in the sense of the word)

K is the number of coins that can be swapped, n is the value to be found, and a (i) indicates how many coins are used for CI. Suppose the coins have been sorted from small to large in nominal terms.

For the greedy choice, we will certainly choose the C^j coin, because: our greedy strategy is always the first choice of the most face value of the coin.

Our goal is to prove that, for all non- greedy choices, they are unlikely to produce optimal solutions.

For non-greedy selection, a coin with a face value of c^j (or higher) is not selected. The non-greedy option uses the following coins:

where a (i) denotes the use of a (i) value of a ci-denominated coin, the sum of which is N. Don't forget, n is the number that needs change.

For greedy options, if the C^j coin is available (when n>=c^j), we prefer to use a coin with a face value of c^j.

So there are:

This shows that in the case of a coin with a par value of c^j, in other words: In the case of C^j <=n, the non- greedy choice does not use C^J. (The choice is, that is greedy choice ...)

There will be contradictions, according to the nature of the aforementioned: there, a (i) <= c-1

Thus, the correctness of greedy selection is proved.

Again to understand it: the general idea is: to examine an optimal solution, and then prove that the solution can be modified to use the greedy choice, the choice of the original problem into a similar, but "smaller" problem. ("Smaller" is an abstraction, not a smaller one in the sense of the word)

The optimal solution here is: an optimal solution under a non-greedy selection, then pruning: Removing an element from a non-greedy selection and then adding that element to the greedy selection. This is actually the formula below.

Thus getting a better question.

Four, references

The correctness proof of greedy algorithm for partial knapsack problem

The correctness proof of some greedy algorithm for changing coins problem

The difference and relation between dynamic programming and greedy algorithm from the viewpoint of activity selection

Find the solution to the coin problem and distinguish it from the 0-1 knapsack problem

The correctness proof of some greedy algorithm for changing coins problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.