C++ Program for the Fractional Knapsack Problem
Pre-requisite: Fractional Knapsack Problem
Given two arrays weight[] and profit[] the weights and profit of N items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
Note: Unlike 0/1 knapsack, you are allowed to break the item.
Examples:
Input: weight[] = {10, 20, 30}, profit[] = {60, 100, 120}, N= 50
Output: Maximum profit earned = 240
Explanation:
Decreasing p/w ratio[] = {6, 5, 4}
Taking up the weight values 10, 20, (2 / 3) * 30
Profit = 60 + 100 + 120 * (2 / 3) = 240
Input: weight[] = {10, 40, 20, 24}, profit[] = {100, 280, 120, 120}, N = 60
Output: Maximum profit earned = 440
Explanation:
Decreasing p/w ratio[] = {10, 7, 6, 5}
Taking up the weight values 10, 40, (1 / 2) * 120
Profit = 100 + 280 + (1 / 2) * 120 = 440
Method 1 – without using STL: The idea is to use Greedy Approach. Below are the steps:
- Find the ratio value/weight for each item and sort the item on the basis of this ratio.
- Choose the item with the highest ratio and add them until we can’t add the next item as a whole.
- In the end, add the next item as much as we can.
- Print the maximum profit after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Item {
int value, weight;
Item( int value, int weight)
: value(value), weight(weight)
{
}
};
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;
}
double fractionalKnapsack( struct Item arr[],
int N, int size)
{
sort(arr, arr + size, cmp);
int curWeight = 0;
double finalvalue = 0.0;
for ( int i = 0; i < size; i++) {
if (curWeight + arr[i].weight <= N) {
curWeight += arr[i].weight;
finalvalue += arr[i].value;
}
else {
int remain = N - curWeight;
finalvalue += arr[i].value
* (( double )remain
/ arr[i].weight);
break ;
}
}
return finalvalue;
}
int main()
{
int N = 60;
Item arr[] = { { 100, 10 },
{ 280, 40 },
{ 120, 20 },
{ 120, 24 } };
int size = sizeof (arr) / sizeof (arr[0]);
cout << "Maximum profit earned = "
<< fractionalKnapsack(arr, N, size);
return 0;
}
|
Output:
Maximum profit earned = 440
Time Complexity: O(N*log2N)
Auxiliary Space: O(1)
Method 2 – using STL:
- Create a map with profit[i] / weight[i] as first and i as Second element for each element.
- Define a variable max_profit = 0.
- Traverse the map in reverse fashion:
- Create a variable named fraction whose value is equivalent to remaining_weight / weight[i].
- If remaining_weight is greater than or equals to zero and its value is greater than weight[i] add current profit to max_profit and reduce the remaining weight by weight[i].
- Else if remaining weight is less than weight[i] add fraction * profit[i] to max_profit and break.
- Print the max_profit.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxProfit(vector< int > profit,
vector< int > weight, int N)
{
int numOfElements = profit.size();
int i;
multimap< double , int > ratio;
double max_profit = 0;
for (i = 0; i < numOfElements; i++) {
ratio.insert(make_pair(
( double )profit[i] / weight[i], i));
}
multimap< double , int >::reverse_iterator it;
for (it = ratio.rbegin(); it != ratio.rend();
it++) {
double fraction = ( double )N / weight[it->second];
if (N >= 0
&& N >= weight[it->second]) {
max_profit += profit[it->second];
N -= weight[it->second];
}
else if (N < weight[it->second]) {
max_profit += fraction
* profit[it->second];
break ;
}
}
cout << "Maximum profit earned is:"
<< max_profit;
}
int main()
{
int size = 4;
vector< int > profit(size), weight(size);
profit[0] = 100, profit[1] = 280,
profit[2] = 120, profit[3] = 120;
weight[0] = 10, weight[1] = 40,
weight[2] = 20, weight[3] = 24;
int N = 60;
maxProfit(profit, weight, N);
}
|
Output:
Maximum profit earned is:440
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
19 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...