From 6baec1d7dd53a51e96fccee4d60f05cb279a562a Mon Sep 17 00:00:00 2001 From: Paul ALNET Date: Fri, 3 May 2024 13:24:05 +0200 Subject: [PATCH] feat(dijkstra): mark labels as reached to increase performance --- .../shortestpath/DijkstraAlgorithm.java | 23 ++++++++----------- .../graphs/algorithm/shortestpath/Label.java | 4 ++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java index c0af685..7c7c148 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/DijkstraAlgorithm.java @@ -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()); } } } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java index 61c7993..8925660 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/shortestpath/Label.java @@ -5,6 +5,7 @@ import org.insa.graphs.model.Node; public class Label implements Comparable