-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroOneKnapsack.java
More file actions
58 lines (47 loc) · 1.6 KB
/
ZeroOneKnapsack.java
File metadata and controls
58 lines (47 loc) · 1.6 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
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class ZeroOneKnapsack{
public static void DynamicKnapsack(int n, int[] w, int[] v, int[][] V, int W){
for(int j = 0; j <= W; j++){
V[0][j] = 0;
}
for(int i = 0; i <= n; i++){
V[i][0] = 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= W; j++){
if(w[i] <= j){
V[i][j] = Math.max(V[i - 1][j], v[i] + V[i - 1][j - w[i]]);
}
else{
V[i][j] = V[i - 1][j];
}
}
}
for(int i = 0; i < n+1; i++){
for(int j = 0; j < W+1; j++){
System.out.print(V[i][j] + " ");
}
System.out.println();
}
System.out.println("Optimal Solution: " + V[n][W]);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = in.nextInt();
int[] w = new int[n + 1];
int[] v = new int[n + 1];
System.out.print("Enter the capacity of the knapsack: ");
int W = in.nextInt();
int[][] V = new int[n + 1][W + 1];
System.out.print("Enter the weights accordingly: ");
for(int i = 1; i <= n; i++){
w[i] = in.nextInt();
}
System.out.print("Enter the profits accordingly: ");
for(int i = 1; i <= n; i++){
v[i] = in.nextInt();
}
DynamicKnapsack(n, w, v, V, W);
}
}