|
@@ -12,88 +12,89 @@ import org.insa.graph.Path;
|
12
|
12
|
|
13
|
13
|
public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
14
|
14
|
|
15
|
|
- public BellmanFordAlgorithm(ShortestPathData data) {
|
16
|
|
- super(data);
|
17
|
|
- }
|
18
|
|
-
|
19
|
|
- @Override
|
20
|
|
- protected ShortestPathSolution doRun() {
|
21
|
|
-
|
22
|
|
- // Retrieve the graph.
|
23
|
|
- ShortestPathData data = getInputData();
|
24
|
|
- Graph graph = data.getGraph();
|
25
|
|
-
|
26
|
|
- final int nbNodes = graph.size();
|
27
|
|
-
|
28
|
|
- // Initialize array of distances.
|
29
|
|
- double[] distances = new double[nbNodes];
|
30
|
|
- Arrays.fill(distances, Double.POSITIVE_INFINITY);
|
31
|
|
- distances[data.getOrigin().getId()] = 0;
|
32
|
|
-
|
33
|
|
- // Notify observers about the first event (origin processed).
|
34
|
|
- notifyOriginProcessed(data.getOrigin());
|
35
|
|
-
|
36
|
|
- // Initialize array of predecessors.
|
37
|
|
- Arc[] predecessorArcs = new Arc[nbNodes];
|
38
|
|
-
|
39
|
|
- // Actual algorithm, we will assume the graph does not contain negative
|
40
|
|
- // cycle...
|
41
|
|
- boolean found = false;
|
42
|
|
- for (int i = 0; !found && i < nbNodes; ++i) {
|
43
|
|
- found = true;
|
44
|
|
- for (Node node : graph) {
|
45
|
|
- for (Arc arc : node) {
|
46
|
|
-
|
47
|
|
- // Small test to check allowed roads...
|
48
|
|
- if (!data.isAllowed(arc)) {
|
49
|
|
- continue;
|
50
|
|
- }
|
51
|
|
-
|
52
|
|
- // Retrieve weight of the arc.
|
53
|
|
- double w = data.getCost(arc);
|
54
|
|
- double oldDistance = distances[arc.getDestination().getId()];
|
55
|
|
- double newDistance = distances[node.getId()] + w;
|
56
|
|
-
|
57
|
|
- if (Double.isInfinite(oldDistance) && Double.isFinite(newDistance)) {
|
58
|
|
- notifyNodeReached(arc.getDestination());
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- // Check if new distances would be better, if so update...
|
62
|
|
- if (newDistance < oldDistance) {
|
63
|
|
- found = false;
|
64
|
|
- distances[arc.getDestination().getId()] = distances[node.getId()] + w;
|
65
|
|
- predecessorArcs[arc.getDestination().getId()] = arc;
|
66
|
|
- }
|
67
|
|
- }
|
68
|
|
- }
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- ShortestPathSolution solution = null;
|
72
|
|
-
|
73
|
|
- // Destination has no predecessor, the solution is infeasible...
|
74
|
|
- if (predecessorArcs[data.getDestination().getId()] == null) {
|
75
|
|
- solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
76
|
|
- } else {
|
77
|
|
-
|
78
|
|
- // The destination has been found, notify the observers.
|
79
|
|
- notifyDestinationReached(data.getDestination());
|
80
|
|
-
|
81
|
|
- // Create the path from the array of predecessors...
|
82
|
|
- ArrayList<Arc> arcs = new ArrayList<>();
|
83
|
|
- Arc arc = predecessorArcs[data.getDestination().getId()];
|
84
|
|
- while (arc != null) {
|
85
|
|
- arcs.add(arc);
|
86
|
|
- arc = predecessorArcs[arc.getOrigin().getId()];
|
87
|
|
- }
|
88
|
|
-
|
89
|
|
- // Reverse the path...
|
90
|
|
- Collections.reverse(arcs);
|
91
|
|
-
|
92
|
|
- // Create the final solution.
|
93
|
|
- solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
94
|
|
- }
|
95
|
|
-
|
96
|
|
- return solution;
|
97
|
|
- }
|
|
15
|
+ public BellmanFordAlgorithm(ShortestPathData data) {
|
|
16
|
+ super(data);
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ @Override
|
|
20
|
+ protected ShortestPathSolution doRun() {
|
|
21
|
+
|
|
22
|
+ // Retrieve the graph.
|
|
23
|
+ ShortestPathData data = getInputData();
|
|
24
|
+ Graph graph = data.getGraph();
|
|
25
|
+
|
|
26
|
+ final int nbNodes = graph.size();
|
|
27
|
+
|
|
28
|
+ // Initialize array of distances.
|
|
29
|
+ double[] distances = new double[nbNodes];
|
|
30
|
+ Arrays.fill(distances, Double.POSITIVE_INFINITY);
|
|
31
|
+ distances[data.getOrigin().getId()] = 0;
|
|
32
|
+
|
|
33
|
+ // Notify observers about the first event (origin processed).
|
|
34
|
+ notifyOriginProcessed(data.getOrigin());
|
|
35
|
+
|
|
36
|
+ // Initialize array of predecessors.
|
|
37
|
+ Arc[] predecessorArcs = new Arc[nbNodes];
|
|
38
|
+
|
|
39
|
+ // Actual algorithm, we will assume the graph does not contain negative
|
|
40
|
+ // cycle...
|
|
41
|
+ boolean found = false;
|
|
42
|
+ for (int i = 0; !found && i < nbNodes; ++i) {
|
|
43
|
+ found = true;
|
|
44
|
+ for (Node node: graph.getNodes()) {
|
|
45
|
+ for (Arc arc: node.getSuccessors()) {
|
|
46
|
+
|
|
47
|
+ // Small test to check allowed roads...
|
|
48
|
+ if (!data.isAllowed(arc)) {
|
|
49
|
+ continue;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ // Retrieve weight of the arc.
|
|
53
|
+ double w = data.getCost(arc);
|
|
54
|
+ double oldDistance = distances[arc.getDestination().getId()];
|
|
55
|
+ double newDistance = distances[node.getId()] + w;
|
|
56
|
+
|
|
57
|
+ if (Double.isInfinite(oldDistance) && Double.isFinite(newDistance)) {
|
|
58
|
+ notifyNodeReached(arc.getDestination());
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ // Check if new distances would be better, if so update...
|
|
62
|
+ if (newDistance < oldDistance) {
|
|
63
|
+ found = false;
|
|
64
|
+ distances[arc.getDestination().getId()] = distances[node.getId()] + w;
|
|
65
|
+ predecessorArcs[arc.getDestination().getId()] = arc;
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ ShortestPathSolution solution = null;
|
|
72
|
+
|
|
73
|
+ // Destination has no predecessor, the solution is infeasible...
|
|
74
|
+ if (predecessorArcs[data.getDestination().getId()] == null) {
|
|
75
|
+ solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
|
76
|
+ }
|
|
77
|
+ else {
|
|
78
|
+
|
|
79
|
+ // The destination has been found, notify the observers.
|
|
80
|
+ notifyDestinationReached(data.getDestination());
|
|
81
|
+
|
|
82
|
+ // Create the path from the array of predecessors...
|
|
83
|
+ ArrayList<Arc> arcs = new ArrayList<>();
|
|
84
|
+ Arc arc = predecessorArcs[data.getDestination().getId()];
|
|
85
|
+ while (arc != null) {
|
|
86
|
+ arcs.add(arc);
|
|
87
|
+ arc = predecessorArcs[arc.getOrigin().getId()];
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ // Reverse the path...
|
|
91
|
+ Collections.reverse(arcs);
|
|
92
|
+
|
|
93
|
+ // Create the final solution.
|
|
94
|
+ solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ return solution;
|
|
98
|
+ }
|
98
|
99
|
|
99
|
100
|
}
|