Implémentation du Dijkstra en cours

This commit is contained in:
Nabzzz 2020-03-27 19:42:31 +01:00
parent 1e63964e41
commit 48b3f25718
4 changed files with 119 additions and 1 deletions

View file

@ -1,5 +1,17 @@
package org.insa.graphs.algorithm.shortestpath;
import java.util.Arrays;
import java.util.Collections;
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 {
public DijkstraAlgorithm(ShortestPathData data) {
@ -10,7 +22,60 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
protected ShortestPathSolution doRun() {
final ShortestPathData data = getInputData();
ShortestPathSolution solution = null;
// TODO:
Graph graph = data.getGraph();
final int nbNodes=graph.size();
BinaryHeap<Node> tas=new BinaryHeap<Node>();
Label []tabLabel=new Label[nbNodes];
//Initilisation
for(Node n:graph.getNodes())
{
tabLabel[n.getId()]=new Label(n);
}
tabLabel[data.getOrigin().getId()].setCout(0);
tas.insert(data.getOrigin());
int nbMarque=0;
while(nbMarque!=nbNodes)
{
Node X=tas.findMin();
tabLabel[X.getId()].setMarque(true);
nbMarque++;
for(Arc a: X.getSuccessors())
{
Node Y=a.getDestination();
if(!tabLabel[Y.getId()].isMarque())
{
//double cout_avant=tabLabel[Y.getId()].getCout();
if((tabLabel[X.getId()].getCout()+a.getLength())<tabLabel[Y.getId()].getCout())
{
tabLabel[Y.getId()].setCout(tabLabel[X.getId()].getCout()+a.getLength());
tas.insert(Y);
tabLabel[Y.getId()].setPere(a);
}
}
}
}
// Destination has no predecessor, the solution is infeasible...
if (tabLabel[data.getDestination().getId()].getPere() == null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
ArrayList<Arc> arcs = new ArrayList<>();
Arc arc = tabLabel[data.getDestination().getId()].getPere();
while (arc != null) {
arcs.add(arc);
arc = tabLabel[arc.getOrigin().getId()].getPere();
}
// Reverse the path...
Collections.reverse(arcs);
// Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
}

View file

@ -0,0 +1,53 @@
package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc;
public class Label {
private Node sommetCourant;
private boolean isMarque;
private double cout;
private Arc pere;
public Label(Node sommetCourant)
{
this.sommetCourant=sommetCourant;
this.setCout(Double.POSITIVE_INFINITY);
this.setMarque(false);
this.setPere(null);
}
public Arc getPere() {
return pere;
}
public void setPere(Arc pere) {
this.pere = pere;
}
public boolean isMarque() {
return isMarque;
}
public void setMarque(boolean isMarque) {
this.isMarque = isMarque;
}
public Node getSommetCourant() {
return sommetCourant;
}
public double getCout() {
return cout;
}
public void setCout(double cout) {
this.cout = cout;
}
}