0/1 knapsack problems (Dynamic Planning) and knapsack Planning
0/1 backpack problems:
There are n kinds of items, for 1 <= I <= n, we know that the weight of the I-th item is positive integer Wi, the value is positive integer Vi, and the maximum carrying capacity of the backpack is positive integer W, we need to find a subset of the n items so that the total weight of the items in the sub-set cannot exceed W and the total value is as large as possible. (Note: you are not allowed to retrieve all or all of each item. You are not allowed to retrieve only some of the items)
According to the problem description, you can convert it into the following constraints and target functions:
Therefore, the problem is to find a solution vector that satisfies the constraint (1) and maximizes the target function formula (2.
The 0-1 backpack problem can be viewed as a sequence in which to determine whether to = 1 or = 0 for any variable. after the judgment, it has been determined. There are two situations in the judgment:
(1) If the size of the backpack is insufficient for item I, it is equal to 0. The value of the backpack does not increase;
(2) The size of a backpack can be loaded with I, which is equal to 1. The value of a backpack increases.
In both cases, the owner of the total value of the backpack should be the value after judgment. To indicate the total value of items that can be loaded with a backpack with a size of j in the first item, the following dynamic planning function can be obtained:
Type (1) Description: load the first I items into a backpack with a capacity of 0 and a backpack with a capacity of j. The value obtained is 0. formula 2: if the weight of the first item is greater than the size of the backpack, then the maximum value obtained by loading item I is the same as the maximum value obtained by loading item I-1, that is, item I cannot be loaded into the backpack; the second sub-statement: if the Weight of item I is smaller than the size of the backpack, there will be two situations: (1) if the item I is loaded into the backpack, the value of the items in the backpack is equal to the value of the first I-1 into the capacity of the backpack plus the value of the first item; (2) if the first item is not loaded into the backpack, the value of the items in the backpack is equal to the value obtained by loading the previous I-1 items into the backpack with capacity j. Obviously, those with greater value in the two are used as the optimal solution for loading the first I item into a backpack with a capacity of j.
We can work out the solutions we need step by step. The first step is to load only the first item and determine the maximum value that the backpack can obtain in various situations. The second step is to load only the first two items, determine the maximum value that a backpack can obtain in various situations; and so on, we get the optimal solution we need at Step n. Finally, it is the maximum value gained when n items are loaded into a backpack with a capacity of W. In order to determine the specific items to be loaded into the backpack, search forward from the value. If>, the nth item is loaded into the backpack, and the first n-1 items are loaded into the backpack with the capacity; otherwise, the nth item is not loaded into the backpack, and the first n-1 item is loaded into the backpack with a capacity of W. So forth until you determine whether the first item is loaded into your backpack. Therefore, we can obtain the following functions :.
According to the dynamic planning function, a two-dimensional array C is used to store the intermediate variable, which indicates the maximum value obtained by loading the first I item into a backpack with a capacity of j.
The following is an example
Suppose the maximum capacity M = 10, number of items N = 3, item size w {3, 4, 5}, item value p {4, 5, 6 }.
From the size of the backpack to 0, the first test of item 1, 0, 1, 2, cannot be placed. therefore, set 0 and put 4 in 3 for the backpack capacity. in this way, the size of this backpack is 4, 5, 6 ,.... when 10, the best solution is to put 4. assume that item 1 is placed in a backpack. then let's look at item 2 again. when the size of the backpack is 3, the best solution is the best price solution in the last row. c is 4. when the backpack capacity is 5, the best solution is 5 of its weight. when the backpack capacity is 7, it is obviously 5 plus a value. Who should I add ?? Obviously, when 7-4 = 3, the optimal solution for the previous c3 row is 4. So. In general, the best solution is 5 + 4 for 9. In this way, a row is pushed down. The rightmost decentralized data is the greatest value. (Note that when the size of the 3rd-row backpack is 7, the best solution is not its own 6. but 9 in the last row. this indicates that item 3 is not selected at this time. item 1 and 2 are selected. so 9 .)
Code:
# Include <iostream> # include <algorithm> # include <vector> # include <string. h> # include <ctype. h> # include <math. h> using namespace std; const int n = 3; // number of items const int W = 10; // backpack capacity int c [n + 1] [W + 1]; void DP (int v [], int w []) {int I, j; for (int I = 1; I <= n; I ++) {for (int j = 1; j <= W; j ++) {if (w [I]> j) c [I] [j] = c [I-1] [j]; elsec [I] [j] = max (c [I-1] [j-w [I] + v [I], c [I-1] [j]);} int main () {int w [4] = {,}; int v [4] = {,}; memset (c, 0, sizeof (c); DP (v, w); for (int I = 0; I <= n; I ++) {for (int j = 0; j <= W; j ++) printf ("%-3d", c [I] [j]); puts ("");}}
Running result
Two-dimensional array storage can be used to optimize the space.
In c [0.. j], c [j] indicates the value of placing the first I item in a backpack with a size of j. From 1 ~ After n (n pieces) loops, f [j] indicates the maximum value.
* Here c [j] is equivalent to the c [I] [j] of the two arrays. So how to get c [I-1] [j] and c [I-1] [j-w [I] + w [j]? (Important! Thinking)
First of all, we need to know that the first I items are saved in order through the I from 1 to n loop. That is, for I = 1 .. N
Now I want to think about how c [j] means that the current State is the benefit of a backpack with a capacity of j, what makes c [j] and c [j-w [I] + w [I] represent the value of the previous State?
Reverse Order!
# Include <iostream> # include <algorithm> # include <vector> # include <string. h> # include <ctype. h> # include <math. h> using namespace std; const int n = 3; // number of items const int W = 10; // backpack capacity int c [W + 1]; void DP (int v [], int w []) {int I, j; for (int I = 1; I <= n; I ++) {for (int j = W; j> = 0; j --) {if (j> = w [I]) c [j] = max (c [j-w [I] + v [I], c [j]); printf ("%-3d ", c [j]) ;}cout <endl ;}int main () {int w [4] = {,}; int v [4] =, 5, 6}; memset (c, 0, sizeof (c); DP (v, w );}