Dijkstra fonctionnel
This commit is contained in:
parent
7fea8f8df7
commit
d7cca9ceee
2 changed files with 128 additions and 2 deletions
|
@ -1,4 +1,13 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
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.Node;
|
||||
import org.insa.graphs.model.Path;
|
||||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
|
@ -11,6 +20,84 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
final ShortestPathData data = getInputData();
|
||||
ShortestPathSolution solution = null;
|
||||
// TODO:
|
||||
|
||||
// Données
|
||||
|
||||
Graph graph = data.getGraph();
|
||||
List<Node> nodes = graph.getNodes();
|
||||
BinaryHeap<Label> tas = new BinaryHeap<Label>();
|
||||
ArrayList<Label> labels = new ArrayList<Label>();
|
||||
|
||||
|
||||
for (Node n : nodes) {
|
||||
labels.add(new Label(n,null));
|
||||
}
|
||||
|
||||
Node origine = data.getOrigin();
|
||||
notifyOriginProcessed(origine);
|
||||
labels.get(origine.getId()).setCost(0);
|
||||
tas.insert(labels.get(origine.getId()));
|
||||
|
||||
// Algorithme
|
||||
|
||||
Label currentLabel;
|
||||
while (tas.isEmpty() != true)
|
||||
{
|
||||
currentLabel = tas.findMin();
|
||||
tas.remove(currentLabel);
|
||||
Node currentNode = currentLabel.getCurrentNode();
|
||||
labels.get(currentNode.getId()).setMark(true);
|
||||
for (Arc newArc : currentNode.getSuccessors())
|
||||
{
|
||||
if (data.isAllowed(newArc))
|
||||
{
|
||||
Label destLabel = labels.get(newArc.getDestination().getId());
|
||||
if (!destLabel.getMark())
|
||||
{
|
||||
Node destNode = destLabel.getCurrentNode();
|
||||
notifyNodeReached(destNode);
|
||||
Double currentCost = currentLabel.getCost();
|
||||
Double destCost = destLabel.getCost();
|
||||
Double arcCost = data.getCost(newArc);
|
||||
if (destCost > currentCost+ arcCost)
|
||||
{
|
||||
|
||||
if (destCost != Double.MAX_VALUE)
|
||||
{
|
||||
tas.remove(destLabel);
|
||||
}
|
||||
destLabel.setCost(currentCost+arcCost);
|
||||
destLabel.setFatherArc(newArc);
|
||||
tas.insert(destLabel);
|
||||
labels.set(destNode.getId(),destLabel);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
notifyNodeMarked(currentNode);
|
||||
}
|
||||
|
||||
Arc currentArc=labels.get(data.getDestination().getId()).getFatherArc();
|
||||
if (currentArc == null) {
|
||||
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||
}
|
||||
else {
|
||||
|
||||
notifyDestinationReached(data.getDestination());
|
||||
|
||||
ArrayList<Arc> arcs = new ArrayList<>();
|
||||
while (currentArc != null) {
|
||||
arcs.add(currentArc);
|
||||
Arc newArc=labels.get(currentArc.getOrigin().getId()).getFatherArc();
|
||||
currentArc = newArc;
|
||||
}
|
||||
Collections.reverse(arcs);
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
import org.insa.graphs.model.*;
|
||||
|
||||
public class Label
|
||||
public class Label implements Comparable<Label>
|
||||
{
|
||||
|
||||
private Node currentNode;
|
||||
|
@ -22,5 +22,44 @@
|
|||
return this.cost;
|
||||
}
|
||||
|
||||
public boolean getMark()
|
||||
{
|
||||
return this.mark;
|
||||
}
|
||||
|
||||
public Arc getFatherArc()
|
||||
{
|
||||
return this.fatherArc;
|
||||
}
|
||||
|
||||
public Node getCurrentNode()
|
||||
{
|
||||
return this.currentNode;
|
||||
}
|
||||
|
||||
public void setCost(double cout)
|
||||
{
|
||||
this.cost = cout;
|
||||
}
|
||||
|
||||
public void setMark(boolean marque)
|
||||
{
|
||||
this.mark = marque;
|
||||
}
|
||||
|
||||
public void setFatherArc(Arc arcpere)
|
||||
{
|
||||
this.fatherArc = arcpere;
|
||||
}
|
||||
|
||||
public void setCurrentNode(Node noeudcourant)
|
||||
{
|
||||
this.currentNode = noeudcourant;
|
||||
}
|
||||
|
||||
public int compareTo(Label other)
|
||||
{
|
||||
return Double.compare(getCost(), other.getCost());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue