|
| 1 | +/* Java code for fractional knapsack problem - Greedy Approach */ |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class fractional_knapsack { |
| 8 | + |
| 9 | + private double finalValue; |
| 10 | + private int remainingWeight; |
| 11 | + private Item [] itemList; |
| 12 | + |
| 13 | + public fractional_knapsack( int totalWeight, Item [] itemList) { |
| 14 | + this.finalValue = 0; |
| 15 | + this.remainingWeight = totalWeight; |
| 16 | + this.itemList = itemList; |
| 17 | + //Sorting the array in descending order based upon the preference order |
| 18 | + Arrays.sort(itemList); |
| 19 | + } |
| 20 | + |
| 21 | + public double getFinalValue() { |
| 22 | + return finalValue; |
| 23 | + } |
| 24 | + |
| 25 | + public int getRemainingWeight() { |
| 26 | + return remainingWeight; |
| 27 | + } |
| 28 | + |
| 29 | + public Item [] getFractions(){ |
| 30 | + int i = 0; |
| 31 | + |
| 32 | + //Setting fraction of preffered items as 1.0, if their weight is less than the total remaining weight |
| 33 | + while(i < itemList.length && remainingWeight > itemList[i].getWeight()){ |
| 34 | + remainingWeight -= itemList[i].getWeight(); |
| 35 | + finalValue += itemList[i].getValue(); |
| 36 | + itemList[i].setFraction(1.0); |
| 37 | + i++; |
| 38 | + } |
| 39 | + |
| 40 | + if( i < itemList.length) { |
| 41 | + //Calculating the fraction of the item whoes weight is greater than the current remaining weight |
| 42 | + finalValue = finalValue + (remainingWeight) * itemList[i].getValue() / itemList[i].getWeight(); |
| 43 | + itemList[i].setFraction(remainingWeight / itemList[i].getWeight()); |
| 44 | + remainingWeight = 0 ; |
| 45 | + } |
| 46 | + |
| 47 | + return itemList; |
| 48 | + } |
| 49 | + |
| 50 | + public static void main(String args[]){ |
| 51 | + int totalWeight = 90; |
| 52 | + Item [] items = {new Item(10,60) , new Item(20,100) , new Item(30, 120)}; |
| 53 | + |
| 54 | + fractional_knapsack fractionalKnapsack = new fractional_knapsack(totalWeight , items); |
| 55 | + |
| 56 | + items = fractionalKnapsack.getFractions(); |
| 57 | + System.out.println("TOTAL VALUE = "+fractionalKnapsack.getFinalValue()+" REMAINING WEIGHT = "+fractionalKnapsack.getRemainingWeight()); |
| 58 | + for(int i = 0 ; i < items.length ; i++)System.out.print("ITEM "+(i+1) +" "+items[i]); |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +class Item implements Comparable<Item>{ |
| 66 | + private int weight; |
| 67 | + private int value; |
| 68 | + |
| 69 | + |
| 70 | + private double preference; |
| 71 | + private double fraction; |
| 72 | + |
| 73 | + public Item(int weight, int value) { |
| 74 | + |
| 75 | + this.weight = weight; |
| 76 | + this.value = value; |
| 77 | + this.fraction = 0.0; |
| 78 | + //Attribute preference helps to decide the order of preference of the items for selection |
| 79 | + this.preference = (double) value / (double) weight; |
| 80 | + } |
| 81 | + |
| 82 | + public int getWeight() { |
| 83 | + return weight; |
| 84 | + } |
| 85 | + |
| 86 | + public void setWeight(int weight) { |
| 87 | + this.weight = weight; |
| 88 | + } |
| 89 | + |
| 90 | + public int getValue() { |
| 91 | + return value; |
| 92 | + } |
| 93 | + |
| 94 | + public void setValue(int value) { |
| 95 | + this.value = value; |
| 96 | + } |
| 97 | + |
| 98 | + public double getFraction() { |
| 99 | + return fraction; |
| 100 | + } |
| 101 | + |
| 102 | + public void setFraction(double fraction) { |
| 103 | + this.fraction = fraction; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + //Enabling sort in descending order |
| 108 | + @Override |
| 109 | + public int compareTo(Item item) { |
| 110 | + double difference = this.preference - item.preference; |
| 111 | + if(difference > 0) |
| 112 | + return -1; |
| 113 | + else if(difference < 0) |
| 114 | + return 1; |
| 115 | + else |
| 116 | + return 0; |
| 117 | + } |
| 118 | + @Override |
| 119 | + public String toString(){ |
| 120 | + return "VALUE = "+this.value+" WEIGHT = "+this.weight+" FRACTION = "+this.fraction+"\n"; |
| 121 | + } |
| 122 | +} |
0 commit comments