dijkstra, not functional
This commit is contained in:
parent
7db072a9f7
commit
4bd0c74fa4
1 changed files with 60 additions and 0 deletions
|
@ -1,5 +1,13 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
|
import org.insa.graphs.model.Arc;
|
||||||
|
import org.insa.graphs.model.Graph;
|
||||||
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
|
@ -11,6 +19,58 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = null;
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|
||||||
|
// Heap of "sommets"
|
||||||
|
BinaryHeap<Label> queue = new BinaryHeap<Label>(); // change to a comparable type
|
||||||
|
|
||||||
|
Graph graph = data.getGraph();
|
||||||
|
final int nbNodes = graph.size();
|
||||||
|
|
||||||
|
// Initialize array of distances.
|
||||||
|
double[] distances = new double[nbNodes];
|
||||||
|
Arrays.fill(distances, Double.POSITIVE_INFINITY);
|
||||||
|
distances[data.getOrigin().getId()] = 0;
|
||||||
|
|
||||||
|
// Notify observers about the first event (origin processed).
|
||||||
|
notifyOriginProcessed(data.getOrigin());
|
||||||
|
|
||||||
|
// Init Dijkstra
|
||||||
|
ArrayList<Label> labels = new ArrayList<Label>();
|
||||||
|
for (Node node : graph.getNodes()) {
|
||||||
|
labels.add(new Label(node));
|
||||||
|
}
|
||||||
|
// Nodes set
|
||||||
|
for (Label label : labels) {
|
||||||
|
label.marked = false;
|
||||||
|
label.setPathCost(Float.MAX_VALUE);
|
||||||
|
label.parentNode = null;
|
||||||
|
}
|
||||||
|
// Origin set
|
||||||
|
Label s = labels.get(0);
|
||||||
|
s.setPathCost(0);
|
||||||
|
queue.insert(s);
|
||||||
|
|
||||||
|
boolean trouve = false; // TODO : breaking condition
|
||||||
|
Label x;
|
||||||
|
while (!queue.isEmpty() && !trouve) {
|
||||||
|
x = queue.ExtractMin();
|
||||||
|
x.mark();
|
||||||
|
|
||||||
|
for (Arc arc : x.node.getSuccessors()) {
|
||||||
|
// TODO : Retrieve label with node
|
||||||
|
Label y;
|
||||||
|
if (!arc.getDestination().marked) {
|
||||||
|
float new_cost = x.getCost() + (float) data.getCost(arc);
|
||||||
|
if (y.getCost() > new_cost) {
|
||||||
|
y.setPathCost(new_cost);
|
||||||
|
queue.insert(y);
|
||||||
|
y.setParentNode(x.getNode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue