Dijkstra v0.3
This commit is contained in:
parent
ad0e3a294f
commit
7ec0ebf44e
1 changed files with 31 additions and 12 deletions
|
@ -1,7 +1,11 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;//trier tout ça
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
@ -13,42 +17,57 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = new ShortestPathSolution(data,Status.UNKNOWN);//modifié
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|
||||||
//initialisation
|
//initialisation
|
||||||
BinaryHeap<Integer> tas=new BinaryHeap<Integer>();
|
BinaryHeap<Integer> tas=new BinaryHeap<Integer>();//type comparable nécessaire (donc pas Arc)
|
||||||
Label[] tablabel=new Label[data.getGraph().size()];
|
Label[] tablabel=new Label[data.getGraph().size()];
|
||||||
for (int i=0;i<tablabel.length;i++) {
|
for (int i=0;i<tablabel.length;i++) {
|
||||||
tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
|
tablabel[i]=new Label(data.getGraph().get(i),null,Integer.MAX_VALUE);//non marqué par défaut
|
||||||
}
|
}//dans le tablabel[idnode] on peut retrouver node
|
||||||
tablabel[data.getOrigin().getId()].cout=0;
|
tablabel[data.getOrigin().getId()].cout=0;
|
||||||
tas.insert(data.getOrigin().getId());
|
tas.insert(data.getOrigin().getId());
|
||||||
|
|
||||||
boolean exnonmar=true;//existe sommets non marqués
|
int nomark=tablabel.length;//existe sommets non marqués (ou à coût infini?)(pas optimal?)
|
||||||
int x;//id du node qu'on étudie
|
int x;//id du node qu'on étudie
|
||||||
|
|
||||||
|
if (data.getOrigin()==data.getDestination()) {
|
||||||
|
//retourne un graphe avec juste un node si besoin de gérer ce cas particulier
|
||||||
|
//(à vérifier, peut-être qu'une fois le reste fini ce ne sera pas nécessaire)
|
||||||
|
Path chemin= new Path(data.getGraph(),data.getOrigin());
|
||||||
|
solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
|
||||||
|
nomark=0;
|
||||||
|
}
|
||||||
|
|
||||||
//itérations
|
//itérations
|
||||||
while (exnonmar){
|
while (nomark>0){
|
||||||
x=tas.deleteMin();
|
x=tas.deleteMin();
|
||||||
tablabel[x].marque=true;
|
tablabel[x].marque=true;
|
||||||
for (Arc arcy: data.getGraph().get(x).getSuccessors()) {
|
nomark--;
|
||||||
|
for (Arc arcy: tablabel[x].sommet_courant.getSuccessors()) {
|
||||||
if (!tablabel[arcy.getDestination().getId()].marque) {
|
if (!tablabel[arcy.getDestination().getId()].marque) {
|
||||||
if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
|
if (tablabel[arcy.getDestination().getId()].cout>tablabel[x].cout+(int)arcy.getMinimumTravelTime()) {
|
||||||
tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
|
tablabel[arcy.getDestination().getId()].cout=tablabel[x].cout+(int)arcy.getMinimumTravelTime();
|
||||||
|
tablabel[arcy.getDestination().getId()].pere=arcy;//ligne non dans le poly
|
||||||
try{
|
try{
|
||||||
tas.remove(arcy.getDestination().getId());
|
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
|
} 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());
|
tas.insert(arcy.getDestination().getId());
|
||||||
}
|
}
|
||||||
}
|
}//s'aider de l'algorithme de Bellman-Ford déjà fait
|
||||||
}
|
}
|
||||||
|
|
||||||
exnonmar=false;//pas ici; comment savoir quand le changer?
|
//penser à gérer le cas "aucun chemin n'existe" (avec l'initialisation par défaut?)
|
||||||
//penser à gérer le cas "aucun chemin n'existe"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//créer le chemin solution grâce au tas
|
//créer le chemin solution grâce au tas (le tas nécessite un type comparable donc l'id des nodes)
|
||||||
|
List<Arc> bonsarcs=new ArrayList<Arc>();//pourquoi une erreur ici?
|
||||||
|
while (!tas.isEmpty()) {
|
||||||
|
bonsarcs.add(tablabel[tas.deleteMin()].pere);//un node de plus qu'il n'y a d'arcs, vérifier ce que ça donne
|
||||||
|
}//est-ce que le cas "un seul node" marche bien en renvoyant un unique arc null?
|
||||||
|
Path chemin= new Path(data.getGraph(),bonsarcs);
|
||||||
|
solution = new ShortestPathSolution(data,Status.OPTIMAL,chemin);
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue