Fractional Knapsack Using C++

Fractional Knapsack Problem using C++

Introduction

The Fractional Knapsack Problem is a classic optimization problem in computer science. It arises when we have a set of items, each with a weight and a value, and a knapsack with a limited capacity. The goal is to fill the knapsack with the most valuable items without exceeding its capacity.

Unlike the 0-1 Knapsack Problem, where we can only include an item in the knapsack once, in the Fractional Knapsack Problem, we can include a fraction of an item. This makes the problem more challenging and requires a different approach to solving it.

Algorithm

The greedy algorithm for solving the Fractional Knapsack Problem is as follows:

1. Sort the items in decreasing order of their value-to-weight ratio.
2. Start filling the knapsack with the items in this order.
3. If the current item exceeds the remaining capacity of the knapsack, include a fraction of the item that just fills the knapsack.
4. Repeat steps 2 and 3 until the knapsack is full.

C++ Implementation

The following C++ code implements the greedy algorithm for the Fractional Knapsack Problem:

cpp
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Item {
int weight;
int value;
double value_per_weight;
};

bool compareItems(const Item& a, const Item& b) {
return a.value_per_weight > b.value_per_weight;
}

int main() {
int capacity;
int num_items;
cout << "Enter the capacity of the knapsack: ";
cin >> capacity;
cout << "Enter the number of items: ";
cin >> num_items;

vector<Item> items(num_items);
for (int i = 0; i < num_items; i++) {
cout << "Enter the weight of item " << i + 1 << ": ";
cin >> items[i].weight;
cout << "Enter the value of item " << i + 1 << ": ";
cin >> items[i].value;
items[i].value_per_weight = (double)items[i].value / items[i].weight;
}

sort(items.begin(), items.end(), compareItems);

double total_value = 0;
int remaining_capacity = capacity;

for (int i = 0; i < num_items; i++) {
if (remaining_capacity >= items[i].weight) {
total_value += items[i].value;
remaining_capacity -= items[i].weight;
} else {
double fraction = (double)remaining_capacity / items[i].weight;
total_value += fraction * items[i].value;
remaining_capacity = 0;
break;
}
}

cout << "Maximum value: " << total_value << endl;

return 0;
}

Example

Let’s say we have the following items:

| Item | Weight | Value |
|—|—|—|
| A | 1 | 5 |
| B | 3 | 10 |
| C | 4 | 12 |
| D | 2 | 8 |

And a knapsack with a capacity of 5.

Solution:

1. Sort the items in decreasing order of value-to-weight ratio:

| Item | Weight | Value | Value-to-Weight Ratio |
|—|—|—|—|
| C | 4 | 12 | 3 |
| B | 3 | 10 | 3.33 |
| D | 2 | 8 | 4 |
| A | 1 | 5 | 5 |

2. Start filling the knapsack with the items in this order:

– Add item C to the knapsack (weight = 4, value = 12).
– Add item B to the knapsack (weight = 3, value = 10).

3. Item D exceeds the remaining capacity (5 – 4 – 3 = -2). So, we include a fraction of item D that just fills the knapsack:

– Fraction = remaining_capacity / weight_of_item_D = 2 / 2 = 1
– Value = fraction value_of_item_D = 1 8 = 8

4. Total value = value_of_item_C + value_of_item_B + value_of_fraction_of_item_D = 12 + 10 + 8 = 30

Conclusion

The Fractional Knapsack Problem is a challenging optimization problem that has many applications in real-world scenarios, such as resource allocation, portfolio optimization, and scheduling. The greedy algorithm presented in this article provides a simple and efficient way to solve the problem and maximize the total value obtained.

FAQs

1. What is the difference between the 0-1 Knapsack Problem and the Fractional Knapsack Problem?
– In the 0-1 Knapsack Problem, you can only include an item in the knapsack once. In the Fractional Knapsack Problem, you can include a fraction of an item.
2. What is the time complexity of the greedy algorithm for the Fractional Knapsack Problem?
– The time complexity is O(n log n), where n is the number of items.
3. When is the Fractional Knapsack Problem used in real-world applications?
– The Fractional Knapsack Problem is used in various scenarios, including resource allocation, portfolio optimization, and scheduling.
4. Can the greedy algorithm always find the optimal solution to the Fractional Knapsack Problem?
– Yes, the greedy algorithm always finds the optimal solution to the Fractional Knapsack Problem.
5. How can I improve the efficiency of the greedy algorithm?
– You can use various techniques to improve the efficiency of the greedy algorithm, such as using a min-heap or a binary search tree.
6. What are some applications of the Fractional Knapsack Problem in different industries?
– The Fractional Knapsack Problem is applied in industries such as computer science, economics, and engineering.
7. How can I learn more about the Fractional Knapsack Problem?
– You can refer to books, articles, and online resources to learn more about the Fractional Knapsack Problem.
8. What are the challenges and limitations of the Fractional Knapsack Problem?
– The challenges and limitations of the Fractional Knapsack Problem include its time complexity and its assumptions of linearity and divisibility.

  Hvordan sette opp en brannmur med UFW på Ubuntu