Dijkstra final

This commit is contained in:
El Haji Fofana 2023-05-15 10:21:46 +02:00
parent 3241ca915a
commit 23e82a6ba0
3 changed files with 37 additions and 45 deletions

View file

@ -15,6 +15,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
public DijkstraAlgorithm(ShortestPathData data) { public DijkstraAlgorithm(ShortestPathData data) {
super(data); super(data);
} }
@Override @Override
@ -23,71 +24,63 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
ShortestPathSolution solution = null; ShortestPathSolution solution = null;
// TODO: // 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>(); BinaryHeap<Label> Tas = new BinaryHeap<Label>();
ArrayList<Arc> arcs = new ArrayList<Arc>(); ArrayList<Arc> Arcs = new ArrayList<Arc>();
/* Initialise nos label */ /* Initialise nos label */
for (Node x: data.getGraph().getNodes()) for (Node x: data.getGraph().getNodes())
{ {
Label a= new Label(x,Double.MAX_VALUE,null); if(x != data.getOrigin()){
List.add(a); 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_Label.get(data.getOrigin().getId()));
Tas.insert(List.get(data.getOrigin().getId()));
Label x; Label x;
Label pre =null;
while (!List_Label.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
int i =0;
while (!List.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
x = Tas.findMin(); x = Tas.findMin();
x.setMarque(true); x.setMarque(true);
Tas.deleteMin(); 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()){ for(Arc suivant : x.getSommet().getSuccessors()){
Node y = suivant.getDestination(); Label l=List_Label.get(suivant.getDestination().getId());
for (Label l : List){ if(!l.isMarque()){
if (l.getSommet()==y){ Boolean changé = false;
if(!l.isMarque()){ if (x.getCost()+data.getCost(suivant) < l.getCost()){
Boolean changé = false; changé = true;
if (x.getCost()+data.getCost(suivant) <= l.getCost()){ }
changé = true; if(changé){
} if (l.getCost() != Double.MAX_VALUE){
if(changé){
if (l.getCost() <= data.getCost(pre.getParent())){
Tas.remove(pre);
}
l.setCost(Math.min(l.getCost(), x.getCost()+data.getCost(suivant))); Tas.remove(l);
pre = l;
Tas.insert(l);
l.setParent(suivant);
l.setMarque(true);
notifyNodeReached(suivant.getDestination());
} }
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){ while (dest.getParent() != null){
System.out.println(dest.getParent()); Arcs.add(dest.getParent());
arcs.add(dest.getParent()); dest = List_Label.get(dest.getParent().getOrigin().getId());
dest = List.get(dest.getParent().getOrigin().getId());
nbarc++;
} }
Collections.reverse(Arcs);
System.out.println("nbarc: "+ nbarc); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), Arcs));
System.out.println(data.getDestination());
System.out.println(arcs);
Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs));
return solution; return solution;
} }

View file

@ -1,6 +1,5 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import java.util.ArrayList;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;