Fractional Knapsack Problem
Fractional Knapsack Problem
Given the weights and values of N items, put these items in a knapsack of capacity W to get the
maximum total value in the knapsack. In Fractional Knapsack, we can break items for
maximizing the total value of the knapsack
Note: In the 0-1 Knapsack problem, we are not allowed to break items. We either take the whole
item or don’t take it.
Input:
Items as (value, weight) pairs
arr[] = {{60, 10}, {100, 20}, {120, 30}}
Knapsack Capacity, W = 50
Input:
Items as (value, weight) pairs
arr[] = {{500, 30}}
Knapsack Capacity, W = 10
Output: 166.667
Naive Approach: Try all possible subsets with all different fractions but that will be very
inefficient.
// class implemented
/*
struct Item{
int value;
int weight;
};
*/
class Solution {
public: