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
|
// On considère le sommet x à chaque itération
|
||||||
Label x = s;
|
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()))) {
|
while (!tas.isEmpty() && !(labels.get(dest_id).getNode().equals(x.getNode()))) {
|
||||||
x = tas.deleteMin();
|
x = tas.deleteMin();
|
||||||
x.mark();
|
x.mark();
|
||||||
|
@ -63,29 +63,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Label successor = labels.get(successorArc.getDestination().getId());
|
Label successor = labels.get(successorArc.getDestination().getId());
|
||||||
|
|
||||||
if (!successor.isMarked() && data.isAllowed(successorArc)) {
|
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
|
// we know its origin and destination
|
||||||
for (Arc arc : x.getNode().getSuccessors()) {
|
for (Arc arc : x.getNode().getSuccessors()) {
|
||||||
if (successor.getNode() == arc.getDestination()) {
|
if (successor.getNode().equals(arc.getDestination())) {
|
||||||
arc_cost = arc.getLength();
|
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) {
|
if (successor.getCost() > possible_path_cost) {
|
||||||
// Mise à jour du label
|
// Mise à jour du label
|
||||||
successor.setPathCost(possible_path_cost);
|
successor.setPathCost(possible_path_cost);
|
||||||
successor.setParentNode(x.getNode());
|
successor.setParentNode(x.getNode());
|
||||||
// Si y est déjà dans le tas, on va l'update, pas l'insérer
|
// Si le noeud n'a pas déjà était rajouté au tas, on le rajoute
|
||||||
// On a pas de fonction update donc on fait un remove puis insert
|
// isReached permet de vérifier en complexité O(1)
|
||||||
if (tas.array.contains(successor)) {
|
// C'est un léger coût en mémoire pour un gain en vitesse
|
||||||
tas.remove(successor);
|
if (!successor.isReached()) {
|
||||||
tas.insert(successor);
|
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> {
|
public class Label implements Comparable<Label> {
|
||||||
Node node;
|
Node node;
|
||||||
boolean marked;
|
boolean marked;
|
||||||
|
boolean reached;
|
||||||
float pathCost;
|
float pathCost;
|
||||||
Node parentNode;
|
Node parentNode;
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ public class Label implements Comparable<Label> {
|
||||||
public Label(Node node) {
|
public Label(Node node) {
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.marked = false;
|
this.marked = false;
|
||||||
|
this.marked = false;
|
||||||
this.pathCost = Float.MAX_VALUE;
|
this.pathCost = Float.MAX_VALUE;
|
||||||
this.parentNode = null;
|
this.parentNode = null;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +21,8 @@ public class Label implements Comparable<Label> {
|
||||||
public Node getNode() { return this.node; }
|
public Node getNode() { return this.node; }
|
||||||
public boolean isMarked() { return this.marked; }
|
public boolean isMarked() { return this.marked; }
|
||||||
public void mark() { this.marked = true; }
|
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 float getPathCost() { return this.pathCost; }
|
||||||
public void setPathCost(float newCost) { this.pathCost = newCost; }
|
public void setPathCost(float newCost) { this.pathCost = newCost; }
|
||||||
public Node getParentNode() { return this.parentNode; }
|
public Node getParentNode() { return this.parentNode; }
|
||||||
|
|
Loading…
Reference in a new issue