[Speaking algorithm class] DP solves 01 knapsack problem (under the condition that the total weight is exactly the knapsack capacity W)

Insert picture description here

01 Knapsack problem
There are n items whose weight (or space occupied) are w_1, w_2,..., w_n, and their values ​​are v_1, v_2,..., v_n.
Given a backpack with a total capacity of W, each item can only be put into the backpack or left alone.
Question: How to choose the items to put in the backpack so that the total weight of the items in the backpack is exactly W and the total value is the largest?
Solution
dynamic programming (DP).
The decision-making process is divided into n stages, and each stage examines whether an item is selected.
Deciding whether to choose an item depends on whether other conditions remain the same and the backpack capacity is smaller.
Examine items 1 to n in turn. When inspecting item i, it means that whether the choice of the first i item has been decided has been completed.
If item 1 is not selected, the problem becomes a sub-problem: only the remaining items 2, 3,..., n can be selected, and the 01 backpack problem with the capacity still W.
If item 1 is selected, the problem becomes a sub-problem: only the remaining items 2, 3,..., n can be selected, and the 01 knapsack problem with a capacity of W-w_1.
For other items, there are similar conversions (note that the backpack capacity W should be replaced with the corresponding remaining capacity).
According to this substructure and recurrence relationship, it can be found that when investigating whether to select an item i, it is necessary to sequentially investigate whether the item is selected during the process of the backpack capacity (integer) from 0 to the current remaining capacity. According to this idea, consider all n items again, and the result will come out.
Therefore, a two-dimensional DP array d_(i, r) is constructed, which indicates that the remaining capacity of the backpack is r, and the optimal value of the items loaded in the backpack has been considered after items 1 to i have been considered. The state transition equation will be listed soon:
█(d_(i, 0)=0#(1))
█(d_(0, r)=0#(2))
█(d_(i, r)=d_ (i-1, r),r<w_i#(3))
█(d_(i, r)=max⁡(d_(i-1, r), d_(i-1, r-w_i )+v_i ),r≥w_i#(4))
Obviously equation (1)(2 ) Are boundary conditions, and the recursion starts from these two conditions.
Equation (3) is for the situation where the remaining capacity of the backpack can no longer fit the item i. At this time, item i can only be selected, and the total value of the items in the backpack remains unchanged.
Equation (4) is aimed at the situation where the remaining capacity of the backpack can satisfy the item i. At this time, you can choose item i or not item i. Given the optimal solution with other conditions unchanged and a smaller backpack capacity, the optimal sub-problem under consideration (the backpack capacity is the current capacity r) The solution is determined by whether to choose this item i, so it depends on which of these two choices can bring greater total value.
The DP matrix can be constructed through double loops. After the loop is over, d_(n, w) stores the optimal solution.

Guess you like

Origin blog.csdn.net/COFACTOR/article/details/111600478