feat(dijkstra): mark labels as reached to increase performance
This commit is contained in:
parent
dcc2bc0746
commit
6baec1d7dd
2 changed files with 14 additions and 13 deletions
|
@ -51,7 +51,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// On considère le sommet x à chaque itération
|
||||
Label x = s;
|
||||
|
||||
int dest_id = data.getDestination().getId();
|
||||
final int dest_id = data.getDestination().getId();
|
||||
while (!tas.isEmpty() && !(labels.get(dest_id).getNode().equals(x.getNode()))) {
|
||||
x = tas.deleteMin();
|
||||
x.mark();
|
||||
|
@ -63,29 +63,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
Label successor = labels.get(successorArc.getDestination().getId());
|
||||
|
||||
if (!successor.isMarked() && data.isAllowed(successorArc)) {
|
||||
// This loop serves to get the lentgh of the arc as
|
||||
// This loop serves to get the length of the arc as
|
||||
// we know its origin and destination
|
||||
for (Arc arc : x.getNode().getSuccessors()) {
|
||||
if (successor.getNode() == arc.getDestination()) {
|
||||
if (successor.getNode().equals(arc.getDestination())) {
|
||||
arc_cost = arc.getLength();
|
||||
}
|
||||
}
|
||||
float possible_path_cost = x.getCost() + arc_cost;
|
||||
final float possible_path_cost = x.getCost() + arc_cost;
|
||||
if (successor.getCost() > possible_path_cost) {
|
||||
// Mise à jour du label
|
||||
successor.setPathCost(possible_path_cost);
|
||||
successor.setParentNode(x.getNode());
|
||||
// Si y est déjà dans le tas, on va l'update, pas l'insérer
|
||||
// On a pas de fonction update donc on fait un remove puis insert
|
||||
if (tas.array.contains(successor)) {
|
||||
tas.remove(successor);
|
||||
// Si le noeud n'a pas déjà était rajouté au tas, on le rajoute
|
||||
// isReached permet de vérifier en complexité O(1)
|
||||
// C'est un léger coût en mémoire pour un gain en vitesse
|
||||
if (!successor.isReached()) {
|
||||
tas.insert(successor);
|
||||
successor.markReached();
|
||||
notifyNodeReached(successor.getNode());
|
||||
}
|
||||
// il n'y est pas déjà on l'insère
|
||||
else {
|
||||
tas.insert(successor);
|
||||
}
|
||||
notifyDestinationReached(successor.getNode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.insa.graphs.model.Node;
|
|||
public class Label implements Comparable<Label> {
|
||||
Node node;
|
||||
boolean marked;
|
||||
boolean reached;
|
||||
float pathCost;
|
||||
Node parentNode;
|
||||
|
||||
|
@ -12,6 +13,7 @@ public class Label implements Comparable<Label> {
|
|||
public Label(Node node) {
|
||||
this.node = node;
|
||||
this.marked = false;
|
||||
this.marked = false;
|
||||
this.pathCost = Float.MAX_VALUE;
|
||||
this.parentNode = null;
|
||||
}
|
||||
|
@ -19,6 +21,8 @@ public class Label implements Comparable<Label> {
|
|||
public Node getNode() { return this.node; }
|
||||
public boolean isMarked() { return this.marked; }
|
||||
public void mark() { this.marked = true; }
|
||||
public boolean isReached() { return this.reached; }
|
||||
public void markReached() { this.reached = true; }
|
||||
public float getPathCost() { return this.pathCost; }
|
||||
public void setPathCost(float newCost) { this.pathCost = newCost; }
|
||||
public Node getParentNode() { return this.parentNode; }
|
||||
|
|
Loading…
Reference in a new issue