Dijkstra final
This commit is contained in:
parent
3241ca915a
commit
23e82a6ba0
3 changed files with 37 additions and 45 deletions
|
@ -15,6 +15,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
|
||||
public DijkstraAlgorithm(ShortestPathData data) {
|
||||
super(data);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,71 +24,63 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
ShortestPathSolution solution = null;
|
||||
// TODO:
|
||||
|
||||
ArrayList<Label> List = new ArrayList<Label>(); //List de labels
|
||||
ArrayList<Label> List_Label = new ArrayList<Label>(data.getGraph().size()); //Liste de labels
|
||||
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
|
||||
ArrayList<Arc> arcs = new ArrayList<Arc>();
|
||||
ArrayList<Arc> Arcs = new ArrayList<Arc>();
|
||||
|
||||
/* Initialise nos label */
|
||||
for (Node x: data.getGraph().getNodes())
|
||||
{
|
||||
Label a= new Label(x,Double.MAX_VALUE,null);
|
||||
List.add(a);
|
||||
if(x != data.getOrigin()){
|
||||
Label a= new Label(x,Double.MAX_VALUE,null);
|
||||
List_Label.add(x.getId(), a);
|
||||
}
|
||||
else{
|
||||
Label a = new Label(data.getOrigin(), 0, null);
|
||||
List_Label.add(data.getOrigin().getId(), a);
|
||||
}
|
||||
|
||||
}
|
||||
List.get(data.getOrigin().getId()).setCost(0);
|
||||
Tas.insert(List.get(data.getOrigin().getId()));
|
||||
Tas.insert(List_Label.get(data.getOrigin().getId()));
|
||||
Label x;
|
||||
Label pre =null;
|
||||
|
||||
int i =0;
|
||||
while (!List.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
||||
|
||||
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
||||
x = Tas.findMin();
|
||||
x.setMarque(true);
|
||||
Tas.deleteMin();
|
||||
if(i ==0){pre= new Label(data.getOrigin(), Double.MAX_VALUE, x.getSommet().getSuccessors().get(0)); i++;}//Permet d'initialiser le pre pour faire avancer l'algo
|
||||
|
||||
|
||||
for(Arc suivant : x.getSommet().getSuccessors()){
|
||||
Node y = suivant.getDestination();
|
||||
for (Label l : List){
|
||||
if (l.getSommet()==y){
|
||||
if(!l.isMarque()){
|
||||
Boolean changé = false;
|
||||
if (x.getCost()+data.getCost(suivant) <= l.getCost()){
|
||||
changé = true;
|
||||
}
|
||||
if(changé){
|
||||
if (l.getCost() <= data.getCost(pre.getParent())){
|
||||
Tas.remove(pre);
|
||||
}
|
||||
Label l=List_Label.get(suivant.getDestination().getId());
|
||||
if(!l.isMarque()){
|
||||
Boolean changé = false;
|
||||
if (x.getCost()+data.getCost(suivant) < l.getCost()){
|
||||
changé = true;
|
||||
}
|
||||
if(changé){
|
||||
if (l.getCost() != Double.MAX_VALUE){
|
||||
|
||||
l.setCost(Math.min(l.getCost(), x.getCost()+data.getCost(suivant)));
|
||||
pre = l;
|
||||
Tas.insert(l);
|
||||
l.setParent(suivant);
|
||||
l.setMarque(true);
|
||||
notifyNodeReached(suivant.getDestination());
|
||||
|
||||
Tas.remove(l);
|
||||
}
|
||||
l.setCost(x.getCost()+data.getCost(suivant));
|
||||
Tas.insert(l);
|
||||
l.setParent(suivant);
|
||||
|
||||
notifyNodeReached(suivant.getDestination());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Label dest =List.get(data.getDestination().getId());
|
||||
Label dest =List_Label.get(data.getDestination().getId());
|
||||
|
||||
int nbarc =0;
|
||||
while (dest.getParent() != null){
|
||||
System.out.println(dest.getParent());
|
||||
arcs.add(dest.getParent());
|
||||
dest = List.get(dest.getParent().getOrigin().getId());
|
||||
nbarc++;
|
||||
Arcs.add(dest.getParent());
|
||||
dest = List_Label.get(dest.getParent().getOrigin().getId());
|
||||
}
|
||||
|
||||
|
||||
System.out.println("nbarc: "+ nbarc);
|
||||
System.out.println(data.getDestination());
|
||||
System.out.println(arcs);
|
||||
Collections.reverse(arcs);
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs));
|
||||
Collections.reverse(Arcs);
|
||||
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
|
||||
return solution;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
|
Binary file not shown.
Loading…
Reference in a new issue