1
0
Fork 0

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