implémentation Dijkstra

This commit is contained in:
Auriane Lartigue 2020-04-13 10:30:44 +02:00
parent d477427f51
commit d258b473c7
2 changed files with 50 additions and 26 deletions

View file

@ -23,7 +23,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Graph graph = data.getGraph();
final int nbNodes = graph.size();
BinaryHeap<Node> Tas = new BinaryHeap<Node>() ;
BinaryHeap<Label> Tas = new BinaryHeap<Label>() ;
Label[] TabLabel = new Label[nbNodes];
@ -33,25 +33,30 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
}
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
Tas.insert(data.getOrigin()); // on insert le sommet dans le tas
Tas.insert(TabLabel[data.getOrigin().getId()]); // on insert le label du sommet dans le tas
int nbMarque = 0; // correspond au nombre de sommet marqué
while(nbMarque != nbNodes) { // il existe des sommets non marqués
Node X = Tas.findMin(); // on prend le sommet du tas et on le stocke dans X
Label LabelX = TabLabel[X.getId()];
LabelX.setMarque(true);
nbMarque++;
//int nbMarque = 0; // correspond au nombre de sommet marqué
while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués
Node X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X
TabLabel[X.getId()].setMarque(true); // on le marque
Tas.remove(TabLabel[X.getId()]);
//nbMarque++;
for(Arc a : X.getSuccessors() ){
Node Y = a.getDestination();
if(!(TabLabel[Y.getId()].isMarque())) {
Label LabelY = TabLabel[Y.getId()];
double aux = LabelY.getCost();
LabelY.setCost(Math.min(LabelY.getCost(), LabelX.getCost()+ a.getLength() ));
if (LabelY.getCost() != aux) { //cost(y) a été mis a jour
Tas.insert(Y);
LabelY.setFather(a);
}
if (data.isAllowed(a)) {
Node Y = a.getDestination();
if(!(TabLabel[Y.getId()].isMarque() ) ) {
if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() );
Tas.insert(TabLabel[Y.getId()]);
TabLabel[Y.getId()].setFather(a);
}
else if(TabLabel[Y.getId()].getCost() > TabLabel[X.getId()].getCost()+ a.getLength()) {
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() );
Tas.remove(TabLabel[Y.getId()]);
Tas.insert(TabLabel[Y.getId()]);
TabLabel[Y.getId()].setFather(a);
}
}
}
}
}
@ -60,14 +65,21 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Node dest = data.getDestination();
Label Labeldest = TabLabel[dest.getId()];
Arc a = Labeldest.getFather();
while (a != null ) {
arcs.add(a);
a = TabLabel[a.getOrigin().getId()].getFather();
System.out.println(a);
}
Collections.reverse(arcs);
new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
if(a==null) {
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
}
else {
while (a != null ) {
arcs.add(a);
a = TabLabel[a.getOrigin().getId()].getFather();
System.out.println(a);
}
Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
}
return solution;
}

View file

@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label{
public class Label implements java.lang.Comparable<Label> {
private Node sommetCourant ;
private boolean marque;
private double cost ;
@ -48,5 +48,17 @@ public class Label{
this.sommetCourant = sommetCourant;
}
public int compareTo(Label autre) {
if (this.getCost() < autre.getCost()) {
return -1;
}
else if (this.getCost() == autre.getCost() ) {
return 0;
}
else {
return 1;
}
}
}