Unbounded Fractional Knapsack

Given weights and values of n items, the task is to put these items in a knapsack of capacity W to get the maximum total value in the knapsack, we can repeatedly put the same item and we can also put fraction of an item.

Examples:

Input: val[] = {14, 27, 44, 19}, wt[] = {6, 7, 9, 8}, W = 50
Output: 244.444



Input: val[] = {100, 60, 120}, wt[] = {20, 10, 30}, W = 50
Output: 300

Approach: The idea here is to just find the item which has largest value to weight ratio. Then fill the whole knapsack with this item only in order to maximize the final value of the knapsack.

Below is the implementataion of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the maximum required value
float knapSack(int W, float wt[], float val[], int n)
{
  
    // maxratio will store the maximum value to weight
    // ratio we can have for any item and maxindex
    // will store the index of that element
    float maxratio = INT_MIN;
    int maxindex = 0;
  
    // Find the maximum ratio
    for (int i = 0; i < n; i++) {
        if ((val[i] / wt[i]) > maxratio) {
            maxratio = (val[i] / wt[i]);
            maxindex = i;
        }
    }
  
    // The item with the maximum value to
    // weight ratio will be put into
    // the knapsack repeatedly until full
    return (W * maxratio);
}
  
// Driver code
int main()
{
    float val[] = { 14, 27, 44, 19 };
    float wt[] = { 6, 7, 9, 8 };
    int n = sizeof(val) / sizeof(val[0]);
    int W = 50;
  
    cout << knapSack(W, wt, val, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
    // Function to return the maximum required value
    static float knapSack(int W, float wt[], 
                            float val[], int n) 
    {
  
        // maxratio will store the maximum value to weight
        // ratio we can have for any item and maxindex
        // will store the index of that element
        float maxratio = Integer.MIN_VALUE;
        int maxindex = 0;
  
        // Find the maximum ratio
        for (int i = 0; i < n; i++) 
        {
            if ((val[i] / wt[i]) > maxratio)
            {
                maxratio = (val[i] / wt[i]);
                maxindex = i;
            }
        }
  
        // The item with the maximum value to
        // weight ratio will be put into
        // the knapsack repeatedly until full
        return (W * maxratio);
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        float val[] = {14, 27, 44, 19};
        float wt[] = {6, 7, 9, 8};
        int n = val.length;
        int W = 50;
  
        System.out.println(knapSack(W, wt, val, n));
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach
  
import sys 
  
# Function to return the maximum required value
def knapSack(W, wt, val, n):
  
    # maxratio will store the maximum value to weight
    # ratio we can have for any item and maxindex
    # will store the index of that element
    maxratio = -sys.maxsize-1;
    maxindex = 0;
  
    # Find the maximum ratio
    for i in range(n):
        if ((val[i] / wt[i]) > maxratio):
            maxratio = (val[i] / wt[i]);
            maxindex = i;
  
  
    # The item with the maximum value to
    # weight ratio will be put into
    # the knapsack repeatedly until full
    return (W * maxratio);
  
  
# Driver code
  
val = [ 14, 27, 44, 19 ];
wt = [ 6, 7, 9, 8 ];
n = len(val);
W = 50;
  
print(knapSack(W, wt, val, n));
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the maximum required value
    static float knapSack(int W, float []wt, 
                            float []val, int n) 
    {
  
        // maxratio will store the maximum value to weight
        // ratio we can have for any item and maxindex
        // will store the index of that element
        float maxratio = int.MinValue;
        int maxindex = 0;
  
        // Find the maximum ratio
        for (int i = 0; i < n; i++) 
        {
            if ((val[i] / wt[i]) > maxratio)
            {
                maxratio = (val[i] / wt[i]);
                maxindex = i;
            }
        }
  
        // The item with the maximum value to
        // weight ratio will be put into
        // the knapsack repeatedly until full
        return (W * maxratio);
    }
  
    // Driver code
    public static void Main() 
    {
        float []val = {14, 27, 44, 19};
        float []wt = {6, 7, 9, 8};
        int n = val.Length;
        int W = 50;
  
        Console.WriteLine(knapSack(W, wt, val, n));
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

244.444


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.