-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
165 lines (147 loc) · 5.71 KB
/
Knapsack.java
File metadata and controls
165 lines (147 loc) · 5.71 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package dynamic_programming;
import data_structures.LinkedList;
public class Knapsack {
public static void main(String[] args) {
// Example:
Item[] items = {new Item(4, 12, "0"), new Item(2, 2, "1"), new Item(2, 1, "2"), new Item(1, 1, "3"), new Item(10, 4, "4")};
System.out.println(knapsackMaxValueVariant(items, 15)); // [1, 2, 3, 4]
System.out.println(knapsackMinWeightVariant(items, 15)); // [1, 2, 3, 4]
System.out.println(knapsackApproximativeVariant(items, 15, 1.00)); // [1, 2, 3, 4]
System.out.println(knapsackApproximativeVariant(items, 15, 0.90));
}
public static LinkedList<Item> knapsackMaxValueVariant(Item[] items, int weightLimit) {
int[][] table = new int[items.length + 1][weightLimit + 1];
for (int i = 0; i < items.length + 1; ++i) {
table[i][0] = 0;
}
for (int w = 0; w < weightLimit + 1; ++w) {
table[0][w] = 0;
}
for (int i = 1; i < items.length + 1; ++i) {
for (int w = 1; w < weightLimit + 1; ++w) {
Item currentItem = items[i - 1];
int newValue = table[i - 1][w];
if (w - currentItem.weight >= 0) {
newValue = Math.max(newValue, table[i - 1][w - currentItem.weight] + currentItem.value);
}
table[i][w] = newValue;
}
}
LinkedList<Item> solution = new LinkedList<>();
int i = items.length;
int w = weightLimit;
while (i != 0 && w != 0) {
Item currentItem = items[i - 1];
if (w - currentItem.weight >= 0 && table[i - 1][w - currentItem.weight] + currentItem.value > table[i - 1][w]) {
solution.addFirst(currentItem);
w -= currentItem.weight;
}
--i;
}
return solution;
}
public static LinkedList<Item> knapsackMinWeightVariant(Item[] items, int weightLimit) {
int totalValue = 0;
for (Item item : items) {
totalValue += item.value;
}
int[][] table = new int[items.length + 1][totalValue + 1];
for (int i = 0; i < items.length + 1; ++i) {
table[i][0] = 0;
}
for (int w = 0; w < totalValue + 1; ++w) {
table[0][w] = Integer.MAX_VALUE;
}
for (int i = 1; i < items.length + 1; ++i) {
for (int v = 1; v < totalValue + 1; ++v) {
Item currentItem = items[i - 1];
int newWeight = table[i - 1][v];
if (v - currentItem.value >= 0) {
newWeight = Math.min(newWeight, table[i - 1][v - currentItem.value] + currentItem.weight);
}
table[i][v] = newWeight;
}
}
LinkedList<Item> solution = new LinkedList<>();
int i = items.length;
int v = totalValue;
while (table[i][v] > weightLimit) {
--v;
}
while (i != 0 && v != 0) {
Item currentItem = items[i - 1];
if (v - currentItem.value >= 0 && table[i - 1][v - currentItem.value] + currentItem.weight > table[i - 1][v]) {
solution.addFirst(currentItem);
v -= currentItem.value;
}
--i;
}
return solution;
}
public static LinkedList<Item> knapsackApproximativeVariant(Item[] items, int weightLimit, double epsilon) {
int maxValue = 0;
for (Item item : items) {
maxValue = Math.max(maxValue, item.value);
}
double k = (epsilon * maxValue) / items.length;
int[] approxValues = new int[items.length];
int totalValue = 0;
for (int i = 0; i < approxValues.length; ++i) {
int value = (int) (items[i].value / k);
approxValues[i] = value;
totalValue += value;
}
int[][] table = new int[items.length + 1][totalValue + 1];
table[0][0] = 0;
for (int w = 0; w < totalValue + 1; ++w) {
table[0][w] = Integer.MAX_VALUE;
}
for (int i = 1; i < items.length + 1; ++i) {
for (int v = 0; v < totalValue + 1; ++v) {
Item currentItem = items[i - 1];
int currentItemApproxValue = approxValues[i - 1];
int newWeight = table[i - 1][v];
if (v - currentItemApproxValue >= 0) {
newWeight = Math.min(newWeight, table[i - 1][v - currentItemApproxValue] + currentItem.weight);
}
table[i][v] = newWeight;
}
}
LinkedList<Item> solution = new LinkedList<>();
int i = items.length;
int v = totalValue;
while (table[i][v] > weightLimit) {
--v;
}
while (i != 0 && v != 0) {
Item currentItem = items[i - 1];
int currentItemApproxValue = approxValues[i - 1];
if (v - currentItemApproxValue >= 0 && table[i - 1][v - currentItemApproxValue] + currentItem.weight > table[i - 1][v]) {
solution.addFirst(currentItem);
v -= currentItemApproxValue;
}
--i;
}
return solution;
}
public static class Item {
final int value;
final int weight;
String label;
public Item(int value, int weight) {
this.value = value;
this.weight = weight;
}
public Item(int value, int weight, String label) {
this(value, weight);
this.label = label;
}
@Override
public String toString() {
if (label != null) {
return "Item{" + label + '}';
}
return super.toString();
}
}
}