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 93637d3..249294c 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 @@ -66,7 +66,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { double arc_cost = 0; for (Arc successorArc : x.getNode().getSuccessors()) { Label successor = labels.get(successorArc.getDestination().getId()); - + arc_cost = Double.MAX_VALUE; if (!successor.isMarked() && data.isAllowed(successorArc)) { // This loop serves to get the length of the arc as // we know its origin and destination @@ -76,7 +76,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // data.getcost(arc) returns a cost considering the mode chosen: // TIME or LENGTH // Similar to using getLength / getMinimumTravelTime - arc_cost = data.getCost(arc); + arc_cost = Math.min(data.getCost(arc), arc_cost); } } @@ -84,7 +84,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { if (successor.getCost() > possible_path_cost) { // Mise à jour du label successor.setPathCost(possible_path_cost); - successor.setParentNode(x.getNode()); + successor.setParentArc(successorArc); // Si le noeud n'a pas déjà été 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 @@ -101,7 +101,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { } } - if (labels.get(data.getDestination().getId()).getParentNode() == null) { + if (labels.get(data.getDestination().getId()).getParentArc() == null) { this.pathCost = 0; solution = new ShortestPathSolution(data, Status.INFEASIBLE); } @@ -109,32 +109,13 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // Create the path ... ArrayList arcs_path = new ArrayList<>(); + Arc arc = labels.get(data.getDestination().getId()).getParentArc(); + // We will find the path using the parent nodes, from the destination to the // origin - Label current_label = labels.get(data.getDestination().getId()); - Label parent_label = current_label; - - while(current_label != null && current_label.getNode().getId() != data.getOrigin().getId()) - { - // Find the label matching the parent node - parent_label = labels.get(current_label.getParentNode().getId()); - - // Knowing the parent node, get the arc between the parent and - // current node and add it to the path - if (parent_label != null) { - double minCost = Double.MAX_VALUE; - Arc minCostArc = null; - for (Arc arc : parent_label.getNode().getSuccessors()) { - if (arc.getDestination().getId() == current_label.getNode().getId() - && data.isAllowed(arc) - && data.getCost(arc) < minCost) { - minCost = Math.min(data.getCost(arc), minCost); - minCostArc = arc; - } - } - arcs_path.add(minCostArc); - } - current_label = parent_label; + while (arc != null && arc.getDestination().getId() != data.getOrigin().getId()) { + arcs_path.add(arc); + arc = labels.get(arc.getOrigin().getId()).getParentArc(); } notifyDestinationReached(data.getDestination()); 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 bf8d6e6..460519c 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 @@ -1,6 +1,7 @@ package org.insa.graphs.algorithm.shortestpath; import org.insa.graphs.model.Node; +import org.insa.graphs.model.Arc; import org.insa.graphs.algorithm.AbstractInputData.Mode; public class Label implements Comparable