Implemented dijkstra algo
This commit is contained in:
parent
d1b49fbafc
commit
b2aa8243cc
2 changed files with 133 additions and 1 deletions
|
@ -1,5 +1,16 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.AbstractSolution;
|
||||||
|
import org.insa.graphs.algorithm.utils.ElementNotFoundException;
|
||||||
|
import org.insa.graphs.model.Arc;
|
||||||
|
import org.insa.graphs.model.Graph;
|
||||||
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
|
@ -9,9 +20,78 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
|
|
||||||
|
Graph graph = data.getGraph();
|
||||||
|
final int nbNodes = graph.size();
|
||||||
|
|
||||||
|
// Initialize array of Labels.
|
||||||
|
Label[] labels = new Label[nbNodes];
|
||||||
|
for (int i = 0; i < nbNodes; i++) {
|
||||||
|
labels[i] = new Label(i);
|
||||||
|
}
|
||||||
|
final int origin = data.getOrigin().getId();
|
||||||
|
final int destination = data.getDestination().getId();
|
||||||
|
labels[origin].setCost(0);
|
||||||
|
|
||||||
|
// Initialize the heap
|
||||||
|
BinaryHeap<Label> heap = new BinaryHeap<>();
|
||||||
|
heap.insert(labels[origin]);
|
||||||
|
|
||||||
|
int iterationCounter= 0;
|
||||||
|
|
||||||
|
while (!heap.isEmpty() && labels[destination].isNotMarked()) {
|
||||||
|
Label parent = heap.deleteMin();
|
||||||
|
parent.setMarked(true);
|
||||||
|
System.out.println(parent.getCost());
|
||||||
|
iterationCounter++;
|
||||||
|
for (Arc arc : graph.getNodes().get(parent.getAssociatedNode()).getSuccessors()) {
|
||||||
|
Label current = labels[arc.getDestination().getId()];
|
||||||
|
notifyNodeReached(arc.getDestination());
|
||||||
|
|
||||||
|
if (current.isNotMarked() && data.isAllowed(arc)) {
|
||||||
|
this.updateCost(arc, parent, current, heap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Nb itérations: " + iterationCounter);
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = null;
|
||||||
// TODO:
|
|
||||||
|
// Destination has no predecessor, the solution is infeasible...
|
||||||
|
if (labels[destination].getFatherArc() == null) {
|
||||||
|
solution = new ShortestPathSolution(data, AbstractSolution.Status.INFEASIBLE);
|
||||||
|
} else {
|
||||||
|
// The destination has been found, notify the observers.
|
||||||
|
notifyDestinationReached(data.getDestination());
|
||||||
|
|
||||||
|
// Create the path from the array of predecessors...
|
||||||
|
ArrayList<Arc> arcs = new ArrayList<>();
|
||||||
|
Arc arc = labels[destination].getFatherArc();
|
||||||
|
while (arc != null) {
|
||||||
|
arcs.add(arc);
|
||||||
|
arc = labels[arc.getOrigin().getId()].getFatherArc();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reverse the path...
|
||||||
|
Collections.reverse(arcs);
|
||||||
|
|
||||||
|
// Create the final solution.
|
||||||
|
solution = new ShortestPathSolution(data, AbstractSolution.Status.OPTIMAL, new Path(graph, arcs));
|
||||||
|
}
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateCost(Arc arc, Label parent, Label current, BinaryHeap<Label> heap) {
|
||||||
|
double newCost = parent.getCost() + arc.getLength();
|
||||||
|
if (newCost < current.getCost()) {
|
||||||
|
try {
|
||||||
|
heap.remove(current);
|
||||||
|
} catch (ElementNotFoundException e) {
|
||||||
|
}
|
||||||
|
current.setCost(newCost);
|
||||||
|
current.setFatherArc(arc);
|
||||||
|
heap.insert(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.model.Arc;
|
||||||
|
|
||||||
|
public class Label implements Comparable<Label> {
|
||||||
|
private int associatedNode;
|
||||||
|
private double cost;
|
||||||
|
|
||||||
|
private boolean marked;
|
||||||
|
|
||||||
|
private Arc fatherArc;
|
||||||
|
|
||||||
|
public Label(int associatedNode) {
|
||||||
|
this.associatedNode = associatedNode;
|
||||||
|
this.marked = false;
|
||||||
|
this.cost = Double.POSITIVE_INFINITY;
|
||||||
|
this.fatherArc = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAssociatedNode() {
|
||||||
|
return associatedNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getCost() {
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCost(double cost) {
|
||||||
|
this.cost = cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNotMarked() {
|
||||||
|
return !marked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMarked(boolean marked) {
|
||||||
|
this.marked = marked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Arc getFatherArc() {
|
||||||
|
return fatherArc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFatherArc(Arc fatherArc) {
|
||||||
|
this.fatherArc = fatherArc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Label label) {
|
||||||
|
return this.cost - label.getCost() > 0 ? 1 : -1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue