-
Notifications
You must be signed in to change notification settings - Fork 2
/
Knapsack.java
89 lines (69 loc) · 3.24 KB
/
Knapsack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package DynamicProgramming;
/**
*
* Implementation of the 0-1 KnapSack problem.
*
*/
public class Knapsack {
public int solveKnapsack(int[] profits, int[] weights, int capacity) {
Integer[][] dp = new Integer[profits.length][capacity + 1];
return this.knapsackRecursive(dp, profits, weights, capacity, 0);
}
private int knapsackRecursive(Integer[][] dp, int[] profits, int[] weights, int capacity, int currentIndex) {
// base checks
if (currentIndex >= profits.length || capacity <= 0)
return 0;
// if we have already solved a similar problem, return the result from memory
if(dp[currentIndex][capacity] != null)
return dp[currentIndex][capacity];
// recursive call after choosing the element at the currentIndex
// if the weight of the element at currentIndex exceeds the capacity, we shouldn't process this
int profit1 = 0;
if( weights[currentIndex] <= capacity )
profit1 = profits[currentIndex] + knapsackRecursive(dp, profits, weights,
capacity - weights[currentIndex], currentIndex + 1);
// recursive call after excluding the element at the currentIndex
int profit2 = knapsackRecursive(dp, profits, weights, capacity, currentIndex + 1);
dp[currentIndex][capacity] = Math.max(profit1, profit2);
return dp[currentIndex][capacity];
}
public int solveKnapsackBottomsUp(int[] profits, int[] weights, int capacity) {
// basic checks
if (capacity <= 0 || profits.length == 0 || weights.length != profits.length)
return 0;
int[][] dp = new int[profits.length][capacity + 1];
// populate the capacity = 0 columns, with '0' capacity we have '0' profit
for(int i = 0; i < profits.length; i++)
dp[i][0] = 0;
// if we have only one weight, we will take it if it is not more than the capacity
for(int c = 0; c < capacity + 1; c++) {
if(weights[0] <= c)
dp[0][c] = profits[0];
}
// process all sub-arrays for all the capacities
for(int i = 1; i < profits.length; i++) {
for(int c = 1; c <= capacity; c++) {
int profit1 = 0, profit2 = 0;
// include the item, if it is not more than the capacity
if(weights[i] <= c)
profit1 = profits[i] + dp[i-1][c-weights[i]];
// exclude the item
profit2 = dp[i-1][c];
// take maximum
dp[i][c] = Math.max(profit1, profit2);
}
}
// maximum profit will be at the bottom-right corner.
return dp[profits.length - 1][capacity];
}
public static void main(String[] args) {
Knapsack ks = new Knapsack();
int[] profits = {1, 6, 10, 16};
int[] weights = {1, 2, 3, 5};
int maxProfit = ks.solveKnapsack(profits, weights, 7);
System.out.println("Total knapsack profit ---> " + maxProfit);
maxProfit = ks.solveKnapsack(profits, weights, 6);
System.out.println("Total knapsack profit ---> " + maxProfit);
System.out.println("Total knapsack profit ---> " + ks.solveKnapsackBottomsUp(profits, weights, 7));
}
}