|
| 1 | +package com.program.dynamic.programming; |
| 2 | + |
| 3 | +public class Knapsack { |
| 4 | + |
| 5 | + private static int table[][]; |
| 6 | + private static int n; |
| 7 | + /** |
| 8 | + * Time : O(N * C ) |
| 9 | + * Space : O (N * C) |
| 10 | + * @param capacity |
| 11 | + * @param w |
| 12 | + * @param v |
| 13 | + * @return |
| 14 | + */ |
| 15 | + public static int knapsackSol(int capacity, int [] w, int v []) { |
| 16 | + |
| 17 | + table = new int[v.length+1][capacity+1]; |
| 18 | + n = w.length; |
| 19 | + |
| 20 | + return knapsackDP(0, capacity, w, v); |
| 21 | + } |
| 22 | + |
| 23 | + static int max(int a, int b) { |
| 24 | + return a > b ? a : b; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Recursive Solution. |
| 29 | + * |
| 30 | + * Time Complexity : O(2^n). |
| 31 | + * @param w |
| 32 | + * @param wt |
| 33 | + * @param val |
| 34 | + * @param n |
| 35 | + * @return |
| 36 | + */ |
| 37 | + public static int knapsack(int w, int wt[], int val[], int n) { |
| 38 | + |
| 39 | + if (w == 0 || n == 0) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + if (wt[n-1] > w) { |
| 44 | + return knapSack(w, wt, val, n-1); |
| 45 | + } else { |
| 46 | + return max(val[n-1] + knapSack(w - wt[n-1], wt, val, n-1), |
| 47 | + knapsack(w, wt, val, n-1)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static int knapsackDP(int i, int w, int [] wt, int []v){ |
| 52 | + |
| 53 | + if (i >= n) { |
| 54 | + table[i][w] = 0; |
| 55 | + return table[i][w]; |
| 56 | + } |
| 57 | + |
| 58 | + if (wt[i] > w) |
| 59 | + return table[i+1][w] = knapsackDP(i + 1, w, wt, v); |
| 60 | + else |
| 61 | + return table[i+1][w] = max(knapsackDP(i + 1, w, wt, v), knapsackDP(i + 1, w - wt[i], wt, v) + v[i]); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + // Returns the maximum value that can be put in a knapsack of capacity W |
| 66 | + static int knapSack(int W, int wt[], int val[], int n) { |
| 67 | + |
| 68 | + int i, w; |
| 69 | + int table[][] = new int[n+1][W+1]; |
| 70 | + |
| 71 | + // Build table K[][] in bottom up manner |
| 72 | + for (i = 0; i <= n; i++) { |
| 73 | + for (w = 0; w <= W; w++) |
| 74 | + { |
| 75 | + if (i==0 || w==0) |
| 76 | + table[i][w] = 0; |
| 77 | + else if (wt[i-1] <= w) |
| 78 | + table[i][w] = max(val[i-1] + table[i-1][w-wt[i-1]], table[i-1][w]); |
| 79 | + else |
| 80 | + table[i][w] = table[i-1][w]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return table[n][W]; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + public static void main(String[] args) { |
| 90 | + int weight[] = new int[7]; |
| 91 | + weight[0] = 9; |
| 92 | + weight[1] = 6; |
| 93 | + weight[2] = 1; |
| 94 | + weight[3] = 2; |
| 95 | + weight[4] = 5; |
| 96 | + weight[5] = 4; |
| 97 | + |
| 98 | + int value[] = new int[7]; |
| 99 | + value[0] = 11; |
| 100 | + value[1] = 10; |
| 101 | + value[2] = 5; |
| 102 | + value[3] = 7; |
| 103 | + value[4] = 12; |
| 104 | + value[5] = 8; |
| 105 | + |
| 106 | + /* |
| 107 | + * Set all values of 2D matrix CostTable to Minus 1 |
| 108 | + */ |
| 109 | + int capacity = 10; |
| 110 | + System.out.println(knapsackSol(capacity, weight, value)); |
| 111 | + |
| 112 | + System.out.println(knapSack(capacity, weight, value, weight.length)); |
| 113 | + |
| 114 | + System.out.println( knapsack(capacity, weight, value, weight.length-1)); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments