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