01 The difference between a backpack and a greedy

Source: Internet
Author: User

The 01 knapsack problem which the greedy method cannot solve is an inseparable problem
01 knapsack problem is to consider the overall optimal solution

The premise of greedy strategy is that local optimal strategy can lead to global optimal solution.

Analysis and editing of examples
Example 1,
[0-1 knapsack problem] There is a backpack, the backpack capacity is m=150. There are 7 items, items can not be divided into arbitrary size.
It is required to maximize the total value of the items loaded into the backpack, but not to exceed the total capacity.
Item A B C D E F G
Weight 35kg 30kg 6kg 50kg 40kg 10kg 25kg
Value 10$ 40$ 30$ 50$ 35$ 40$ 30$
Analysis:
Objective function: ∑pi max
The restriction is that the total weight of the loaded item does not exceed the backpack capacity: ∑wi<=m (m=150)
⑴ according to the greedy strategy, each time to select the most valuable items loaded into the backpack, the results are optimal?
⑵ can the optimal solution be obtained each time the smallest item is selected for loading?
⑶ each time the unit weight value of the most valuable items, become the solution of the strategy.
It is worth noting that the greedy algorithm is not completely not available, once the greedy strategy has been proved, it is an efficient algorithm.
Greedy algorithm is also a very common algorithm, this is because it is simple, the construction of greedy strategy is not very difficult.
Unfortunately, it needs to be proven before it can really be used in the algorithm of the problem.
In general, the proof of greedy algorithm revolves around: the optimal solution of the whole problem must be obtained by the optimal solution of the sub-problem in the greedy strategy.
For the 3 kinds of greedy strategies in the example, it is impossible to set up (can not be proved), explained as follows:
⑴ Greedy strategy: Choose the most valuable person.
Counter Example:
W=30
Item: A B C
Weight: 28 12 12
Value: 30 20 20
According to the strategy, first select Item A, then you can no longer select, however, choose B, C is better.
⑵ Greedy strategy: Choose the smallest weight. Its inverse example is similar to the first strategy's counter example.
⑶ greedy strategy: Select items with the highest value per unit weight.
Counter Example:
W=30
Item: A B C
Weight: 28 20 10
Value: 28 20 10
According to the strategy, three items per unit weight value, the program can not be judged according to the existing policy, if you choose a, the answer is wrong.
"NOTE: If the item can be divided into any size, then strategy 3 can get the best solution"
For the selection of items with the largest unit weight value, this strategy can add an optimized rule: for the unit weight value of the same, priority selection of small weight! Thus, the above counter-example is resolved.
However, if the topic is as follows, this strategy will not be the same.
W=40
Item: A B C
Weight: 25 20 15
Value: 25 20 15
Attached: The topic is a DP problem, with the greedy method does not necessarily find the optimal solution, after understanding the dynamic programming algorithm after the subject has a new solution.

01 The difference between a backpack and a greedy

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.