perf(dijkstra): create labels dynamically
This commit is contained in:
parent
8fa1d62d02
commit
a5b553e712
1 changed files with 20 additions and 16 deletions
|
@ -12,6 +12,8 @@ import org.insa.graphs.model.Path;
|
|||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
Label[] labels;
|
||||
|
||||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
}
|
||||
|
@ -20,6 +22,12 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
return new Label(node);
|
||||
}
|
||||
|
||||
protected Label getLabel(Node node) {
|
||||
if (labels[node.getId()] == null)
|
||||
labels[node.getId()] = createLabel(node);
|
||||
return labels[node.getId()];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ShortestPathSolution doRun() {
|
||||
|
||||
|
@ -31,7 +39,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
final int nbNodes = graph.size();
|
||||
|
||||
// List of sommets, but with type Label
|
||||
ArrayList<Label> labels = new ArrayList<Label>(nbNodes);
|
||||
labels = new Label[nbNodes];
|
||||
|
||||
// Heap of sommets
|
||||
BinaryHeap<Label> tas = new BinaryHeap<Label>();
|
||||
|
@ -39,25 +47,21 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// Notify observers about the first event (origin processed).
|
||||
notifyOriginProcessed(data.getOrigin());
|
||||
|
||||
// Init Dijkstra
|
||||
for (Node node : graph.getNodes()) {
|
||||
// Luckily they are ordered by id.
|
||||
// ArrayList.set only works if the value is already initialized (because why Java?)
|
||||
labels.add(this.createLabel(node));
|
||||
}
|
||||
// Init Dijkstra : for performance reasons we create labels on the fly,
|
||||
// instead of at initialization
|
||||
|
||||
Label s = labels.get(data.getOrigin().getId());
|
||||
final Label s = getLabel(data.getOrigin());
|
||||
|
||||
// Add origin in the heap
|
||||
// Label s = origin;//labels.get(0);
|
||||
// Label s = origin;
|
||||
s.setPathCost(0);
|
||||
tas.insert(s);
|
||||
|
||||
// On considère le sommet x à chaque itération
|
||||
Label x = s;
|
||||
|
||||
final int dest_id = data.getDestination().getId();
|
||||
while (!tas.isEmpty() && !(labels.get(dest_id).getNode().equals(x.getNode()))) {
|
||||
final Node dest = data.getDestination();
|
||||
while (!tas.isEmpty() && !(x.getNode().equals(dest))) {
|
||||
x = tas.deleteMin();
|
||||
x.mark();
|
||||
notifyNodeMarked(x.getNode());
|
||||
|
@ -65,7 +69,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// We create a list of node successors of x, instead of a list of Arcs.
|
||||
double arc_cost = 0;
|
||||
for (Arc successorArc : x.getNode().getSuccessors()) {
|
||||
Label successor = labels.get(successorArc.getDestination().getId());
|
||||
Label successor = getLabel(successorArc.getDestination());
|
||||
arc_cost = Double.MAX_VALUE;
|
||||
if (!successor.isMarked() && data.isAllowed(successorArc)) {
|
||||
// This loop serves to get the length of the arc as
|
||||
|
@ -101,7 +105,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
}
|
||||
}
|
||||
|
||||
if (labels.get(data.getDestination().getId()).getParentArc() == null) {
|
||||
if (getLabel(data.getDestination()).getParentArc() == null) {
|
||||
this.pathCost = 0;
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||
}
|
||||
|
@ -109,13 +113,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// Create the path ...
|
||||
ArrayList<Arc> arcs_path = new ArrayList<>();
|
||||
|
||||
Arc arc = labels.get(data.getDestination().getId()).getParentArc();
|
||||
Arc arc = getLabel(data.getDestination()).getParentArc();
|
||||
|
||||
// We will find the path using the parent nodes, from the destination to the
|
||||
// origin
|
||||
while (arc != null && arc.getDestination().getId() != data.getOrigin().getId()) {
|
||||
arcs_path.add(arc);
|
||||
arc = labels.get(arc.getOrigin().getId()).getParentArc();
|
||||
arc = getLabel(arc.getOrigin()).getParentArc();
|
||||
}
|
||||
|
||||
notifyDestinationReached(data.getDestination());
|
||||
|
@ -126,7 +130,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// Create the final solution.
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs_path));
|
||||
|
||||
this.pathCost = labels.get(data.getDestination().getId()).getCost();
|
||||
this.pathCost = getLabel(data.getDestination()).getCost();
|
||||
}
|
||||
return solution;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue