forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractional_Knapsack.cpp
85 lines (69 loc) · 1.85 KB
/
Fractional_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
#include <bits/stdc++.h>
using namespace std;
// Structure for an item which stores
// weight & corresponding value of Item
struct Item
{
int value, weight;
// Constructor
Item(int value, int weight)
: value(value), weight(weight)
{
}
};
// Comparison function to sort Item
// according to val/weight ratio
bool cmp(struct Item a, struct Item b)
{
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;
}
// Main greedy function to solve problem
double fractionalKnapsack(struct Item arr[],
int N, int size)
{
// Sort Item on basis of ratio
sort(arr, arr + size, cmp);
// Current weight in knapsack
int curWeight = 0;
// Result (value in Knapsack)
double finalvalue = 0.0;
// Looping through all Items
for (int i = 0; i < size; i++)
{
// If adding Item won't overflow,
// add it completely
if (curWeight + arr[i].weight <= N)
{
curWeight += arr[i].weight;
finalvalue += arr[i].value;
}
// If we can't add current Item,
// add fractional part of it
else
{
int remain = N - curWeight;
finalvalue += arr[i].value * ((double)remain / arr[i].weight);
break;
}
}
// Returning final value
return finalvalue;
}
// Driver Code
int main()
{
// Weight of knapsack
int N = 60;
// Given weights and values as a pairs
Item arr[] = {{100, 10},
{280, 40},
{120, 20},
{120, 24}};
int size = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << "Maximum profit earned = "
<< fractionalKnapsack(arr, N, size);
return 0;
}