Greedy algorithm optimal solution problem 2

Source: Internet
Author: User

1. Questions

If the value of the coin is {1, 1*c, 2*c, ..., k*c}, then the greedy algorithm always uses the fewest coins for change.

such as "discrete mathematics and its applications," the greedy algorithm of the inverse example:

There are denominations of 1, 10, 25 coins, looking for 30.

Solution of greedy algorithm: 5c0 + 0c1 + 1c2 = 5*1 + 0*10 + 1*25 = 30, total 6 coins required

and the optimal solution is: 0c0 + 3c1 + 0c2 = 0*1 + 3*10 + 0*25 = 30, only 3 coins

But if the middle missing coin face value: {1, 5, 10, 15, 25},

Then the solution of the greedy algorithm is: 1*25 + 1*5 = 30, only 2 coins, is still one of the best solution (because there are 15*2 also the best solution)

2. Rules

Because the largest 25 denomination of the coin no longer satisfies the "greedy algorithm optimal solution problem" problem in the conditions, so can not be proved by the proof method, we find a way to meet the problem conditions of change plan, see if we can find some rules, and then prove that these laws to find some inferences to indirectly prove

Suppose there are denominations {1, 3, 6, 9} coins, when the need for change s of the optimal greedy algorithm change scheme:

1 3 6 9
9 1
10 1 1
11 2 1
12 1 1
13 1 1 1
14 2 1 1
15 1 1
16 1 1 1
17 2 1 1
18 2

As you can see from the table:

1> In addition to the maximum par value and 1 denominations of coins, there are at most 1 coins in other denominations

2> 1 coins with a maximum of 2

3. Proof of deduction

3.1 In addition to the maximum par value and 1 coins, the other denominations have a maximum of 1 coins

Optimal change formula: S = m01 + m11c + m22c + ... + MKKC

S = M01 + (M11 + M22 + ... + mkk) c = m01 + GC = M0 + GC (M0 < C, g >= 0)

From the formula, we can consciously feel the s, there must be a M0 face value of 1 coins, but from the rigorous mathematical logic, we still prove

3.2 Assuming the optimal change formula S = M0 + GC (M0 < C, g >= 0), there is another optimal change formula S1 = x + HC (x < c, x! = M0, g >= 0)

M0 + GC = x + HC

(m0-x) = (h-g) c or (X-M0) = (g-h) C (2 forms of proof are the same)

∵x! = M0

∴ (H-G) c! = 0

∴ (m0-x) = NC (n > 0)

∴M0 = NC + x > C (n > 0)

When the ∵M0 is greater than C, the C M0 must be converted into a coin with a face value of C, the total number of conversions is necessarily less than M0

∴M0 = NC + x is not established, assuming not established

∴ Optimal Change formula S, there must be M0 a coin with a face value of 1

3.3 Known s the number of M0 fixed, so as long as the number of S = GC Optimal change, that is, s optimal change scheme, we first try to find a variety of scenarios

S = GC = (M11 + M22 + ... + mkk) c

Remove C, g = M11 + M22 + ... + MKK

3.3.1 when G <= k, g = x (X∈{1, 2, ..., k}), a coin with a face value of x is the optimal solution

3.3.2 when k< g <= 2k, g = k + x (x∈{1, 2, ..., k}), only 2 coins are required, since 1 coins can only be expressed at the nominal value of K, and G > K, so g = g = k + x is the optimal solution

3.3.3 when 2k< g <= 3k, G = 2k + x (x∈{1, 2, ..., k}), only 3 coins can be used, because 2 coins can only be expressed in 2k of face value, and g > 2k, so g = 2k + x is the optimal solution

That is, for any one G, can always be expressed as G = nk + x This form (such as all of our numbers can be used (N10 + x) (0<= N, 0< x < 10) to represent), and this form of change scheme is optimal (using n + 1 coins)

3.4 proof, G = M11 + M22 + ... + mkk = nk + x (0 < x < K), no other change scheme uses the number of coins <= n

To disprove, suppose there is a change scheme G1 = M11 + M22 + ... + mkk, number of coins used <= n

The total number of coins in the ∵n is always greater than or equal to N of the total number of coins made up of {1c, 2c, ..., KC}

∴g1 <= NK < NK + x = g

∴g1! = G

∴ does not exist for this change plan G1

∴g = nk + x The change scheme is always optimal.

∴g always use the maximum number of k, and a number of other x to get the change, which completely satisfies the greedy algorithm's change scheme

∴s = Optimal Change quantity for GC is greedy algorithm

∴s = M0 + GC The optimal change scheme is greedy algorithm

Greedy algorithm optimal solution problem 2

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.