-
Notifications
You must be signed in to change notification settings - Fork 0
/
frac_knapsack_21BCE3982.cpp
120 lines (89 loc) · 2.54 KB
/
frac_knapsack_21BCE3982.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
#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
//Class to represent an item which stores weight and corresponding value of Item
class Item {
public:
float profit;
float weight;
Item() {
this->profit = 0;
this->weight = 0;
}
Item(float profit, float weight) {
this->profit = profit;
this->weight = weight;
}
};
//A comparator function used to sort Item according to val/weight ratio
bool compareItems(Item a, Item b) {
return (float) a.profit / a.weight < (float) b.profit / b.weight;
}
//Function to print Item
void printItem(Item a) {
float pw_ratio = a.profit / (float)a.weight;
cout << "Profit : " << a.profit << " Weight : " << a.weight << " Ratio : " << pw_ratio << endl;
}
//Insertion Sort technique to sort Item according to val/weight ratio
void insertionSort(vector<Item> &items, int n) {
for (int i = 1; i < n; i++) {
Item key = items[i];
int j = i - 1;
while (j >= 0 && compareItems(items[j], key)) {
items[j + 1] = items[j];
j--;
}
items[j + 1] = key;
}
}
int main() {
float capacity;
int n;
cout << "Enter Capacity of Knapsack : ";
cin >> capacity;
cout << "Enter Number of Items : ";
cin >> n;
vector<Item> items(n);
cout << "Enter Profits : " << endl;
for (int i = 0; i < n; i++) {
cin >> items[i].profit;
}
cout << "Enter Weights : " << endl;
for (int i = 0; i < n; i++) {
cin >> items[i].weight;
}
insertionSort(items, n);
cout<<"Items arranged in decreasing order of profit/weight ratio : "<<endl;
for (Item x: items)
printItem(x);
vector<float> x(n);
for (int i = 0; i < n; i++) {
x[i] = 0;
}
int i = 0;
//Taking whole items
for (i = 0; i < n; i++) {
if (items[i].weight > capacity)
break;
else {
x[i] = 1;
capacity -= items[i].weight;
}
}
//Taking fraction of last item
if (i < n) {
x[i] = capacity / items[i].weight;
capacity -= x[i] * items[i].weight;
}
float total_profit =0;
//Calculating total profit by multiplying profit of each item with fraction of that item
for (int j=0;j<n;j++){
total_profit+=(x[j]*items[j].profit);
}
//Printing the result
cout<<"Amount of each item taken :"<<endl;
for (int j = 0; j < n; j++)
cout << "Item "<<j<<" : "<<x[j] << endl;
cout<<"Total Profit : "<<total_profit<<endl;
return 0;
}