Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knapsack Problem #1136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions algorithms/Knapsack Problem/cpp/knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <bits/stdc++.h>
using namespace std;

struct Item {
int value, weight;


Item(int value, int weight)
{
this->value=value;
this->weight=weight;
}
};


bool cmp(struct Item a, struct Item b)
{
double r1 = (double)a.value / (double)a.weight;
double r2 = (double)b.value / (double)b.weight;
return r1 > r2;
}


double fractionalKnapsack(int W, struct Item arr[], int n)
{
sort(arr, arr + n, cmp);

int curWeight = 0;
double finalvalue = 0.0;


for (int i = 0; i < n; i++) {
if (curWeight + arr[i].weight <= W) {
curWeight += arr[i].weight;
finalvalue += arr[i].value;
}


else {
int remain = W - curWeight;
finalvalue += arr[i].value
* ((double)remain
/ (double)arr[i].weight);
break;
}
}

return finalvalue;
}


int main()
{
int W = 50;
Item arr[] = { { 60, 10 }, { 100, 20 }, { 120, 30 } };

int n = sizeof(arr) / sizeof(arr[0]);


cout << fractionalKnapsack(W, arr, n);
return 0;
}
71 changes: 71 additions & 0 deletions algorithms/Knapsack Problem/java/knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.*;

public class FractionalKnapSack {
private static double getMax(int[] wt, int[] val,
int capacity)
{
ItemValue[] iVal = new ItemValue[wt.length];

for (int i = 0; i < wt.length; i++) {
iVal[i] = new ItemValue(wt[i], val[i], i);
}

// sorting items;
Arrays.sort(iVal, new Comparator<ItemValue>() {
@Override
public int compare(ItemValue o1, ItemValue o2)
{
return o2.cost.compareTo(o1.cost);
}
});

double totalValue = 0d;

for (ItemValue i : iVal) {

int curWt = (int)i.wt;
int curVal = (int)i.val;

if (capacity - curWt >= 0) {
capacity = capacity - curWt;
totalValue += curVal;
}
else {
double rem
= ((double)capacity / (double)curWt);
totalValue += (curVal * rem);
capacity
= (int)(capacity - (curWt * rem));
break;
}
}

return totalValue;
}


static class ItemValue {
Double cost;
double wt, val, ind;

public ItemValue(int wt, int val, int ind)
{
this.wt = wt;
this.val = val;
this.ind = ind;
cost = new Double((double)val / (double)wt);
}
}

public static void main(String[] args)
{
int[] wt = { 10, 40, 20, 30 };
int[] val = { 60, 40, 100, 120 };
int capacity = 50;

double maxValue = getMax(wt, val, capacity);


System.out.println(maxValue);
}
}