Dijkstra with Hashmap

This commit is contained in:
Sebastien Moll 2026-05-12 17:54:35 +02:00
parent bf2c35eadd
commit 771dfb96b2

View file

@ -1,13 +1,14 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import java.util.HashMap;
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.Graph;
import org.insa.graphs.model.Path;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Path;
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -26,22 +27,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution = null;
// Initialize array of labels.
ArrayList<Label> labelsList = new ArrayList<Label>();
for (int i=0; i<nbNodes; i++) {
labelsList.add(createLabel(graph.get(i)));
}
// Initialize hashmap of labels.
HashMap<Integer, Label> labelsMap = new HashMap<Integer, Label>();
// Set cost of origin to zero
Label destinationNodeLabel = labelsList.get(data.getDestination().getId());
Label concurentNodeLabel = labelsList.get(data.getOrigin().getId());
Label concurentNodeLabel = createLabel(data.getOrigin());
concurentNodeLabel.setCoutRealise(0);
notifyOriginProcessed(data.getOrigin());
@ -52,9 +47,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
boolean found = false;
while (!found && !labelsHeap.isEmpty()) {
concurentNodeLabel = labelsHeap.deleteMin();
if (!concurentNodeLabel.getMarque()) {
if (!labelsMap.containsKey(concurentNodeLabel.getSommetCourant().getId())) {
notifyNodeMarked(concurentNodeLabel.getSommetCourant());
concurentNodeLabel.setMarque();
labelsMap.put(concurentNodeLabel.getSommetCourant().getId(), concurentNodeLabel);
for (Arc arc : concurentNodeLabel.getSommetCourant().getSuccessors()) {
// Small test to check allowed roads...
@ -62,27 +57,25 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
continue;
}
Label successorLabel = labelsList.get(arc.getDestination().getId());
Label successorLabel = createLabel(arc.getDestination());
Double newCost = concurentNodeLabel.getCoutRealise() + data.getCost(arc);
if (successorLabel.getCoutRealise() == Double.POSITIVE_INFINITY) {
notifyNodeReached(arc.getDestination());
}
if (newCost < successorLabel.getCoutRealise()) {
successorLabel.setPere(arc);
successorLabel.setCoutRealise(newCost);
labelsHeap.insert(successorLabel);
}
successorLabel.setPere(arc);
successorLabel.setCoutRealise(newCost);
labelsHeap.insert(successorLabel);
notifyNodeReached(successorLabel.getSommetCourant());
}
if (destinationNodeLabel.getMarque()) {
if (labelsMap.containsKey(data.getDestination().getId())) {
found = true;
}
}
}
// Destination has no predecessor, the solution is infeasible...
if (destinationNodeLabel.getPere() == null) {
if (concurentNodeLabel.getPere() == null) {
return new ShortestPathSolution(data, Status.INFEASIBLE);
}
Label destinationNodeLabel = labelsMap.get(data.getDestination().getId());
notifyDestinationReached(data.getDestination());
@ -90,7 +83,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
concurentNodeLabel = destinationNodeLabel;
while(concurentNodeLabel.getPere() != null) {
pathArcs.add(0, concurentNodeLabel.getPere());
concurentNodeLabel = labelsList.get(concurentNodeLabel.getPere().getOrigin().getId());
concurentNodeLabel = labelsMap.get(concurentNodeLabel.getPere().getOrigin().getId());
}
// Collections.reverse(pathArcs);