问题描述:有n个物品,第 i 个物品的重量与价值分别为 w [ i ] w[i] w[i] 与 v [ i ] v[i] v[i]。背包容量为 V,试问在每个物品有无限个(物品必须保持完整)的情况下,如何让背包装入的物品具有更大的价值总和。现有数据如下:
w = [2,3,4,5];
v = [3,4,5,6];
V = 10;
解题思路:令 d p [ i ] [ j ] dp[i][j] dp[i][j] 表示从编号 1~i 的物品中挑选任意数量的任意物品放入容量为 j 的背包中得到的最大价值,那么有 d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j − k ∗ w [ i ] ] + k ∗ v [ i ] ∣ 0 ≤ k & k ∗ w [ i ] ≤ j } dp[i][j]=max\{dp[i-1][j-k*w[i]]+k*v[i]|0 \le k\&k*w[i] \le j\} dp[i][j]=max{dp[i−1][j−k∗w[i]]+k∗v[i]∣0≤k&k∗w[i]≤j}。根据题意,在背包容量不变的情况下,若增加一件物品仍然无法改变背包内的物品,那么最大价值仍然为 d p [ i − 1 ] [ j ] dp[i-1][j] dp[i−1][j],即 d p [ i ] [ j ] = d p [ i − 1 ] [ j ] dp[i][j]= dp[i-1][j] dp[i][j]=dp[i−1][j]。因此有 d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , d p [ i − 1 ] [ j − k ∗ w [ i ] ] + k ∗ v [ i ] ∣ 0 ≤ k & k ∗ w [ i ] ≤ j } dp[i][j]=max\{dp[i-1][j],dp[i-1][j-k*w[i]]+k*v[i]|0 \le k \&k*w[i] \le j\} dp[i][j]=max{dp[i−1][j],dp[i−1][j−k∗w[i]]+k∗v[i]∣0≤k&k∗w[i]≤j}。
public int knapsackProblem(int[] w, int[] v, int cap) {
int[][] dp = new int[w.length + 1][cap + 1];
for (int i = 1; i <= w.length; i++) {
for (int j = cap; j > 0; j--) {
dp[i][j] = dp[i - 1][j];
for (int k = 0; k * w[i - 1] <= j; k++) {
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - k * w[i - 1]] + k * v[i - 1]);
}
}
}
return dp[w.length][cap];
}
由以上代码可知,时间复杂度为 O ( n V 2 ) O(nV^2) O(nV2)。
时间优化:
d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , d p [ i − 1 ] [ j − k ∗ w [ i ] ] + k ∗ v [ i ] ∣ 0 ≤ k & k ∗ w [ i ] ≤ j } dp[i][j]=max\{dp[i-1][j],dp[i-1][j-k*w[i]]+k*v[i]|0 \le k\&k*w[i] \le j\} dp[i][j]=max{dp[i−1][j],dp[i−1][j−k∗w[i]]+k∗v[i]∣0≤k&k∗w[i]≤j}
d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , m a x { d p [ i − 1 ] [ ( j − w [ i ] ) − ( k − 1 ) ∗ w [ i ] ] + ( k − 1 ) ∗ v [ i ] ∣ 0 ≤ ( k − 1 ) ∗ w [ i ] ≤ j } + v [ i ] } dp[i][j]=max\{dp[i-1][j], max\{dp[i-1][(j-w[i])-(k-1)*w[i]]+(k-1)*v[i]|0 \le (k-1)*w[i] \le j\} + v[i]\} dp[i][j]=max{dp[i−1][j],max{dp[i−1][(j−w[i])−(k−1)∗w[i]]+(k−1)∗v[i]∣0≤(k−1)∗w[i]≤j}+v[i]}
d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , m a x { d p [ i − 1 ] [ ( j − w [ i ] ) − k ∗ w [ i ] ] + k ∗ v [ i ] ∣ 0 ≤ k & ( k + 1 ) ∗ w [ i ] ≤ j } + v [ i ] } dp[i][j]=max\{dp[i-1][j], max\{dp[i-1][(j-w[i])-k*w[i]]+k*v[i]|0 \le k \& (k+1)*w[i] \le j\}+v[i]\} dp[i][j]=max{dp[i−1][j],max{dp[i−1][(j−w[i])−k∗w[i]]+k∗v[i]∣0≤k&(k+1)∗w[i]≤j}+v[i]}
纵观上述表达式的第二项,有
d p [ i ] [ j − w [ i ] ] = m a x { d p [ i − 1 ] [ ( j − w [ i ] ) − k ∗ w [ i ] ] + k ∗ v [ i ] ∣ 0 ≤ k & ( k + 1 ) ∗ v [ i ] ≤ j } dp[i][j-w[i]]=max\{dp[i-1][(j-w[i])-k*w[i]]+k*v[i]|0 \le k \& (k+1)*v[i] \le j\} dp[i][j−w[i]]=max{dp[i−1][(j−w[i])−k∗w[i]]+k∗v[i]∣0≤k&(k+1)∗v[i]≤j}
进而 d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , d p [ i ] [ j − w [ i ] ] + v [ i ] } dp[i][j]=max\{dp[i-1][j], dp[i][j-w[i]]+v[i]\} dp[i][j]=max{dp[i−1][j],dp[i][j−w[i]]+v[i]}
空间优化:
d p [ i ] [ j ] = m a x { d p [ i − 1 ] [ j ] , d p [ i ] [ j − w [ i ] ] + v [ i ] } dp[i][j]=max\{dp[i-1][j], dp[i][j-w[i]]+v[i]\} dp[i][j]=max{dp[i−1][j],dp[i][j−w[i]]+v[i]} => d p [ j ] = m a x { d p [ j ] , d p [ j − w [ i ] ] + v [ i ] } , j = 1 , 2 , . . . c a p dp[j]=max\{dp[j], dp[j-w[i]]+v[i]\}, j=1,2,...cap dp[j]=max{dp[j],dp[j−w[i]]+v[i]},j=1,2,...cap
public int knapsackProblem(int[] w, int[] v, int cap) {
int[] dp = new int[cap + 1];
for (int i = 0; i < w.length; i++) {
for (int j = 1; j <= cap; j++) {
if (w[i] <= j) {
dp[j] = Math.max(dp[j], dp[j - w[i]] + v[i]);
}
}
}
return dp[cap];
}
时间复杂度为 O ( n V ) O(nV) O(nV)。
上一篇:背包问题之0-1背包问题
下一篇:背包问题之多重背包问题