[英]Knapsack but exact weight
Is there a algorithm to determine a knapsack which has an exact weight W? 是否有算法来确定具有精确重量W的背包? Ie it's like the normal 0/1 knapsack problem with n items each having weight w_i and value v_i.
也就是说,正常的0/1背包问题是n个项目各有权重w_i和值v_i。 Maximise the value of all the items, however the total weight of the items in the knapsack need to have exactly weight W !
最大化所有物品的价值,但背包中物品的总重量需要具有正确的重量W !
I know the "normal" 0/1 knapsack algorithm but this could also return a knapsack with less weight but higher value. 我知道“普通”0/1背包算法,但这也可以返回一个重量更轻但价值更高的背包。 I want to find the highest value but exact W weight.
我想找到最高值但确切的W重量。
Here is my 0/1 knapsack implementation: 这是我的0/1背包实现:
public class KnapSackTest {
public static void main(String[] args) {
int[] w = new int[] {4, 1, 5, 8, 3, 9, 2}; //weights
int[] v = new int[] {2, 12, 8, 9, 3, 4, 3}; //values
int n = w.length;
int W = 15; // W (max weight)
int[][] DP = new int[n+1][W+1];
for(int i = 1; i < n+1; i++) {
for(int j = 0; j < W+1; j++) {
if(i == 0 || j == 0) {
DP[i][j] = 0;
} else if (j - w[i-1] >= 0) {
DP[i][j] = Math.max(DP[i-1][j], DP[i-1][j - w[i-1]] + v[i-1]);
} else {
DP[i][j] = DP[i-1][j];
}
}
}
System.out.println("Result: " + DP[n][W]);
}
}
This gives me: 这给了我:
Result: 29
(Just ask if anything is unclear in my question!) (请问我的问题中是否有任何不清楚的地方!)
By simply setting DP[i][j] = -infinity
in your last else
clause it will do the trick. 只需在你的最后一个
else
子句中设置DP[i][j] = -infinity
就可以了。
The ides behind it is to slightly change the recursive formula definition to calculate: 它背后的想法是稍微改变递归公式定义来计算:
j
up to item i
. j
的最大值直到项目i
。 Now, the induction hypothesis will change, and the proof of correctness will be very similar to regular knapsack with the following modification: 现在,感应假设会发生变化,正确性的证明与常规背包非常相似,具有以下修改:
DP[i][j-weight[i]] is now the maximal value that can be constructed with exactly j-weight[i]
, and you can either take item i
, giving value of DP[i][j-weight[i]]
, or not taking it, giving value of DP[i-1][j]
- which is the maximal value when using exactly weight j
with first i-1
items. DP [i] [j-weight [i]]现在是可以用
j-weight[i]
构造的最大值,你可以取项目i
,给出DP[i][j-weight[i]]
,或不接受它,给出DP[i-1][j]
的值 - 这是当使用精确权重j
与第一个i-1
项目时的最大值。
Note that if for some reason you cannot construct DP[i][j]
, you will never use it, as the value -infinity will always discarded when looking for MAX. 请注意,如果由于某种原因你不能构造
DP[i][j]
,你将永远不会使用它,因为在查找MAX时总是会丢弃-infinity值。
Actually, the accepted answer is wrong, as found by @Shinchan in the comments. 实际上,接受的答案是错误的,正如@Shinchan在评论中所发现的那样。
You get exact weight knapsack by changing only the initial dp
state, not the algorithm itself. 通过仅更改初始
dp
状态而不是算法本身,您可以获得精确的重量背包。
The initialization, instead of: 初始化,而不是:
if(i == 0 || j == 0) {
DP[i][j] = 0;
}
should be: 应该:
if (j == 0) {
DP[i][j] = 0;
} else if (i == 0 && j > 0) { // obviously `&& j > 0` is not needed, but for clarity
DP[i][j] = -inf;
}
The rest stays as in your question. 剩下的就像你的问题一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.