Talking about the algorithm - Kidd jewelry selection and greedy algorithm

This article originating in individual public number: TechFlow

1

Before starting today's article, let's tell a story:

In a dark and stormy night a month high, Kidd sneaked a well-known jewelry hall. He had three in front of the cabinet filled with jewelry, these three rules are A, B and C. Each cabinet is furnished with a jewelry, volume three of jewelry are 6, the value is 10,5 respectively, 6. Kidd can only open a cupboard, he needs to steal the jewelry into the carry bag. His package volume is 10, so I ask, Kidd should take what strategy?

Students will have learned algorithm glance, this is a knapsack problem .

We pretend never learned, according to common sense to analyze it. Apparently ABC three jewelry A jewelry is among the most valuable, since Kidd can only open a cupboard, in theory, should acquire A.

However, if A took, since the volume of A is too large, then it can not fit in the B or C. Therefore, the correct answer should be a chance to acquire C B , this can get the value is 11, otherwise it can only hold a A, the value is 10.

Every time we make a decision when they are selecting the most immediate return options, this algorithm is called greedy algorithm . Just as we face three most valuable jewelry to choose the same, but up to the moment of return does not represent the final maximum return, so we can not rely on the immediate benefits to decision-making. That greedy method there will be a counterexample of.

There are counter-examples greedy this conclusion is not difficult, it is easy to want to understand, the difficulty is how do we judge the current problem, greedy algorithm can not use it ? In particular, we may think of half past one in the case of counterexample.

There is a relatively tricky method is called Equal assumptions law , the name is my take, never heard of normal, do not tangle. Indeed textbooks which talked about the greedy algorithm, but also did not elaborate on this issue, but in practice this issue is very important. Equal assume that law is actually very simple, it is assumed that we in a particular decision, faced with the option of two evenly matched, we judge by the final result of these two options are the same greedy algorithm to determine whether or not the establishment . If the same two equal options for the final result, then the greedy algorithm is feasible, otherwise infeasible.

2

We also use an example of the problem just Kidd:

Kidd stole assume this is no longer the jewelry, but very expensive perfume , and assume that will not affect its value after mixing perfume . ABC now has three bottles of perfume, bottle volume of value are 6, respectively, 10,5,5. Kidd bottle volume is 10, apparently Kidd should first load value is 10 A perfume. When Kidd Bahrain A perfume bottle volume there is a surplus, this time he should be loaded B or C perfume it perfume?

B and C because the value of the perfume is the same, we try to know, whether we choose B or C have no effect on the final result. That these two options are both equal, and its final result is equal. So this time, we believe that the greedy algorithm is feasible.

The above-mentioned principle I think we should all slow connection, but some people may also have such a question. I want to judge the final results of the two options are the same, since such a decision can be made, then obviously should have known greedy method may not be feasible, and how this method can be called to verify the feasibility of greedy it?

The answer is very simple, we say it is to judge whether the end result is consistent, but in fact, we only need to determine the next several intermediate results, you can determine the final result. The end result is a more accurate representation just to say, after all, this is not a strict mathematical proof, but only for our own method of speculation.

### 3

Let's look at another example:

Conan The school has recently had a lot of leadership to inspect, schools need to arrange a number of meetings, but the school has only one conference room up to the teachers want to know how to arrange the meeting. The teachers for this thing bruised and battered, had invited Conan help. Will Conan way to calculate what should it?

We assume that meeting, a total of n, the time to start is si, the end of time is ei. We use the greedy algorithm to solve, relatively easy to think that all the meeting in accordance with the starting time to sort, assuming we follow the start time of ordering, in order to arrange. We then use just assume equal method to determine the feasibility of this algorithm.

Assuming that there are two conference start time, like the end of a different time. So in the current algorithm, these two conferences are equal. A conference is assumed that the two [1,3], and the other is [1,4]. Obviously the final results of these two options is likely to be different, because if there is a meeting [3,4]. If you choose the first meeting, we can arrange, and select the second can not. It will affect the final result, so this greedy strategy is not feasible.

Positive solution should be in accordance with the end of time to sort, order arrangements. We use the same method to determine the equalization is assumed, as there are two assumptions session end time, and can be arranged, each time they are [2,3] and [1,3]. Which one of them regardless of choice, will end at 3:00, it does not affect the arrangements for the latter.

You might ask, if a [1,2] meeting there is what will happen? In fact it can be proved that such meetings do not exist, because if there is, then according to the order, you should arrange [1,2] meeting. In this way, the start of the meeting before 2:00 will be excluded. So these two options do not begin with a set up, naturally not equal a. If it is equal, and that such a meeting would not exist.

These are the Equal assumptions law.

4

Finally, we go back to the beginning of the story, if we are Kidd, we really ought to get B and C do?

The answer should not be, take the B and C income, of course, is the biggest, but there are two problems. Volume three ABC jewelry only a thin 1, we really can be estimated by the naked eye, we took the A B or C will not fit yet? Besides, the more open the cabinet, the greater the probability of discovery, we really can get two jewel?

Obviously, in real life the greedy method does not work, or should think twice.

This article to end here, please point a " concern " it.

Guess you like

Origin www.cnblogs.com/techflow/p/12169494.html