01 knapsack problem dynamic programming in addition there are other solution do?

Explosive search method and greedy method also Solutions 01 backpack, but there are limitations.

Solution 01 backpack burst search method

Example: Capacity Backpack m = 10, the size of the article A = [2, 3, 5, 7], the value of goods V = [1, 5, 2, 4]

Search burst Solution: enumeration were taken every object or not taken, take 1 represents, 0 to not take

image

Burst search algorithm limitations :

image

01 backpack greedy solution

Take the highest value:

  • m=2, A = [1, 1, 2], V = [2, 2, 3]
  • Greedy answer: 3, the correct answer: 4

Take the lightest weight:

  • m=2, A = [1, 1, 2], V = [1, 1, 3]
  • Greedy Answer: 2, the correct answer: 3

Take the highest value per unit:

  • m=3, A = [1, 1, 3], V = [2, 2, 5]
  • Greedy Answer: 4, the correct answer: 5

You can see, all the greedy are wrong! ! !

So, how dynamic programming solution 01 backpack it?

Example 1:

Capacity Backpack m = 10, the size of the article A = [2, 3, 5, 7], the value of goods V = [1, 5, 2, 4].

Using the array to record the i-th article from the former, in the case where the capacity of the maximum value j can be taken:

image

DP [i] [j] denotes the i-th object before, in the case where the capacity of j, to the maximum value can take

If you take the i-th object, the value of dp [i - 1] [jA [i]] + V [i] (jA [i]> 0)

If the i-th object is not taken, the value of dp [i - 1] [j]

State transition: dp [i] [j] = max (dp [i - 1] [j - A [i]] + V [i], dp [i - 1] [j])

The other two sub-problems. In fact, in addition to the 01 backpack, knapsack problem we need to know - the full backpack and multiple knapsack problem, some of the rest are three kinds of deformation and combinations.

If you want to learn more thoroughly this knowledge, you can listen to " backpack Four Lectures ", the basics are covered and brush questions to the ~

Published 452 original articles · won praise 393 · views 190 000 +

Guess you like

Origin blog.csdn.net/JiuZhang_ninechapter/article/details/104075003