From 8199e937480d51e9b0ee0acec464b73ab1aeb178 Mon Sep 17 00:00:00 2001 From: Adrien Barbanson Date: Mon, 3 May 2021 09:36:48 +0200 Subject: [PATCH] Fin bilan dijkstra --- .../shortestpath/DijkstraAlgorithm.java | 132 +++++++++++++++++- .../java/org/insa/graphs/model/Label.java | 49 +++++++ 2 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 be-graphes-model/src/main/java/org/insa/graphs/model/Label.java 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 bacb8e3..bdf40e0 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,5 +1,16 @@ package org.insa.graphs.algorithm.shortestpath; +import java.util.ArrayList; +import java.util.Collections; + +import org.insa.graphs.algorithm.AbstractInputData.Mode; +import org.insa.graphs.algorithm.AbstractSolution.Status; +import org.insa.graphs.algorithm.utils.BinaryHeap; +import org.insa.graphs.model.Arc; +import org.insa.graphs.model.Label; +import org.insa.graphs.model.Node; +import org.insa.graphs.model.Path; + public class DijkstraAlgorithm extends ShortestPathAlgorithm { public DijkstraAlgorithm(ShortestPathData data) { @@ -9,9 +20,124 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { @Override protected ShortestPathSolution doRun() { final ShortestPathData data = getInputData(); - ShortestPathSolution solution = null; - // TODO: - return solution; + + int numberOfNodes = data.getGraph().size(); + + double shortestCostToDestination = Double.POSITIVE_INFINITY; + + // it's the cost of the last marked node. In order to stop algorithm when it's not + // useful + double lastMarkedNodeCost = 0; + + // Dijkstra Init + Label[] labels = new Label[numberOfNodes]; + + + for(Node node : data.getGraph().getNodes()) { + labels[node.getId()] = new Label(node); + } + + // Let's set the origin cost at 0 + labels[data.getOrigin().getId()].setNewCost(null, 0d); + + // We need to add a binaryMinHeap to get min at each iteration + BinaryHeap