Dijkstra beta0.2
This commit is contained in:
parent
2a5ac7f244
commit
ad0e3a294f
1 changed files with 20 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.model.Arc;
|
||||
|
||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||
|
||||
|
@ -16,35 +17,37 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// TODO:
|
||||
|
||||
//initialisation
|
||||
BinaryHeap tas=new BinaryHeap();
|
||||
BinaryHeap<Integer> tas=new BinaryHeap<Integer>();
|
||||
Label[] tablabel=new Label[data.getGraph().size()];
|
||||
for (int i=0;i<tablabel.length;i++) {
|
||||
tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);
|
||||
tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
|
||||
}
|
||||
tablabel[data.getOrigin().getId()].cout=0;
|
||||
tas.insert(data.getOrigin().getId());
|
||||
|
||||
boolean exnonmar=true;
|
||||
int min;
|
||||
boolean exnonmar=true;//existe sommets non marqués
|
||||
int x;//id du node qu'on étudie
|
||||
//itérations
|
||||
while (exnonmar) {
|
||||
min=tas.deleteMin();
|
||||
tablabel[min].marque=true;
|
||||
for (successeurs y) {
|
||||
if (!tablabel[y].marque) {
|
||||
if (tablabel[y].cout>tablabel[x].cout+(int)tablabel[y].pere.getMinimumTravelTime()) {
|
||||
tablabel[y].cout=tablabel[x].cout+(int)tablabel[y].pere.getMinimumTravelTime();
|
||||
if (exist y in tas) {
|
||||
tas.update(y);
|
||||
}else {
|
||||
tas.insert(y);
|
||||
while (exnonmar){
|
||||
x=tas.deleteMin();
|
||||
tablabel[x].marque=true;
|
||||
for (Arc arcy: data.getGraph().get(x).getSuccessors()) {
|
||||
if (!tablabel[arcy.getDestination().getId()].marque) {
|
||||
if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
|
||||
tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
|
||||
try{
|
||||
tas.remove(arcy.getDestination().getId());
|
||||
} finally {//méthode fortement douteuse pour opérer update ou inssert selon si l'élément est déjà dans le tas ou pas
|
||||
tas.insert(arcy.getDestination().getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exnonmar=false;
|
||||
exnonmar=false;//pas ici; comment savoir quand le changer?
|
||||
//penser à gérer le cas "aucun chemin n'existe"
|
||||
}
|
||||
}
|
||||
//créer le chemin solution grâce au tas
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue