Skip to content

Instantly share code, notes, and snippets.

@anishshobithps
Last active August 11, 2022 17:32
Show Gist options
  • Save anishshobithps/f0f2e66356ac9e8fdceb191e8753f8fc to your computer and use it in GitHub Desktop.
Save anishshobithps/f0f2e66356ac9e8fdceb191e8753f8fc to your computer and use it in GitHub Desktop.
Fractional Knapsack, also sorts by (Profit / weight) Ratio.
import java.util.*;
class Item {
public double weight;
public double profit;
public double ratio;
Item(double weight, double profit) {
this.weight = weight;
this.profit = profit;
this.ratio = (this.profit / this.weight);
}
public double getProfit() {
return this.profit;
}
public double getWeight() {
return this.weight;
}
public double getRatio() {
return this.ratio;
}
}
public class Main {
static int n;
static double C, weight, profit;
// We use ArrayList which is a class instead of a normal array.
static ArrayList<Item> items = new ArrayList<>();
static void solve_knapsack() {
double c = C, profit = 0;
for (int i = 0; i < n; i++) {
// As ArrayList is a class and not a Array, we need to get the item using the get method.
Item item = items.get(i);
if (c >= item.getWeight()) {
System.out.printf("\nItem %d (weight: %.2f, profit : %.2f) is added to the knapsack", i + 1, item.getWeight(), item.getProfit());
profit += item.getProfit();
c -= item.getWeight();
} else if (c != 0) {
System.out.printf("\nItem %d (weight: %.2f, profit : %.2f) is added to the knapsack", i + 1, item.getWeight(), item.getProfit());
profit += (item.getProfit() * c) / item.getWeight();
c -= c;
}
}
System.out.printf("\nTotal profit of the item added to the knapsack = %.2f", profit);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("Enter the number of items : ");
n = sc.nextInt();
System.out.println();
for (int i = 0; i < n; i++) {
System.out.printf("Enter the profit of item %d : ", i + 1);
profit = sc.nextDouble();
System.out.printf("Enter the weight of item %d : ", i + 1);
weight = sc.nextDouble();
System.out.println();
// Adding new elements to the ArrayList (items).
items.add(new Item(weight, profit));
}
// We sort items (ArrayList) using sort under Collections class.
// Comparator will take a generic of type Item.
Collections.sort(items, new Comparator<Item>() {
// The compare function has to always a return int.
public int compare(Item I1, Item I2) {
// We cannot use compareTo() function because it can't derefrence a double as I used in the program.
// So I fixed by comparing values of Item 2 with Item 1 and if its true then return 1 else -1.
// I2 > I1 (would return in sorting in decending order this would avoid another function call).
return (I2.getRatio() > I1.getRatio() ? 1 : -1);
}
});
System.out.printf("Enter the maximum capacity : ");
C = sc.nextDouble();
System.out.printf("\nFraction Knapsack using Greedy Method :");
solve_knapsack();
}
}
// Implementation by Sanjana
// Modified by me
import java.util.*;
class Item implements Comparable<Item> {
float profit;
float weight;
float ratio;
Item(int i)
{
Scanner sc = new Scanner(System.in);
System.out.printf("Enter the profit of item %d: ", i);
profit = sc.nextFloat();
System.out.printf("Enter the weight of item %d: ", i);
weight = sc.nextFloat();
ratio = profit / weight;
System.out.println();
}
public int compareTo(Item item) //sorting function
{
return ratio < item.ratio ? 1 : -1;
}
}
public class Main {
public static int n;
public static float c;
public static ArrayList<Item> ItemArray = new ArrayList();
static void solve(ArrayList<Item> ItemArray) {
int i;
float remaining = c, totalProfit = 0;
for (i = 0; i < ItemArray.size(); i++) {
Item item = ItemArray.get(i);
if (remaining >= item.weight) {
System.out.printf("\nItem %d (weight: %.2f, profit : %.2f) is added to the knapsack", i+1, item.weight, item.profit);
totalProfit = totalProfit + item.profit;
remaining = remaining - item.weight;
}
else {
totalProfit += (item.profit * remaining) / item.weight;
System.out.printf("\nItem %d (weight: %.2f, profit : %.2f) is added to the knapsack", i+1, item.weight, item.profit);
break;
}
}
System.out.printf("\nTotal Profit obtained is %.2f", totalProfit);
}
public static void main(String[] args) {
System.out.print("Enter the number of items : ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println();
for (int i = 1; i <= n; i++)
ItemArray.add(new Item(i));
System.out.print("Enter the maximum capacity : ");
c = sc.nextFloat();
Collections.sort(ItemArray);
solve(ItemArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment