-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnbounded_1Recursive.cpp
More file actions
58 lines (51 loc) · 1.19 KB
/
Unbounded_1Recursive.cpp
File metadata and controls
58 lines (51 loc) · 1.19 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 UKnapsack(int wt[], int val[], int W, int n)
{
cout<<"No of elements in array "<<n<<endl;
cout<<"Capacity of bag "<<W<<endl;
cout<<"Weight array"<<endl;
for(int i=0; i<n; i++)
{
cout<<wt[i]<<" ";
}
cout<<endl;
cout<<"Value array"<<endl;
for(int i=0; i<n; i++)
{
cout<<val[i]<<" ";
}
cout<<endl;
int result, val1, val2;
if(n==0 || W==0)
{
return 0;
}
else
{
if(wt[n-1]<=W)
{
val1 = val[n-1] + UKnapsack(wt,val,W-wt[n-1],n);
val2 = UKnapsack(wt,val,W,n-1);
cout<<"val1 "<<val1<<" val2 "<<val2<<endl;
result = max(val1,val2);
cout<<"val returned from second if "<<result<<endl;
return(result);
}
else
{
result = UKnapsack(wt,val,W,n-1);
cout<<"val returned from third if "<<result<<endl;
return(result);
}
}
}
int main()
{
int wt[] = {1,3,4,5};
int val[] = {1,1,2,10};
int W= 7;
int n = sizeof(wt)/sizeof(wt[0]);
int max_profit = UKnapsack(wt,val,W,n);
cout<<"Max Profit "<<max_profit<<endl;
}