0-1背包—DP、暴力、贪心
最简单地:v是空间,w是价值,要求总价值最大 dp = max {dp] + w*};//自顶向下; //自底向上
- /**********************************************************
-
-
0-1背包问题
-
- *
-
-
问题描述:
-
-
-
给定n种物品和一背包。物品 i 的重量是 w* ,其价值为
-
-
-
v* ,背包的容量为 c 。问:应该如何选择装入背包中的物品,
-
-
-
使得装入背包中物品的总价值最大?
-
-
-
-
在选择装入背包中的物品时,对每种物品 i 只有两种选择,
-
-
-
即装入或不装入背包。不能将物品 i 装入背包多次,也不能只
-
-
-
装入部分的物品 i 。因此,该问题称为 0-1 背包问题。
-
- *
-
-
此问题的形式化描述为,给定 c > 0, w* > 0, v* > 0
-
-
-
1 <= i <= n ,要求找出 n 元 0-1 向量 x, 其中x*
-
-
-
等于0或1,使得对 w* * x* 求和小于等于 c ,并且 v* *
-
-
-
x*达到最大。因此,0-1背包问题是一个特殊的整数规划问题。
-
- *
- ***********************************************************/
- public class BagZeroOne {
-
/********************************************************************** -
* 动态规划解 (Dynamic Programming) -
* -
* @param v in the 物品价值数组 -
* @param w in the 物品重量数组 -
* @param c in the 背包的容量 -
* @param m out the 最优值数组,m*即背包容量为j,可选物品为 i, i + 1, ... , n 时0-1背包问题的最优值 -
* -
* 由于0-1背包问题的最优子结构性质,可以建立计算m*的递归式如下: -
* / max{m), m] + v*} j >= w* -
* m* / -
* \ -
* \ m 0 <= j < w* -
* -
* / v j >= w -
* m / -
* \ -
* \ 0 0 <= j < w -
* -
**********************************************************************/ -
public static void knapsack(int v, int w, int c, int m) { -
int n = v.length - 1; -
int jMax = Math.min(w - 1, c); -
for (int j = 0; j <= jMax; j++) { -
m = 0; -
} -
for (int j = w; j <= c; j++) { -
m = v; -
} -
for (int i = n - 1; i > 0; i--) { -
jMax = Math.min(w* - 1, c); -
for (int j = 0; j <= jMax; j++) { -
m* = m; -
} -
for (int j = w*; j <= c; j++) { -
m* = Math.max(m, m] + v*); -
} -
} -
/* -
m = m; -
if (c >= w) { -
m = Math.max(m, m] + v); -
} -
*/ -
} -
/** -
* @param m in the 最优值数组 -
* @param w in the 重量数组 -
* @param c in the 背包容量 -
* @param x out the 物品选择数组 if x* == 1 则选物品 i, 否则不选 -
**/ -
public static void trackback(int m, int w, int c, int x) { -
int n = w.length - 1; -
for (int i = 1; i < n; i++) { -
if (m* == m) { -
x* = 0; //不选物品 i -
} else { -
x* = 1; //选择物品 i -
c -= w*; //剩余容量 -
} -
} -
x = (m > 0)? 1: 0; -
} -
public static void testDynamicProgramming() { -
System.out.print("1. --- testDynamicProgramming ---> "); -
//输入 -
int c = 7; -
int w = {0, 5, 3, 2, 1}; -
int v = {0, 4, 4, 3, 1}; -
//应该的输出 -
int expectedVMax = 8; -
int expectedX = {0, 0, 1, 1, 1}; -
//程序运行时变量 -
int m = new int; -
int x = new int; -
knapsack(v, w, c, m); -
trackback(m, w, c, x); -
if (m == expectedVMax && java.util.Arrays.equals(x, expectedX)) { -
System.out.println("Test success!"); -
} else { -
System.out.println("Test fail!"); -
} -
} -
/****************************************************************************** -
* 暴力解 (Brutal Force) -
* -
* 物品 i 的重量 w*, 价值 v* -
* -
* 递归算法 -
* try (物品 i, 当前选择已经达到的重量之和 tw, 本方案可能达到的总价值 tv) -
* { -
* //考虑物品 i 包含在当前方案的可能性 -
* if (包含物品 i 是可接受的) -
* { -
* 将物品 i 包含在当前方案中; -
* if (i < n - 1) -
* try(i + 1, tw + w*, tv); -
* else //又是一个完整方案,因它比前面的方案要好,以它作为最佳方案 -
* 以当前方案作为临时最佳方案保存 -
* 恢复物品 i 不包含在内的状态 -
* } -
* //考虑物品 i 不包含在当前方案中的可能性 -
* if (不包含物品 i 仅是可考虑的) -
* { -
* if (i < n - 1) -
* try(i + 1, tw, tv - v*); -
* else //又是一个完整方案,因它比前面的方案要好,以它作为最佳方案 -
* 以当前方案作为临时最佳方案保存 -
* } -
* } -
******************************************************************************/ -
private static int w; //重量 -
private static int v; //价值 -
private static int x; //最优解 -
private static int opt; //有效解 -
private static int c; //背包容量 -
private static int maxv; //最优值 -
public static void find(int i, int tw, int tv) { -
//考虑物品 i 包含在当前方案中的可能性 -
if (tw + w* <= c) { //包含物品 i 是可以接受的 -
opt* = 1; -
if (i < w.length - 1) { -
find(i + 1, tw + w*, tv); -
} else { //又是一个完整方案,因它比前面的方案好,以它作为最佳方案 -
for (int j = 0; j < x.length; j++) { -
x = opt; -
} -
maxv = tv; -
} -
opt* = 0; -
} -
//考虑物品 i 不包含在当前方案中的可能性 -
if (tv - v* > maxv) { //不包含物品 i 是可以考虑的 -
if (i < w.length - 1) { -
find(i + 1, tw, tv - v*); -
} else { //又是一个完整方案,因它比前面的方案好,以它作为最佳方案 -
for (int j = 0; j < x.length; j++) { -
x = opt; -
} -
maxv = tv - v*; -
} -
} -
} -
public static void testBrutalForceRecursive() { -
System.out.print("2. --- testBrutalForceRecursive ---> "); -
int expectedX = {0, 1, 1, 1}; -
int expectedMaxV = 8; -
w = new int {5, 3, 2, 1}; -
v = new int {4, 4, 3, 1}; -
x = new int; -
opt = new int; -
c = 7; -
int tv = 0; -
for (int i : v) { -
tv += i; -
} -
find(0, 0, tv); - // System.out.println("maxv = " + maxv);
- // System.out.println("x = " + java.util.Arrays.toString(x));
-
if (maxv == expectedMaxV && java.util.Arrays.equals(x, expectedX)) { -
System.out.println("Test success!"); -
} else { -
System.out.println("Test fail!"); -
} -
} -
/**************************************************************** -
* 暴力解 (Brutal Force) -
* -
* 非递归算法 -
* -
* -
*****************************************************************/ -
//当前候选解中各物品的考虑和选择状态,以及置该物品候选解的状态 -
private static int flag; //物品的考虑状态:0.不选;1.将被考虑;2.曾被选中 -
private static int twe; //已经达到的总重量 -
private static int tve; //期望的总价值 -
private static int maxw; //背包容量 -
private static int cop; //临时最佳解的物品选择方案,当cop* 为 1 时,物品 i 在解中 -
//将考虑物品 i -
private static void next(int i, int tw, int tv) { -
flag* = 1; -
twe* = tw; -
tve* = tv; -
} -
public static int find(int w, int v, int n) { -
int i, k, f; -
int maxv, tw, tv, totv = 0; -
maxv = 0; -
for (int value : v) { -
totv += value; -
} -
next(0, 0, totv); -
i = 0; -
while (i >= 0) { -
f = flag*; -
tw = twe*; -
tv = tve*; -
switch (f) { -
case 0: //回退 -
i--; -
break; -
case 1: //考虑被选中 -
flag*++; -
if (tw + w* <= maxw) { //选中可行吗? -
if (i < n - 1) { -
next(i + 1, tw + w*, tv); -
i++; -
} else { -
maxv = tv; -
for (k = 0; k < n; k++) { -
cop = ((flag != 0)? 1 : 0); -
} -
} -
} -
break; -
default: //flag等于2 -
flag* = 0; -
if (tv - v* > maxv) { //不选物品 i 可行吗? -
if (i < n - 1) { -
next(i + 1, tw, tv - v*); -
i++; -
} else { -
maxv = tv - v*; -
for (k = 0; k < n; k++) { -
cop = ((flag != 0)? 1 : 0); -
} -
} -
} -
break; -
} -
} -
return maxv; -
} -
public static void testBrutalForceNotRecursive() { -
System.out.print("3. --- testBrutalForceNotRecursive ---> "); -
int expectedX = {0, 1, 1, 1}; -
int expectedMaxV = 8; -
int w = new int {5, 3, 2, 1}; -
int v = new int {4, 4, 3, 1}; -
int n = w.length; -
cop = new int; -
flag = new int; -
twe = new int; -
tve = new int; -
maxw = 7; -
int maxv = find(w, v, n); - // System.out.println("maxv = " + maxv);
- // System.out.println("x = " + java.util.Arrays.toString(x));
-
if (maxv == expectedMaxV && java.util.Arrays.equals(cop, expectedX)) { -
System.out.println("Test success!"); -
} else { -
System.out.println("Test fail!"); -
} -
} -
public static void main(String args) { -
testDynamicProgramming(); -
testBrutalForceRecursive(); -
testBrutalForceNotRecursive(); -
} - }