-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack_back.java
More file actions
79 lines (63 loc) · 2.56 KB
/
Knapsack_back.java
File metadata and controls
79 lines (63 loc) · 2.56 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
package knapsack;
// Dibuat Oleh : Muhamad Irvan Dimetrio
import java.util.Arrays;
public class knapsack_back {
static int valueAux = 0; // added
static int weightAux = 0; // added
public static void main(String[] args) {
int[] weights = {100, 50, 45, 20, 10, 5};
int[] values = {40, 35, 18, 4, 10, 2};
int[] sol = new int[weights.length];
int[] finalSol = new int[weights.length];
int max = 160;
knapsack_back(weights, values, max, 0, sol, finalSol); // removed the two parameters
for (int i = 0; i < finalSol.length; i++) {
System.out.println(finalSol[i] * weights[i]);
}
}
public static void knapsack_back(int[] weights, int[] values, int max, int index, int[] sol, int[] finalSol) { // removed the parameters
sol[index] = -1;
int n = weights.length;
while (sol[index] < 1) {
sol[index] = sol[index] + 1;
if (sum(index, sol, weights) <= max && index == n - 1) {
System.out.println("Sol: " + Arrays.toString(sol));
System.out.println("weight = " + sum(index, sol, weights));
update(weights, values, max, index, sol, finalSol);
System.out.println("*******************************");
} else if (index < n - 1) { // changed
knapsack_back(weights, values, max, index + 1, sol, finalSol);
}
// sol[index]=-1; // removed this line
}
}
private static int sum(int index, int[] weights, int[] sol) {
int res = 0;
// for (int i = 0; i < index; i++) { // thrown out
for (int i = 0; i < sol.length; i++) {
if (sol[i] < 0) {
System.out.println("in sum: i = " + i + " sol[i] = " + sol[i]);
}
res += sol[i] * weights[i];
}
return res;
}
private static void update(int[] weights, int[] values, int max, int index, int[] sol, int[] finalSol) { //removed the two parameters
int totalValue = 0;
int totalWeight = 0;
for (int i = 0; i < weights.length; i++) {
if (sol[i] == 1) {
totalValue += values[i];
totalWeight += weights[i];
}
}
if (totalValue > valueAux) {
for (int i = 0; i < weights.length; i++) {
finalSol[i] = sol[i];
}
valueAux = totalValue;
weightAux = totalWeight;
System.out.println("new valueAux: " + valueAux); // changed
}
}
}