-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class BagZeroOne {
-
-
-
-
- @param
- @param
- @param
- @param
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void knapsack(int[] v, int[] w, int c, int[][] m) {
- int n = v.length - 1;
- int jMax = Math.min(w[n] - 1, c);
- for (int j = 0; j <= jMax; j++) {
- m[n][j] = 0;
- }
- for (int j = w[n]; j <= c; j++) {
- m[n][j] = v[n];
- }
- for (int i = n - 1; i > 0; i--) {
- jMax = Math.min(w[i] - 1, c);
- for (int j = 0; j <= jMax; j++) {
- m[i][j] = m[i + 1][j];
- }
- for (int j = w[i]; j <= c; j++) {
- m[i][j] = Math.max(m[i + 1][j], m[i + 1][j - w[i]] + v[i]);
- }
- }
-
-
-
-
-
-
- }
-
-
- @param
- @param
- @param
- @param
-
- 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[i][c] == m[i + 1][c]) {
- x[i] = 0;
- } else {
- x[i] = 1;
- c -= w[i];
- }
- }
- x[n] = (m[n][c] > 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[w.length][c + 1];
- int[] x = new int[w.length];
-
-
- knapsack(v, w, c, m);
- trackback(m, w, c, x);
-
- if (m[1][c] == expectedVMax && java.util.Arrays.equals(x, expectedX)) {
- System.out.println("Test success!");
- } else {
- System.out.println("Test fail!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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) {
-
- if (tw + w[i] <= c) {
- opt[i] = 1;
- if (i < w.length - 1) {
- find(i + 1, tw + w[i], tv);
- } else {
- for (int j = 0; j < x.length; j++) {
- x[j] = opt[j];
- }
- maxv = tv;
- }
- opt[i] = 0;
- }
-
- if (tv - v[i] > maxv) {
- if (i < w.length - 1) {
- find(i + 1, tw, tv - v[i]);
- } else {
- for (int j = 0; j < x.length; j++) {
- x[j] = opt[j];
- }
- maxv = tv - v[i];
- }
- }
- }
-
- 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[w.length];
- opt = new int[w.length];
- c = 7;
- int tv = 0;
- for (int i : v) {
- tv += i;
- }
-
- find(0, 0, tv);
-
-
- if (maxv == expectedMaxV && java.util.Arrays.equals(x, expectedX)) {
- System.out.println("Test success!");
- } else {
- System.out.println("Test fail!");
- }
- }
-
-
-
-
-
-
-
-
-
-
- private static int[] flag;
- private static int[] twe;
- private static int[] tve;
-
- private static int maxw;
- private static int[] cop;
-
-
- private static void next(int i, int tw, int tv) {
- flag[i] = 1;
- twe[i] = tw;
- tve[i] = 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[i];
- tw = twe[i];
- tv = tve[i];
- switch (f) {
- case 0:
- i--;
- break;
-
- case 1:
- flag[i]++;
- if (tw + w[i] <= maxw) {
- if (i < n - 1) {
- next(i + 1, tw + w[i], tv);
- i++;
- } else {
- maxv = tv;
- for (k = 0; k < n; k++) {
- cop[k] = ((flag[k] != 0)? 1 : 0);
- }
- }
- }
- break;
-
- default:
- flag[i] = 0;
- if (tv - v[i] > maxv) {
- if (i < n - 1) {
- next(i + 1, tw, tv - v[i]);
- i++;
- } else {
- maxv = tv - v[i];
- for (k = 0; k < n; k++) {
- cop[k] = ((flag[k] != 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[n];
-
- flag = new int[n];
- twe = new int[n];
- tve = new int[n];
-
- maxw = 7;
-
- int maxv = find(w, v, n);
-
-
- 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();
- }
- }
|