-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedySolve.java
More file actions
80 lines (63 loc) · 1.82 KB
/
Copy pathGreedySolve.java
File metadata and controls
80 lines (63 loc) · 1.82 KB
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GreedySolve extends Knapsack {
public GreedySolve(List <Item> items, int max_capacity) {
super(items,max_capacity);
}
public GreedySolve() {
super();
}
@Override
public void Solve() {
// Compute the ratio of value to weight of each item and store as density
// Sort the items according to their density
// Arrange items from largest density to lowest density
// Store the sorted items into arrayList
ArrayList <Item> newList = new ArrayList<Item>();
GreedySolve knapsack = new GreedySolve();
knapsack.setItem(newList);
items = Arrangement();
// Pick the largest value of density that does not exceed the knapsack capacity
for(Item z: items)
{
if(z.getWeight() <= max_capacity - knapsack.getCapacity() && !knapsack.aDuplicateCheck(z))
{
newList.add(z);
knapsack.setItem(newList);
}
}
//Display the knapsack result
knapsack.result("Greedy",max_capacity);
}
//calculate density for each item
// Sorting the items by density using selection sort and
public ArrayList<Item> Arrangement()
{
int i,j,pos;
Item max, temp;
for(Item itm:items) {
float density = (float) itm.getValue()/itm.getWeight();
itm.setDensity(density);
}
for(i = 0;i<items.size();i++ ) {
max = items.get(i);
pos = i;
for(j = i;j<items.size();j++) {
max= max.compareTo(items.get(j))<0?items.get(j):max;
if(max.getDensity()==items.get(j).getDensity()) {
pos = j;
}
}
temp = items.get(i);
items.set(i, max);
items.set(pos, temp);
}
return new ArrayList<Item>(items);
}
@Override
public int compareTo(Knapsack o) {
// TODO Auto-generated method stub
return 0;
}
}