The Fractional Knapsack Problem is a variation of the 0/1 knapsack problem, where we can take fractions (or portions) of items, rather than having to decide whether to include or exclude each item entirely. This makes the problem easier to solve because you are allowed to break an item into smaller parts and take just a fraction of its total value or weight, depending on the knapsack's capacity.
n items, each with:
v[i]w[i]W.W.Unlike the 0/1 Knapsack Problem, in the fractional version, we can take any fraction of an item, and the value of the fraction will be proportional to the amount of that item taken.
The Fractional Knapsack Problem can be solved using a Greedy Algorithm, as it satisfies the greedy-choice property. This means that the locally optimal choice at each step (i.e., picking the most valuable item per unit weight) leads to a globally optimal solution.
Compute the value-to-weight ratio for each item:
For each item i, calculate the ratio v[i] / w[i]. This ratio indicates the value we get per unit of weight.
Sort the items based on the value-to-weight ratio in descending order.
Take the items:
Repeat for the next most valuable item until the knapsack is full or all items have been considered.
Given items i with value v[i] and weight w[i], the value-to-weight ratio is:
ratio[i] = v[i] / w[i]
To maximize the value, we:
Let's go through an example to better understand the solution.
W = 50| Item | Weight (w[i]) | Value (v[i]) | Value-to-Weight Ratio (v[i]/w[i]) |
|---|---|---|---|
| 1 | 10 | 60 | 6.0 |
| 2 | 20 | 100 | 5.0 |
| 3 | 30 | 120 | 4.0 |
Compute value-to-weight ratios:
Sort the items based on the value-to-weight ratio (in descending order):
Start filling the knapsack:
Item 1: Weight = 10, Value = 60, Ratio = 6.0
Item 2: Weight = 20, Value = 100, Ratio = 5.0
Item 3: Weight = 30, Value = 120, Ratio = 4.0
Knapsack is full with a total value of 240.
#include <iostream>
#include <vector>
#include <algorithm>
// Structure to represent an item
struct Item {
int weight;
int value;
float ratio; // Value-to-weight ratio
};
// Comparison function to sort items by value-to-weight ratio in descending order
bool compare(Item a, Item b) {
return a.ratio > b.ratio;
}
// Function to solve Fractional Knapsack Problem
float fractionalKnapsack(int W, std::vector<Item>& items) {
// Sort items by their value-to-weight ratio in descending order
std::sort(items.begin(), items.end(), compare);
float totalValue = 0.0f;
for (auto& item : items) {
if (W == 0) {
break;
}
if (item.weight <= W) {
// If the item can be fully included, take it completely
totalValue += item.value;
W -= item.weight;
} else {
// Otherwise, take the fraction of the item that fits
totalValue += item.value * ((float)W / item.weight);
W = 0;
}
}
return totalValue;
}
int main() {
int W = 50; // Knapsack capacity
std::vector<Item> items = {
{10, 60, 0.0f}, // Item 1
{20, 100, 0.0f}, // Item 2
{30, 120, 0.0f} // Item 3
};
// Compute the value-to-weight ratio for each item
for (auto& item : items) {
item.ratio = (float)item.value / item.weight;
}
// Calculate the maximum value achievable
float maxValue = fractionalKnapsack(W, items);
std::cout << "Maximum value in knapsack = " << maxValue << std::endl;
return 0;
}
Item to hold the weight, value, and value-to-weight ratio for each item.compare() sorts the items based on the value-to-weight ratio in descending order.n is the number of items, due to sorting the items by the value-to-weight ratio.n is the number of items, for storing the items and their ratios.Open this section to load past papers