I recently read about the 9-story backpack. We need to carefully study this kind of White Paper. Because there are some thinking jumps in it, we need to add our own understanding on the basis of the original article.
Question
There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.
Basic Ideas
This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.
Define the state with a subproblem: that is, F [I] [v] indicates the maximum value that a backpack with a capacity of V can obtain when the first I item is placed. The state transition equation is:
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of V, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "before the I-1 items into the capacity of V in the backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity V-C [I ", the greatest value that can be obtained at this time is f [I-1] [V-C [I] plus the value W [I] obtained by placing the I item.
Optimize space complexity
The time and space complexity of the above method are O (VN). The time complexity should no longer be optimized, but the space complexity can be optimized to O.
First, consider how to implement the basic idea mentioned above. There must be a main loop I = 1 .. n, two-dimensional array f [I] [0 .. all values of V. Then, if only one array f [0 .. v]. can we ensure that f [I] [v] represents the state we defined after the end of the I-th loop? F [I] [v] is derived from two subproblems: F [I-1] [v] and f [I-1] [V-C [I, can we ensure that f [I] [v] is pushed (that is, when f [v] is pushed in the I Main Loop) can we get the values of F [I-1] [v] and f [I-1] [V-C [I? In fact, this requires that in each main loop we use v = V .. 0 in order. f [v], this ensures that f [v] f [V-C [I] saves the value of State f [I-1] [V-C [I. The pseudocode is as follows:
for i=1..N
for v=V..0
f[v]=max{f[v],f[v-c[i]]+w[i]};
F [v] = max {f [v], F [V-C [I]} is equivalent to our transfer equation.f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]}
Because the current F [V-C [I] is equivalent to the original f [I-1] [V-C [I]. If we change the circular order of V from the forward order to the order, then f [I] [v] is deduced by F [I] [V-C [I, it is not consistent with the meaning of this question, but it is another important backpack problem p02 is the most simple solution, so it is necessary to learn to solve the problem of 01 backpack with only one-dimensional array. (Why reverse order? In reverse order, for example, after I = 1 is calculated in an External Loop, all f [0... v] has a value, so when the External Loop enters I = 2, the maximum F [v] = max {f [v], f [V-C [2] + W [I]}, the f [V-C [2] used in this case is not the f [V-C [I] obtained in the previous loop I = 1, that is, F [I-1] [V-C [I], so the reverse order can correspond to the two-dimensional formula. That is, V-C [I] is not executed yet. Therefore, f [V-C [I] stores the results of the first I-1 loop. If the sequence is used, F [0... v] the gradual update is f [I] [V-C [I], instead of F [I-1] [V-C [I], so the reverse order is required here .)
In fact, the program that uses a one-dimensional array to solve the 01 backpack will be used multiple times later, so here we abstract a process to process an item in the 01 backpack, direct calls in future Code are not described.
Zeroonepack indicates processing an item in a 01 backpack. The two parameters cost and weight indicate the cost and value of this item respectively.
procedure ZeroOnePack(cost,weight)
for v=V..cost
f[v]=max{f[v],f[v-cost]+weight}
Note that the processing in this process is different from the pseudo code given above. The preceding example program v = V .. 0 is used to show that every state in the program is solved according to the equation, avoiding unnecessary complexity of thinking. Now that the process has been abstracted as a black box, you can add optimization. Items whose cost is cost will not affect the status f [0 .. cost-1], which is obvious.
With this process, you can write the pseudocode for the 01 backpack problem as follows:
for i=1..N
ZeroOnePack(c[i],w[i]);
Initialization details
We can see that there are actually two different ways to solve the problem of a backpack. Some questions require the optimal solution when "just full of backpacks", and some questions do not require that the backpack be full. One difference is that the implementation methods of these two questions are different during initialization.
If the first method is used, it is required to be filled with a backpack. during initialization, F [0] is 0, and f [1 .. v] is set to-∞, which ensures that the final f [N] is an optimal solution just filled with a backpack.
If you do not need to fill your backpack, but only want the price to be as high as possible, you should set all f [0.. V] to 0 during initialization.
Why? It can be understood as follows: The initialized F array is actually the legal status when no item can be put into a backpack. If a backpack is required to be filled, only a backpack with a capacity of 0 may be filled with nothing with a value of 0. There is no legal solution for a backpack with other capacities, for undefined states, their values should all be-∞. If the backpack does not have to be filled, then any capacity of the backpack has a legal solution: "Nothing is loaded." the value of this solution is 0, therefore, the initial status values are all 0.
This tips can be fully applied to other types of backpack problems, so we will not explain the initialization before the status transfer.
Constant Optimization
In the preceding pseudo code, for V = V .. 1 can be used to improve the lower limit of the loop.
Because only the value of F [N] corresponding to the last N is required, F [0... v], in fact, as long as you know f [V-C [N]; corresponding to find the N-1 of F [V-C [N, you only need to know the N-2 f [V-C [N]-C [n-1. Similarly, for the J-ID backpack, you only need to know f [V-sum {C [J .. n]}], that is, in the code
for i=1..N
for v=V..0
Can be changed
for i=1..n
bound=max{V-sum{c[i..n]},c[i]}
for v=V..bound
This is useful when V is large.
Backpack 9 lecture 01 backpack (zeroonepack)