Unbounded Fractional Knapsack
Last Updated :
14 Apr, 2023
Given the 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 a 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 the 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 implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float knapSack( int W, float wt[], float val[], int n)
{
float maxratio = INT_MIN;
int maxindex = 0;
for ( int i = 0; i < n; i++) {
if ((val[i] / wt[i]) > maxratio) {
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
return (W * maxratio);
}
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;
}
|
Java
class GFG
{
static float knapSack( int W, float wt[],
float val[], int n)
{
float maxratio = Integer.MIN_VALUE;
int maxindex = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((val[i] / wt[i]) > maxratio)
{
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
return (W * maxratio);
}
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));
}
}
|
Python3
import sys
def knapSack(W, wt, val, n):
maxratio = - sys.maxsize - 1 ;
maxindex = 0 ;
for i in range (n):
if ((val[i] / wt[i]) > maxratio):
maxratio = (val[i] / wt[i]);
maxindex = i;
return (W * maxratio);
val = [ 14 , 27 , 44 , 19 ];
wt = [ 6 , 7 , 9 , 8 ];
n = len (val);
W = 50 ;
print (knapSack(W, wt, val, n));
|
C#
using System;
class GFG
{
static float knapSack( int W, float []wt,
float []val, int n)
{
float maxratio = int .MinValue;
int maxindex = 0;
for ( int i = 0; i < n; i++)
{
if ((val[i] / wt[i]) > maxratio)
{
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
return (W * maxratio);
}
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));
}
}
|
Javascript
<script>
function knapSack(W, wt, val, n)
{
var maxratio = -1000000000;
var maxindex = 0;
for ( var i = 0; i < n; i++) {
if (parseInt(val[i] / wt[i]) > maxratio) {
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
return (W * maxratio);
}
var val = [14, 27, 44, 19];
var wt = [6, 7, 9, 8];
var n = val.length;
var W = 50;
document.write( knapSack(W, wt, val, n).toFixed(3));
</script>
|
Time Complexity: O(n) where n is size of input array val and wt. This is because a for loop is being executed from 1 till n in knapSack function.
Space Complexity: O(1) as no extra space has been used.
Share your thoughts in the comments
Please Login to comment...