-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRucksackproblem.java
More file actions
70 lines (53 loc) · 2.11 KB
/
Copy pathRucksackproblem.java
File metadata and controls
70 lines (53 loc) · 2.11 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
package np;
public class Rucksackproblem {
public static void main(String[] args){
int b = 100;
int[] w = {3, 2, 4};
int[] v = {2, 3, 1};
System.out.println("Gesamtgewicht: " + dynamisch(b, w, v));
}
public static int dynamisch(int b, int[] w, int[] v){
//Demonstration für exponentielle Laufzeit
int counter = 0;
int n = w.length;
//Array speichert Nr. der Gewichte, die im jeweiligen Platz in r liegen
String[][] el = new String[n+1][b];
//maximale Nutzwert mit allen Elementen bis i bei Höchstgewicht von j (r[i][j])
int[][] r = new int[n+1][b];
//Iteration Elemente: Alle Elemente durchgehen, letztes bis erstes (Reihenfolge egal)
for(int i = n-1; i >= 0; i--){
//Iteration Höchstgewichte: bestimmt den größten Nutzen bei allen Gewichten bisher bei jeweiligem Gesamtgewicht
for(int j = 0; j < b; j++){
//Yay, Counter!
counter++;
//Liegt das Gewicht des aktuellen Elements innerhalb der aktuellen Beschränkungen?
if(w[i] <= j){
//Bestimmung größter Wert:
//Wert des vorherigen Elements, oder
//Wert d. Elements + Wert vom vorherigen Element bei Gesamtgewicht minus aktuelles Gewicht
r[i][j] = Math.max(v[i] + r[i+1][j-w[i]], r[i+1][j]);
//Speichern der jeweiligen Gewichtnr.
if(v[i] + r[i+1][j-w[i]] > r[i+1][j]){
el[i][j] = el[i+1][j-w[i]] + ", " + i;
} else{
el[i][j] = el[i+1][j];
}
}
else{
//Speichern der jeweiligen Gewichtnr.
el[i][j] = el[i+1][j];
//Gewicht d. Elements überschreitet Beschränkung
r[i][j] = r[i+1][j]; //Für alle Gewichte wird der Wert für das vorherige Element übernommen
}
}
}
//Yay, Counter!
System.out.println("Counter: " + counter);
//String.substring() wegen "null, " am Anfang
System.out.println("Gewichte: " + el[0][b-1].substring(6));
//pseudopolynomiell, da Laufzeit polynomiell von b abhängt
//Größe von b ist exponentiell von Länge von b abhängig --> exponentielle Laufzeit
//bei beendeter Iteration höchster Wert bei höchstem Gewicht bestimmt (Math.max)
return r[0][b-1];
}
}