forked from lebotlan/BE-Graphes
Dijkstra with Hashmap
This commit is contained in:
parent
bf2c35eadd
commit
771dfb96b2
1 changed files with 17 additions and 24 deletions
|
|
@ -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 {
|
||||
|
||||
|
|
@ -27,21 +28,15 @@ public class DijkstraAlgorithm extends 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,35 +57,33 @@ 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);
|
||||
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());
|
||||
|
||||
ArrayList<Arc> pathArcs = new ArrayList<Arc>();
|
||||
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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue