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 86f02d1..9bae6bd 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 @@ -5,18 +5,20 @@ import org.insa.graphs.algorithm.utils.ElementNotFoundException; import org.insa.graphs.model.Arc; import org.insa.graphs.model.Graph; import org.insa.graphs.algorithm.utils.BinaryHeap; +import org.insa.graphs.model.Node; import org.insa.graphs.model.Path; import java.util.ArrayList; import java.util.Collections; - public class DijkstraAlgorithm extends ShortestPathAlgorithm { public DijkstraAlgorithm(ShortestPathData data) { super(data); } + final private boolean DEBUG = false; + @Override protected ShortestPathSolution doRun() { final ShortestPathData data = getInputData(); @@ -40,20 +42,26 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { int iterationCounter= 0; while (!heap.isEmpty() && labels[destination].isNotMarked()) { - Label parent = heap.deleteMin(); - parent.setMarked(true); - System.out.println(parent.getCost()); iterationCounter++; - for (Arc arc : graph.getNodes().get(parent.getAssociatedNode()).getSuccessors()) { - Label current = labels[arc.getDestination().getId()]; + + Label parentLabel = heap.deleteMin(); + parentLabel.setMarked(true); + Node parentNode = graph.getNodes().get(parentLabel.getAssociatedNode()); + notifyNodeMarked(parentNode); + + this.log("Cout marqué : " + parentLabel.getCost()); + this.log("Nb successeurs : " + parentNode.getNumberOfSuccessors()); + + for (Arc arc : parentNode.getSuccessors()) { + Label currentLabel = labels[arc.getDestination().getId()]; notifyNodeReached(arc.getDestination()); - if (current.isNotMarked() && data.isAllowed(arc)) { - this.updateCost(arc, parent, current, heap, data); + if (currentLabel.isNotMarked() && data.isAllowed(arc)) { + this.updateCost(arc, parentLabel, currentLabel, heap, data); } } } - System.out.println("Nb itérations: " + iterationCounter); + this.log("==============="); ShortestPathSolution solution = null; // Destination has no predecessor, the solution is infeasible... if (labels[destination].getFatherArc() == null) { @@ -76,9 +84,15 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { // Create the final solution. solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, arcs)); } - + this.log("Nb arcs solution : " + solution.getPath().getArcs().size()); + this.log("Nb itérations solution: " + iterationCounter); return solution; } + + private void log(String message) { + if (this.DEBUG) + System.out.println(message); + } private void updateCost(Arc arc, Label parent, Label current, BinaryHeap