-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path01knapsack.cpp
75 lines (73 loc) · 1.54 KB
/
01knapsack.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
// Time Complexity=O(2^n) and Space Complexity=O(n*W);
int KnapSack(int W, vector<int> val, vector<int> wt, int n)
{
int K[n+1][W+1];
memset(K,-1,sizeof(K));
if(n==0 || W==0)
{
return 0;
}
if(K[n][W]!=-1)
{
return K[n][W];
}
if(wt[n-1]<=W)
{
return K[n][W]=max(val[n-1]+KnapSack(W-wt[n-1],val,wt,n-1),KnapSack(W,val,wt,n-1));
}
else
{
return K[n][W]=KnapSack(W,val,wt,n-1);
}
}
// Time Complexity=O(n*W) and Space Complexity=O(n*W);
int DpKnapSack(int W, vector<int> val, vector<int> wt, int n)
{
int K[n+1][W+1];
for (int i = 0; i <= n; i++)
{
for (int w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
{
K[i][w] = 0;
}
else if (wt[i - 1] <= w)
{
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
}
else
{
K[i][w] = K[i - 1][w];
}
}
}
return K[n][W];
}
int main()
{
int n, Capas;
cout <<"Enter the number of items and bag capacity : "<<endl;
cin >> n >> Capas;
vector<int> Values(n);
vector<int> Weights(n);
int Temp;
cout <<"Enter the Value of items : "<<endl;
for (size_t i = 0; i < n; i++)
{
cin >> Temp;
Values[i]=Temp;
}
cout <<"Enter the Weight of items : "<<endl;
for (size_t i = 0; i < n; i++)
{
cin >> Temp;
Weights[i]=Temp;
}
cout <<"Maximum profit earned : "<< KnapSack(Capas, Values, Weights, n) << endl;
cout <<"Maximum profit earned : "<< DpKnapSack(Capas, Values, Weights, n) << endl;
return 0;
}