-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_168knap01.java
More file actions
73 lines (71 loc) · 2.22 KB
/
_168knap01.java
File metadata and controls
73 lines (71 loc) · 2.22 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class _168knap01 {
public static int knapsack(int val[],int wt[],int W, int n){
if(W==0 || n==0){
return 0;
}
if(wt[n-1]<=W){ //valid
// include
int ans1 = val[n-1]+knapsack(val,wt,W-wt[n-1],n-1);
//exclude
int ans2 = knapsack(val,wt,W,n-1);
return Math.max(ans1,ans2);
}else { // not valid
return knapsack(val,wt,W,n-1);
}
}
public static int knapsackdp(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){ //valid
// include
int ans1 = val[n-1]+knapsackdp(val,wt,W-wt[n-1],n-1,dp);
//exclude
int ans2 = knapsackdp(val,wt,W,n-1,dp);
dp[n][W]= Math.max(ans1,ans2);
return dp[n][W];
}else { // not valid
dp[n][W]= knapsackdp(val,wt,W,n-1,dp);
return dp[n][W];
}
}
public static int knapsackTab(int val[],int wt[],int W){
int n = val.length;
int dp[][]=new int[n+1][W+1];
for(int j=0;j<dp[0].length;j++){
dp[0][j]=0;
}
for(int i=1;i<n+1;i++){
for(int j=1;j<W+1;j++){
int v=val[i-1];
int w=wt[i-1];
if(w<=j){
int incProfit = v +dp[i-1][j-w];
int excProfit = dp[i-1][j];
dp[i][j]=Math.max(incProfit,excProfit);
}else{
int excProfit = dp[i-1][j];
dp[i][j]=excProfit;
}
}
}
return dp[n][W];
}
public static void main(String[] args) {
int val[]={15,14,10,45,30};
int wt[]={2,5,1,3,4};
int W = 7;
int dp[][] = new int[val.length+1][W+1];
for(int i = 0;i<dp.length;i++){
for(int j = 0;j<dp[0].length;j++){
dp[i][j]= -1;
}
}
System.out.println(knapsack(val, wt, W, val.length));
System.out.println(knapsackTab(val, wt, W));
System.out.println(knapsackdp(val, wt, W, val.length,dp));
}
}