In this article, we will learn to solve the fractional knapsack problem using C++. We will start by looking at the problem statement and then move to the solution. This problem is one of many popular classical problems. It is fairly different than its sibling 0-1 knapsack and 0-N knapsack. This is a greedy algorithm and the other two are dynamic programming algorithms.
You are given the list of weight and prices of certain items and a bag/knapsack of certain capacity say W. Your task is to fill this bag in such a manner that the total price of all the items that you take in this bag is maximum for all the configurations. And you can either collect any object as a whole or a fraction of it.
Okay, let’s look at the algorithm to solve this problem. Since we will be implementing a greedy solution to this problem, below is a brief description of greedy algorithms.
In these algorithms, we aim to achieve a local maximum/minimum at each step in order to achieve a global maximum/minimum in the end. That is, we maximize/minimize the answer to each of the subproblems and it leads us to the maximum/minimum answer of the overall problem.
Note:
The greedy choice that you are going to make must be a safe move.
Safe Move:
A move is called a safe move if there exists an optimal answer reliable with this initial move.
Below are the steps that you should follow to develop a perfect greedy algorithm.
Now we will go through the knapsack algorithm, step by step.
O(N)
to O(log2N)
.Lemma:
There exists an optimal solution that uses as much as possible of an item with the maximum value per unit of weight.
The example attached below proves that our lemma is correct.
capacity = capacity - weight[this_item]
.total_value += value[this_item]
value += fraction_of_weight_taken * value[this_item]
.The overall structure of the pseudocode becomes:
The fractional knapsack is a greedy algorithm, and in this article, we looked at its implementation. We learned in brief about the greedy algorithms, then we discussed the pseudocode of the fractional knapsack algorithm. We proved that our greedy choice is a safe move, and in the end, we wrote a C++ program to demonstrate this solution. The overall time complexity of this concept is: O(Nlog2N)
. This is it for today, thanks for reading.
If you want to learn more about knapsack and other greedy algorithms, you can refer to the following websites.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.