First, the topic
There are n items and a backpack with a capacity of V
I can use up to Mi pieces, each space is CI, the value is W
Solve what items are loaded into the backpack so that the total amount of space spent on these items does not exceed the backpack capacity, and the total value of the largest
Second, the basic algorithm
It's similar to a complete backpack problem.
The basic equation simply changes the equation of the complete knapsack problem slightly.
Because there are mi+1 strategies for article I:
Take 0 pieces, take 1 pieces ... Take Mi Pieces
Dp[i, V] indicates the maximum value of the first I object in a backpack with a capacity of V, then there is a state transition equation:
Dp[i, V] = max{dp[i? 1, v? k? Ci] + k? Wi | 0≤K≤MI}
Complexity of O (V *σmi)
3, converted to 01 knapsack problem
Another good way to write the basic method is to convert to 01 Knapsack solution:
Replace the item I in the Mi piece 01 backpack items, then get the number of items for Σmi 01 knapsack problem
Direct solution, the complexity is still O (V *σmi)
But we're looking to turn it into a 01 knapsack problem, to reduce complexity like a full backpack.
Still considering the idea of binary, we consider replacing item I with a number of items
Each strategy that makes the article I in the original question-take 0...Mi-can be equivalent to taking several items after substitution
In addition, the strategy to take more than MI pieces must not appear
The method is:
Divide item I into several 01 items in the backpack, each of which has a factor
The cost and value of this item are the original cost and value multiplied by this factor
These coefficients were 1, 2, 2^2 ... 2^ (k?1), Mi? 2^k + 1, and K is satisfied Mi? 2^k + 1 > 0 Maximum integer
For example, if the MI is 13, then the corresponding k = 3, the item with a maximum of 13 pieces should be divided into 2 items with coefficients of 1, 4, 6, four respectively
The coefficients of these items and for MI, which indicate that it is not possible to take more than MI pieces of article I items
In addition this method can also guarantee for 0 ... Each integer in MI can be represented by a number of coefficients
Here the proof of algorithm correctness can be divided into 0 ... 2k?1 and 2k ... Mi Two section to discuss separately
In this way, the article I is divided into O (logmi) items, the original problem into the complexity of O (vσlogmi) 01 knapsack problem, is a great improvement
The following is an O (LOGM) time process for handling items in a multiple backpack:
def multiplepack (DP, C, W, M)
If C. M≥v
Completepack (DP, C, W)
Return
K: = 1
While K < M
Zeroonepack (KC, KW)
M: = m? K
K: = 2k
Zeroonepack (C. M, W. M
Four, O (V * N) algorithm
Multi-knapsack problem also has O (V N) complexity algorithm
This algorithm is based on the state transfer equation of the basic algorithm
However, the method of applying a monotone queue allows the value of each state to be solved at a time of averaging O (1)
This method is on the "Eight Men" slide in the building.
Explanation of the monotone queue:
V. Summary
In this talk, we see the process of improving the complexity of an algorithm from O (vσmi) to O (Vσlogmi)
And I know the existence of an algorithm with an O (V N) complexity
Pay special attention to the idea and method of "splitting items", prove its correctness, and write out the complete program code
From "Backpack Nine v_2.0"%%% author, thank the author
Multiple Backpack 2016.5.8