fractional knapsack problem

Solutions on MaxInterview for fractional knapsack problem by the best coders in the world

showing results for - "fractional knapsack problem"
Rogelio
04 Jul 2017
1def greedy_knapsack(values,weights,capacity):
2    n = len(values)
3    def score(i) : return values[i]/weights[i]
4    items = sorted(range(n)  , key=score , reverse = True)
5    sel, value,weight = [],0,0
6    for i in items:
7        if weight +weights[i] <= capacity:
8            sel += [i]
9            weight += weights[i]
10            value += values [i]
11    return sel, value, weight
12
13
14weights = [4,9,10,20,2,1]
15values = [400,1800,3500,4000,1000,200]
16capacity = 20
17
18print(greedy_knapsack(values,weights,capacity))
Andrea
15 Apr 2017
1/*Given Weights and values of n items, we need to put these items in a knapsack
2  of capacity W to get the maximunm value in the knapsack.
3  Sample Input:N=3
4  			   W=50;arr[]={{60,10},{100,20},{120,30}};
5  Sample Output:240
6  TC=O(nlogn)
7*/
8#include<bits/stdc++.h>
9using namespace std;
10struct item
11{
12    int value,weight;
13};
14bool cmp(item a,item b)
15{
16    double r1=(double)a.value/a.weight;
17    double r2=(double)b.value/b.weight;
18    return(r1>r2);
19}
20double fractionalknapsack(item arr[],int w,int n)
21{
22    sort(arr,arr+n,cmp);
23    int cur_weight=0;
24    double final_val=0.0;
25    for(int i=0;i<n;i++)
26    {
27        if(cur_weight+arr[i].weight<=w)
28        {
29            cur_weight+=arr[i].weight;
30            final_val+=arr[i].value;
31        }
32        else
33        {
34            int remain=w-cur_weight;
35            final_val+=arr[i].value*((double)remain/arr[i].weight);
36        }
37    }
38    return final_val;
39}
40int main()
41{
42    int n;
43    cout<<"Enter the number of items:"<<endl;
44    cin>>n;
45    int w;
46    cout<<"enter the maximum weight:"<<endl;
47    cin>>w;
48    item arr[n];
49    cout<<"enter the value and weights:"<<endl;
50    for(int i=0;i<n;i++)
51    {
52        cin>>arr[i].value>>arr[i].weight;
53    }
54    double ans=fractionalknapsack(arr,w,n);
55    cout<<ans;
56    return 0;
57}
58
Juan Martín
03 May 2019
1#include<bits/stdc++.h>
2using namespace std;
3vector<pair<int,int> >a;
4//dp table is full of zeros
5int n,s,dp[1002][1002];
6void ini(){
7    for(int i=0;i<1002;i++)
8        for(int j=0;j<1002;j++)
9            dp[i][j]=-1;
10}
11int f(int x,int b){
12	//base solution
13	if(x>=n or b<=0)return 0;
14	//if we calculate this before, we just return the answer (value diferente of 0)
15	if(dp[x][b]!=-1)return dp[x][b];
16	//calculate de answer for x (position) and b(empty space in knapsack)
17	//we get max between take it or not and element, this gonna calculate all the
18	//posible combinations, with dp we won't calculate what is already calculated.
19	return dp[x][b]=max(f(x+1,b),b-a[x].second>=0?f(x+1,b-a[x].second)+a[x].first:INT_MIN);
20}
21int main(){
22	//fast scan and print
23	ios_base::sync_with_stdio(0);cin.tie(0);
24	//we obtain quantity of elements and size of knapsack
25	cin>>n>>s;
26	a.resize(n);
27	//we get value of elements
28	for(int i=0;i<n;i++)
29		cin>>a[i].first;
30	//we get size of elements
31	for(int i=0;i<n;i++)
32		cin>>a[i].second;
33	//initialize dp table
34	ini();
35	//print answer
36	cout<<f(0,s);
37	return 0;
38}
39
queries leading to this page
fractional knapsack is based on which methodknapsack greedy c 2b 2b0 1 nacksack problemfractional knapsack problem object 5 weight 100how to implement knapsack problem in python using greedy algorithmfractional knapsack solutionknapsack problem using greedy methodknapsack python codethe fractional knapsack problem is an example forfractional knapsack problem with exampleknapsack that you must fill optimallyknapsack problem greedy methodalgorithm knapsack problemgreedy algorithm for knapsack lowest running timeknapsack solved using greedy approachknapsack fractional problem in pythonknapsack problem greedy algorithm analysisfraction knapsack problemfractional knapsack algorithm gatevidyalaknapsack problem 27simple 27knapsack code greedydiscuss the use of greedy method in solving knapsack problem knapsack problem using lcbb methodknapsack problem in greedyfranctional knapsack problemfractional knapsack sudo codeknapsack problem solutionknapsack problem using greedy method pythonfractional knapsack problem cppgreedy algorithm knapsackgreedy knapsack c optimal solution for knapsack using greedyknapsack solution codinggreedy used in 1 2f0 knapsackknapsack greedy algorithm javaknapsack problem time complexity greedyprogram to implement knapsack problemknapsack problem using greedy method geeksforgeeksfractional knapsack problem in c 2b 2bwhat is fractional knapsack problemfractional knapsack problem in knapsack greedy informationknapsack javafractional knapsack problem in pythonknapsack definitionknapsack problem decriptionfractional knapsack example step by stepalgorithm of knapsack problem using greedy methodknapsack problem dynamic programming complexitybackpack algorithm dynamic programmingknapsack treeknapsack problem listknapsack problem dynamic programming algorithmthe complexity of the knapsack problem isinteger knapsack problemknapsack problem greedywhat is 0 2f1 knapsack problemfractional knapsack problem hackerrankknapsack solutionknapsack problem exampleknapsack optimizationknapsack problem using greedygreedy knapsackfractional vs integral knapsack problemone cancel the oi knapsack optimisation problem by using an algorithm that solves the oven knapsack decisionfractional knapsack problem code in c 2b 2bknapsack greedy algorithm correctnessknapsack 7b0 2c1 29 questionknapsack greedy algorithm problemsgivent the value and weight of each item 2c find the maximum number of items that could be put in kanpsack of weight wknapsack with greedy methodgreedy knapsack examplefractional knapsack problem with liquefaction knapsack problemfractional knapsack algorithm code 29 1 knapsackknapsack algorithm cryptographyknapsack using greedy methodknapsack greedy profitknapsack problem dpfractional knapsack time complexity0 1 knapsack problemfractional knapsack problem explained with codeimplement fractional knapsack problem and explain it 27s complexity what is the optimal solution for knapsack problemcode of binary knapscak problemknapsack problem greedy algorithm fractionalhow to do knapsack problemgreedy algorithm to solve knapsack problem0 2f1 knapsack problem greedy algorithmknapsack pythonalgorithm for greedy knapsack problemfractional knapsack in pythonknapsack problem using greedy method codefractional knapsack javagreedy method knapsack problem python codeknapsack problem greedy javaknapsack gfg using greedymax knapsack problemfractional knapsack pythongreedy knapsack time complexitythe knapsack problem cannot be solved by which of the following approacheswhich technique cannot be used to solve knapsack problemwhat is knapsack called in greedyimplement fractional knapsack problem and explain its complexity fractional knapsack problem hackerearthfractional knapsack codeimplement knapsack problem using greedy approach knapsack greedyknapsack problem greedy knapsack 01write a program to implement the fractional knapsack algorithmknapsack greedy implemetationonline knapsack problem solver lcbknaosack algorithm0 1 knapsack problem lknapsack problem o 28wgreedy algorithmsknapsack greedy methoddynamic programming knapsackknapsack using greedyfractional knapsack jsfractional knapsack definitionfractional knapsack problem pythonfractional knapsack problem time complexityknapsack problem explainedfractional knapsack problem is also known asfind out difference between used objects and unused objects when we kind the maximum profit using greedy knapsackknapsack solved with greedy algorithmknapsack problem dynamic programmingknapsack weight greedy knapsack algorithmonline fractional knapsack solverknapsack algorithm implementationwhat is a knapsack problemknapsack problem using greedy method in cppdynamic knapsack also known asexplain knapsack problem 3fwhy do we need knapsack algorithmknapsack problem c 2b 2b using greedy methodbinary knapsack problemknapsack problem algorithm using greedy methodfractional knapsack problem in cexplain knapsack problem using greedy methodknapsack 0 1 for 28100 2c50 2c20 2c10 2c7 2c3 29greedy algorithm for knapsackgreedy knapsack algoknapsack problem greedy algorithm pseudocodefractional knapsack problem c 2b 2b codefractional knapsack in linear time implementationknapsack problem codeexplain 0 1 knapsack problem with respect to fractional knapsackgreedy method knapsack problemcode of fractional knapscak problemknapsack problem a 2aalgorithm for greedy knapsack problem to maximize the profitknapsack algorithm python greedyknapsack problem without matrix4 fractional knapsack problemgreedy algorithm knapsack problem with exampleknapsack algorithm greedyfractional knapsack code in pythonknapsack algorithm exampledefine functional knap problem and give a greedy algorithm to solve this problem efficientlygreedy 3 code for knapsacksolve the following instance using greedy approach 2c also write the algorithm knapsack capacity 3d 10 2c p 3d 3c1 2c 6 2c 18 2c 22 2c 28 3e and w 3d 3c1 2c2 2c5 2c6 2c7 3e greedy fractional knapsack algorithmknapsack algorithmexplain knapsack problem what is the knapsack problemdiscuss the use of greedy method in solving knapsack problemthe knapsack problem can be solved using greedy algorithmknapsack greedy implementation in coding blockscan knapsack be solved using greedywhat do you mean by greedy strategy explain with knapsack problemknapsack problem 28dynamic programming 29knapsack problem time complexitywhen does knapsack greedy algorithm not workknapsack problem gfgknapsack problem geeksforgeekssolve fractional knapsack problem algo0 2f1 knapsack problem using greedy methodapproach is optimal for the fractional knapsack problem proof of fractional knapsack0 2f1 knapsack problem by dynamic programmingfractional knapsack problem practicefractional knapsack problem dpexample of knapsack problemfractional knapsack cagreedy knapsack programgreedy algorithm for 0 2f1 knapsack problemknapsack python implementation using greedy methodanalysis of fractional knapsack problemcomplexity of knapsack problem using greedy methodcode for fractional knapsackwhat is the use of knapsack algorithmgreedy vs dp knapsackwhat is solution space for fractional knapsackknapsack problem greedy algorithmknapsack solved with greedy techniqueknapsack problem using greedy method algorithmknapsack problem greedy algorithm c 2b 2bknapsack problem fractionalknapsack problem using greedy method space complexityfractional knapsack code in c 2b 2bsolve fractional knapsack problem onlineknapsack problem examplesknapsack problemknapsack using stacckknapsack problem is an example ofknapsack problem using greedy method in javais knapsack greedyknapsack greedy algorithm c 2b 2bknapsack greedy approach using sortingknapsack problem by greedy method in c 2b 2bwhat is fractional knapsack problem complexity01 knapsack problemmethods to solve knapsack problemssack bag problemknapsack algorithm usefractional knapsack problem is solved most efficiently by which of the following algorithmfractional knapsack problem in dpwhat type of algorithm is knapsackknapsack problem example with solutiongreedy algotithm for knapsackknapsack problem in dynamic programming examplewhat is m in knapsack problemwhat is knapsack problembackpack problem dynamic programmingsolution to knapsack problemwhich stragy is better for knapsack0 1 knapsack problemknapsack problem by greedy methodknapsack problem using greedy method c 2b 2bknapsack fractional c 2b 2b codeknapsack greedy knapsack program in pythonknapsack problem using greedy method time complexityfractional knapsack algorithmknapsack problem leetcodehow to use knapsack minecraftknapsack problem runtimegreedy search knapsackimplementation of fractional knapsack problemknapsack in cryptographyfractional knapsack in linear timeknapsack codedefine knapsack problemknapsack implementation0 2f1 knapsackwrite down the greedy algorithm to solve knapsack problemknapsack problem spojgeeks for geeks knapsack problemknapsack problem using greedy method exampleproblem statement knapsack greedy algorithmknapsack memoization diagram0 2f1 knapsack problem 01 knapsack problem leetcodeknapsack problem time com 27knapsack problem algorithmknapsack 0 1 greedyknapsack greedy algorithm codegreedy algorithm for knapsack problemknapsack jsknapsack solved with greedy approachknapsack greedy algorihtmis knapsack problem solutionhow to solve knapsack problem using greedy methodknapsack bagfractional knapsack problem leetcodegreedy algorithm knapsack problemthe fractional knapsack problemdiscuss use of greedy algorithm in knapsack problemfractional kanpsack problem codefractional knapsack problemcode for knapsack problemoptimality of knapsack problemfractional knapsack problem in cppgreedy knapsack program in c 2b 2bcons of knapsack dynamix programmingknapsack problem solver01 knapsack codeknapsack problem using greedy approachgreedy algorithm knapsack problem pythonknapsack problem 27simple implementation 27knapsack problem using greedy method in pythonfractional knapsack problem is an example forthe knapsack problemdynamic knapsack complexityexplanation of knapsack problem using greedy approach knapsack problem dynamic programming solution explainedknapsack problem greedy algorithmknapsack problem greedy algorithm time complexityknapsack problem with greedy methodbounded knapsack problemknapsack to blockchain knapsack problemknapsack data structureknapsack exampleknsapsackknapsack greedy algorithm hackerrankconsider a knapsack instance 3a number of objects 28n 29 3d 4 2c weights 28wi 29 3d 7b15 2c 10 2c 9 2c 5 7d 2c profits 28pi 29 3d 7b1 2c 5 2c 3 2c 4 7d and knapsack capacity 28w 29 3d 8 kg use greedy 2c dynamic approach and b 26b technique to find the solution of this problem greedy knapsack problem explaineda greedy algorithm finds the optimal solution to the integer knapsack problemknapsack problem complexitythe fractional knapsack problem can be solved by usingfractional knapsack calculatorknapsack dynamic programmingfracionla knapsack pythonknapsack problem greedy methodknapsack problem geeksknapsack implementation in c 2b 2b greedyknapsack greedy algorithmfractional knapsack problem geeksforgeeksknapsack problem easy fractional knapsack greedy approachknapsack greedy implementation in c 2b 2bwhy knapsack is greedybackpack algorithmgreedy algorithm 0 1 knapsack problem pythongreedy knapsack problemdynamic knapsackgreedy knapsack example step by stepknapsack greedy algorithm code javagreedy knapsack in c 2b 2bis it possible to do fraction knapsack in linear timegreedy algorithm code for knapsack problemknapsack algorithm in javaknapsack problem linear programmingimplement greedy knapsack algorithmfractional knapsack problem