-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01knapsackusingDP.cpp
More file actions
80 lines (77 loc) · 2.13 KB
/
Copy path01knapsackusingDP.cpp
File metadata and controls
80 lines (77 loc) · 2.13 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
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
int solve(int n,int m,vector<int>&weight,vector<int>&profit){
vector<vector<int>>dp(n+1,vector<int>(m+1));
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0 or j==0){
dp[i][j]=0;
}
else if(weight[i-1]>j){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=max(dp[i-1][j],profit[i-1]+dp[i-1][j-weight[i-1]]);
}
}
}
for(int i=0;i<dp.size();i++){
for(int j=0;j<dp[0].size();j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}
vector<int>selected(n);
int i=n,j=m;
while(i>=0 and j>=0){
if(dp[i][j]==dp[i-1][j]){
selected[i]=0;
i--;
}
else{
selected[i]=1;
i--;
j=j-weight[i-1];
}
}
for(auto it:selected){
cout<<it<<" ";
}
return dp[n][m];
}
int KnapsackRecursive(int n,int m,vector<int>&weight,vector<int>&profit){
if (n == 0 || m== 0)
return 0;
// If weight of the nth item is more
// than Knapsack capacity W, then
// this item cannot be included
// in the optimal solution
if (weight[n - 1] > m)
return KnapsackRecursive(n-1, m, weight, profit);
// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else
return max(
profit[n - 1]
+ KnapsackRecursive(n-1,m - weight[n - 1], weight, profit),
KnapsackRecursive(n-1,m,weight,profit));
}
int main(){
int n,m;
cout<<"Enter the number of objects"<<endl;
cin>>n;
cout<<"Enter the capacity of the basket"<<endl;
cin>>m;
vector<int>weight(n);
vector<int>profit(n);
for(int i=0;i<n;i++){
cout<<"Enter the weight of the obejct "<<i<<endl;
cin>>weight[i];
cout<<"Enter the profit associated with the object"<<i<<endl;
cin>>profit[i];
}
int ans=KnapsackRecursive(n,m,weight,profit);
cout<<"the Maximum profit is "<<ans<<endl;
return 0;
}