-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack_3DP.cpp
More file actions
58 lines (51 loc) · 1.2 KB
/
Knapsack_3DP.cpp
File metadata and controls
58 lines (51 loc) · 1.2 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
#include<iostream>
using namespace std;
int Knapsack(int* wt, int* val, int W, int n)
{
int t[n+1][W+1];
for(int i=0; i<n+1; i++)
{
for(int j=0; j<W+1; j++)
{
if(i==0 || j==0)
{
t[i][j] = 0;
}
else
{
if(wt[i-1]<=j)
{
t[i][j] = max( val[i-1]+ t[i-1][j-wt[i-1]], t[i-1][j]);
}
else if(wt[i-1]>j)
{
t[i][j] = t[i-1][j];
}
}
}
}
for(int i=0; i<n+1;i++)
{
for(int j=0; j<W+1; j++)
{
cout<<t[i][j]<<" ";
}
cout<<endl;
}
return t[n][W];
}
int main()
{
int wt[] = {1,3,4};
int val[] = {1,4,5};
int W = 7;
int n = sizeof(wt)/sizeof(wt[0]);
int MaxProfit = Knapsack(wt,val,W,n);
cout<<MaxProfit;
}
// t matrix
// 0 0 0 0 0 0 0 0
// 0 1 1 1 1 1 1 1
// 0 1 1 4 5 5 5 5
// 0 1 1 4 5 6 6 9
// Here all for all possible values of n and W, max profit is calculated, even when that value is not needed(in recursive code) in calculation of final value(n=3 and W=7)