-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzeroOneKnapsack.java
More file actions
52 lines (40 loc) · 1.28 KB
/
zeroOneKnapsack.java
File metadata and controls
52 lines (40 loc) · 1.28 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
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class ZeroOneKnapsack {
public int knapsack(int W, int[] wt, int[] val, int n)
{
int[][] dp = new int[n+1][W+1];
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if(wt[i-1] <= w){
dp[i][w] = Math.max(val[i-1] + dp[i-1][w - wt[i-1]], dp[i-1][w]);
}
else{
dp[i][w] = dp[i-1][w];
}
}
}
return dp[n][W];
}
public void main_func(){
Scanner s = new Scanner(System.in);
System.out.print("Knapsack capacity => ");
int W = s.nextInt();
System.out.print("Number of items => ");
int n = s.nextInt();
int[] wt = new int[n];
int[] val = new int[n];
System.out.print("Weights item => ");
for (int i = 0; i < n; i++)
wt[i] = s.nextInt();
System.out.print("Value item => ");
for (int i = 0; i < n; i++)
val[i] = s.nextInt();
int maxVal = knapsack(W, wt, val, n);
System.out.println("Maximum value: "+ maxVal);
}
}