-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnbounded_2Memoization.cpp
More file actions
62 lines (56 loc) · 1.25 KB
/
Unbounded_2Memoization.cpp
File metadata and controls
62 lines (56 loc) · 1.25 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
#include<iostream>
#include<algorithm>
using namespace std;
int t[10][10];
int UKnapsack(int wt[], int val[], int W, int n)
{
if(n==0 || W==0)
{
return 0;
}
else
{
if(t[n][W]!=-1)
{
return t[n][W];
}
else
{
if(wt[n-1]<=W)
{
t[n][W] = max(val[n-1]+UKnapsack(wt,val,W-wt[n-1],n), UKnapsack(wt,val,W,n-1));
return t[n][W];
}
else if(wt[n-1]>W)
{
t[n][W] = UKnapsack(wt,val,W,n-1);
return t[n][W];
}
}
}
return 0;
}
int main()
{
fill(*t,*t+ 10*10, -1);
int wt[] = {1,3,4};
int val[] = {1,4,5};
int W = 7;
int n = sizeof(wt)/sizeof(wt[1]);
int max_profit = UKnapsack(wt,val,W,n);
cout<<"max profit "<<max_profit<<endl;
for(int i=0; i<n+1;i++)
{
for(int j=0; j<W+1; j++)
{
cout<<t[i][j]<<" ";
}
cout<<endl;
}
}
// t matrix
// -1 -1 -1 -1 -1 -1 -1 -1
// -1 1 2 3 4 5 6 7
// -1 1 -1 4 5 -1 -1 9
// -1 -1 -1 4 -1 -1 -1 9
// More values are computed in case of unbounded Knapsack as compared to 0/1 Knapsack t matrix