-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKnapsack.java
More file actions
49 lines (39 loc) · 1.14 KB
/
Copy pathKnapsack.java
File metadata and controls
49 lines (39 loc) · 1.14 KB
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
package Lab_Task_1;
public class Knapsack {
public static void main(String arg[])
{
int val[] = { 45,15,20,10,30 };
int wt[] = { 2,1,3,1,2 };
int W = 7;
int n = val.length;
printknapSack(W, wt, val, n);
}
static void printknapSack(int W, int wt[], int val[], int n) {
int i, w;
int K[][] = new int[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = Math.max(val[i - 1] +
K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
int res = K[n][W];
System.out.println(res);
w = W;
for (i = n; i > 0 && res > 0; i--) {
if (res == K[i - 1][w]) {
// Nothing
}
else {
System.out.print(wt[i - 1] + "\t");
res = res - val[i - 1];
w = w - wt[i - 1];
}
}
}
}