From 14240bbf30b9cb1c2753a090d7290fc0e53693bc Mon Sep 17 00:00:00 2001 From: georgia Date: Wed, 12 Apr 2023 16:14:38 +0200 Subject: [PATCH] dijkstra --- .../shortestpath/DijkstraAlgorithm.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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 7c9ff5d..d52e3f0 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 @@ -1,8 +1,13 @@ package org.insa.graphs.algorithm.shortestpath; import org.insa.graphs.model.Node; import org.insa.graphs.model.Arc; +import org.insa.graphs.algorithm.AbstractSolution.Status; +import org.insa.graphs.model.Graph; + +import org.insa.graphs.model.Path; import java.util.ArrayList; +import java.util.Collections; import org.insa.graphs.algorithm.utils.BinaryHeap; @@ -28,7 +33,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { Label label_origine = tab.get(data.getOrigin().getId()); label_origine.setCoutmin(0); - Label label_dest =tab.get(data.getDestination().getId()) + Label label_dest =tab.get(data.getDestination().getId()); /*insertion de label origine dans le tas */ tas.insert(label_origine); Label x; @@ -53,6 +58,29 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { } } } + // On n'a pas trouvé de solution, on est sorti de la boucle car le tas est vide + if (label_dest.getMarque()==false) { + solution = new ShortestPathSolution(data, Status.INFEASIBLE); + } + else { + + // The destination has been found, notify the observers. + notifyDestinationReached(data.getDestination()); + + // Create the path from the array of predecessors... + ArrayList arcs = new ArrayList<>(); + Arc arc = label_dest.getPere(); + while (arc != null) { + arcs.add(arc); + arc = tab.get(arc.getOrigin().getId()).getPere(); + } + + // Reverse the path... + Collections.reverse(arcs); + + // Create the final solution. + solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs)); + } return solution;