|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | /*
|
7 |
| -In the 0/1 Knapsack Problem we must include an item entirely or exclude the item with an objective of maximizing profit/value constrainted by the size of the knapsack. |
| 7 | +In the 0/1 Knapsack Problem we must include an item entirely or exclude the item with an objective |
| 8 | +of maximizing profit/value constrainted by the size of the knapsack. |
8 | 9 |
|
9 | 10 | If the problem is not 0/1 knapsack and we are allowed to split items, we can take a greedy approach
|
10 | 11 | we can chose items by sorting items according to profit per unit weight.
|
@@ -39,12 +40,52 @@ For each item we are exploring the two choices(We include and exclude each item)
|
39 | 40 | DYNAMIC PROGRAMMING - BOTTOM UP APPROACH
|
40 | 41 | */
|
41 | 42 |
|
42 |
| -public int knapsack(int W, int item, int wt[], int val[]){ |
43 |
| - int k[][] = new int[item+1][W+1]; |
| 43 | +public int knapsack(int W, int items, int wt[], int val[]){ |
| 44 | + int k[][] = new int[items+1][W+1]; |
44 | 45 |
|
45 |
| - for(int i=0;i<=item;i++){ |
46 |
| - for(int j=0) |
| 46 | + for(int item=0 ; item <= items ; item++){ |
| 47 | + for(int w=0 ;w <= W ; w++){ |
| 48 | + //Base Case: when items = 0 or capacity of knapsack = 0 |
| 49 | + if(item==0 || w==0) k[item][w] = 0; |
| 50 | + |
| 51 | + //if weight of current item exceeds the knapsack capacity we EXCLUDE current item |
| 52 | + //Simply put the value which was calculated with respect to previous item and same knapsack capacity |
| 53 | + else if(wt[item-1] > w) k[item][w] = k[item-1][w]; |
| 54 | + |
| 55 | + //If the weight of item does not exceed knapsack's capacity - we explore 2 choices |
| 56 | + //1. INCLUDE current item - we find the cell that represents a state which has been calculated with respect to the |
| 57 | + //previous item and knapsack's capacity reduced by current item's weight ie, k[item-1][w-wt[item-1]] |
| 58 | + //2. EXCLUDE current item - value same as the one which was calculated |
| 59 | + //with respect to previous item and same knapsack capacity |
| 60 | + else k[item][w] = Math.max( k[item-1][w], val[item-1] + k[item-1][w-wt[item-1]]); |
| 61 | + } |
47 | 62 | }
|
| 63 | + |
| 64 | + return k[items][W]; |
48 | 65 | }
|
49 | 66 |
|
| 67 | +/* |
| 68 | +Time and Space Complexity - O(items * knapsackCapacity) |
| 69 | +*/ |
| 70 | + |
| 71 | +//RECURSION USING MEMOIZATION |
| 72 | + |
| 73 | +public int knapsack(int W, int item, int wt[], int val[], HashMap<String,Integer> map){ |
| 74 | + if(W==0 || item==0) return 0; |
| 75 | + |
| 76 | + String key = item + "|" + W; |
| 77 | + int include = 0; |
| 78 | + int exclude = 0; |
| 79 | + if(!map.containsKey(key)){ |
| 80 | + if(wt[item] < W) include = v[n] + knapsack(W-w[n],item-1,wt,val,map); |
| 81 | + exclude = knapsack(W,item-1,wt,val,map); |
| 82 | + |
| 83 | + map.put(key, Math.max(include,exclude)); |
| 84 | + } |
| 85 | + return map.get(key); |
| 86 | +} |
| 87 | + |
| 88 | +//Time Complexity - O(n*W) |
| 89 | +//where n - number of items and W - capacity of knapsack |
| 90 | + |
50 | 91 |
|
0 commit comments