implémentation Dijkstra
This commit is contained in:
parent
d477427f51
commit
d258b473c7
2 changed files with 50 additions and 26 deletions
|
@ -23,7 +23,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
Graph graph = data.getGraph();
|
Graph graph = data.getGraph();
|
||||||
final int nbNodes = graph.size();
|
final int nbNodes = graph.size();
|
||||||
BinaryHeap<Node> Tas = new BinaryHeap<Node>() ;
|
BinaryHeap<Label> Tas = new BinaryHeap<Label>() ;
|
||||||
|
|
||||||
Label[] TabLabel = new Label[nbNodes];
|
Label[] TabLabel = new Label[nbNodes];
|
||||||
|
|
||||||
|
@ -33,24 +33,29 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
TabLabel[data.getOrigin().getId()].setCost(0); // on met le cost du sommet à 0
|
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é
|
//int nbMarque = 0; // correspond au nombre de sommet marqué
|
||||||
|
while(!TabLabel[data.getDestination().getId()].isMarque() ) { // tant que la destination est non marqués
|
||||||
while(nbMarque != nbNodes) { // il existe des sommets non marqués
|
Node X = Tas.findMin().getSommetCourant(); // on prend le sommet du tas et on le stocke dans X
|
||||||
Node X = Tas.findMin(); // on prend le sommet du tas et on le stocke dans X
|
TabLabel[X.getId()].setMarque(true); // on le marque
|
||||||
Label LabelX = TabLabel[X.getId()];
|
Tas.remove(TabLabel[X.getId()]);
|
||||||
LabelX.setMarque(true);
|
//nbMarque++;
|
||||||
nbMarque++;
|
|
||||||
for(Arc a : X.getSuccessors() ){
|
for(Arc a : X.getSuccessors() ){
|
||||||
|
if (data.isAllowed(a)) {
|
||||||
Node Y = a.getDestination();
|
Node Y = a.getDestination();
|
||||||
if(!(TabLabel[Y.getId()].isMarque() ) ) {
|
if(!(TabLabel[Y.getId()].isMarque() ) ) {
|
||||||
Label LabelY = TabLabel[Y.getId()];
|
if(TabLabel[Y.getId()].getCost()==Double.POSITIVE_INFINITY ) { //Y n'est pas dans le tas
|
||||||
double aux = LabelY.getCost();
|
TabLabel[Y.getId()].setCost( TabLabel[X.getId()].getCost()+ a.getLength() );
|
||||||
LabelY.setCost(Math.min(LabelY.getCost(), LabelX.getCost()+ a.getLength() ));
|
Tas.insert(TabLabel[Y.getId()]);
|
||||||
if (LabelY.getCost() != aux) { //cost(y) a été mis a jour
|
TabLabel[Y.getId()].setFather(a);
|
||||||
Tas.insert(Y);
|
}
|
||||||
LabelY.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,6 +65,11 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Node dest = data.getDestination();
|
Node dest = data.getDestination();
|
||||||
Label Labeldest = TabLabel[dest.getId()];
|
Label Labeldest = TabLabel[dest.getId()];
|
||||||
Arc a = Labeldest.getFather();
|
Arc a = Labeldest.getFather();
|
||||||
|
|
||||||
|
if(a==null) {
|
||||||
|
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||||
|
}
|
||||||
|
else {
|
||||||
while (a != null ) {
|
while (a != null ) {
|
||||||
arcs.add(a);
|
arcs.add(a);
|
||||||
a = TabLabel[a.getOrigin().getId()].getFather();
|
a = TabLabel[a.getOrigin().getId()].getFather();
|
||||||
|
@ -67,7 +77,9 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.reverse(arcs);
|
Collections.reverse(arcs);
|
||||||
new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
||||||
|
}
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.insa.graphs.algorithm.shortestpath;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class Label{
|
public class Label implements java.lang.Comparable<Label> {
|
||||||
private Node sommetCourant ;
|
private Node sommetCourant ;
|
||||||
private boolean marque;
|
private boolean marque;
|
||||||
private double cost ;
|
private double cost ;
|
||||||
|
@ -48,5 +48,17 @@ public class Label{
|
||||||
this.sommetCourant = sommetCourant;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue