-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knapsack.java
60 lines (52 loc) · 1.77 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
import java.util.Arrays;
public class Knapsack {
public static int knapsack(int val[], int wt[], int W, int n, int[][] dp) {
if (W == 0 || n == 0) {
return 0;
}
if (dp[n][W] != -1) {
return dp[n][W];
}
if (wt[n - 1] <= W) {
// Include the item
int ans_1 = val[n - 1] + knapsack(val, wt, W - wt[n - 1], n - 1, dp);
// Exclude the item
int ans_2 = knapsack(val, wt, W, n - 1, dp);
dp[n][W] = Math.max(ans_1, ans_2);
} else {
dp[n][W] = knapsack(val, wt, W, n - 1, dp);
}
return dp[n][W];
}
public static int tab_knapsack(int val[], int wt[], int W, int n) {
int dp[][] = new int[n + 1][W + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= W; j++) {
int v = val[i - 1];
int w = wt[i - 1];
if (w <= j) {
int include_profit = v + dp[i - 1][j - w];
int exclude_profit = dp[i - 1][j];
dp[i][j] = Math.max(include_profit, exclude_profit);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[n][W];
}
public static void main(String[] args) {
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = val.length;
int[][] dp = new int[n + 1][W + 1];
for (int i = 0; i < dp.length; i++) {
Arrays.fill(dp[i], -1);
}
// Using memoized recursive approach
System.out.println(knapsack(val, wt, W, n, dp));
// Using iterative tabulation approach
System.out.println(tab_knapsack(val, wt, W, n));
}
}