-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_Knapsack.cpp
149 lines (143 loc) · 3.6 KB
/
14_Knapsack.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Sorting Profits/Weights in non-increasing order comes out to be the most optimal one in Knapsack
#include<bits/stdc++.h>
using namespace std;
using namespace std :: chrono;
int partition(vector<pair<int,int>> &arr , int low , int high)
{
int i=low ;
int j=high ;
int pivot = arr[low].first ;
while(i<j)
{
while(arr[i].first <= pivot && i<=high-1)
i++;
while(arr[j].first > pivot && j>=low+1)
j--;
if(i<j) swap(arr[i],arr[j]);
}
swap(arr[low],arr[j]);
return j;
}
void QuickSort(vector<pair<int,int>> &arr, int low , int high)
{
if(low<high)
{
int j= partition(arr,low,high) ;
QuickSort(arr,low,j-1);
QuickSort(arr,j+1,high) ;
}
}
int partitionPW(vector<pair<float,pair<float,float>>>&arr , int low , int high)
{
int i=low ;
int j=high ;
int pivot = arr[low].first ;
while(i<j)
{
while(arr[i].first <= pivot && i<=high-1)
i++;
while(arr[j].first > pivot && j>=low+1)
j--;
if(i<j) swap(arr[i],arr[j]);
}
swap(arr[low],arr[j]);
return j;
}
void QuickSortPW(vector<pair<float,pair<float,float>>>&arr , int low , int high)
{
if(low<high)
{
int j= partitionPW(arr,low,high) ;
QuickSortPW(arr,low,j-1);
QuickSortPW(arr,j+1,high) ;
}
}
void KnapSack(vector<int>&profit , vector<int>& weight , float maxW)
{
int n=profit.size();
vector<pair<int,int>> arr(n) ;
vector<int> X(n);
// case I -> sort according to non-decreasing weights
for(int i=0 ; i<n ; i++)
{
arr[i] = {weight[i],profit[i]};
}
QuickSort(arr,0,n-1);
float prft =0 ;
float currWt =0 ;
int it =0 ;
float W = maxW;
while(it<n)
{
if(arr[it].first <= W )
{
prft +=arr[it].second ;
X[it]=1 ;
W = W-arr[it].first;
}
else break;
it++;
}
if(it<n)
{
prft += (W / arr[it].first) * (arr[it].second) ;
}
cout << "Sorting Weights in non-decreasing order :- " << prft << endl ;
// case II -> sort Profits
for(int i=0 ; i<n ; i++)
{
arr[i] = {profit[i],weight[i]};
}
QuickSort(arr,0,n-1);
reverse(arr.begin(),arr.end());
prft =0 , currWt=0 , it =0;
W=maxW;
while(it<n)
{
if(arr[it].first <= W )
{
prft +=arr[it].first ;
X[it]=1 ;
W = W-arr[it].second;
}
else break;
it++;
}
if(it<n)
{
prft += (W / arr[it].second) * (arr[it].first) ;
}
cout << "Sorting profits in non-increasing order :- " << prft << endl ;
// CASE - III Sorting in non-increasing order of profits/weights
vector<pair<float,pair<float,float>>> A(n) ;
for(int i=0 ; i<n ; i++)
{
A[i] ={profit[i]/weight[i],{profit[i],weight[i]}};
}
QuickSortPW (A,0,n-1);
reverse(A.begin(),A.end());
prft =0 , currWt=0 , it =0;
W=maxW;
while(it<n)
{
if(A[it].second.second <= W )
{
prft +=A[it].second.first ;
W = W-A[it].second.second;
}
else break;
it++;
}
if(it<n)
{
prft += (W / A[it].second.second) * (A[it].second.first) ;
}
cout << "Sorting profits/weights in non-increasing order :- " << prft << endl ;
}
int main()
{
vector<int> profit ={5,15,10,20} ;
vector<int> weight = {3,2,1,4} ;
KnapSack(profit,weight,5);
return 0 ;
}