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;
|
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 {
|
||||||
|
|
||||||
|
|
@ -26,22 +27,16 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
// parent class ShortestPathAlgorithm)
|
// parent class 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,27 +57,25 @@ 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());
|
||||||
|
|
||||||
|
|
@ -90,7 +83,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
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);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue