Dijkstra final

This commit is contained in:
El Haji Fofana 2023-05-10 12:11:41 +02:00
parent 112c046292
commit 107f373c06
2 changed files with 23 additions and 21 deletions

View file

@ -1,9 +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.AbstractSolution.Status;
import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Collections;
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.Node; import org.insa.graphs.model.Node;
@ -25,6 +27,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
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 */
for (Node x: data.getGraph().getNodes()) for (Node x: data.getGraph().getNodes())
{ {
Label a= new Label(x,Double.MAX_VALUE,null); Label a= new Label(x,Double.MAX_VALUE,null);
@ -32,28 +35,28 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
} }
List.get(data.getOrigin().getId()).setCost(0); List.get(data.getOrigin().getId()).setCost(0);
Tas.insert(List.get(data.getOrigin().getId())); Tas.insert(List.get(data.getOrigin().getId()));
int i = 1;
Label x; Label x;
Label pre = null; Label pre =null;
while (List.get(data.getDestination().getId()).isMarque()){ int i =0;
//System.out.println(List); 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(); Node y = suivant.getDestination();
for (Label l : List){ for (Label l : List){
if (l.getSommet()==y){ if (l.getSommet()==y){
if(!l.isMarque()){ if(!l.isMarque()){
Boolean changé = false; Boolean changé = false;
if (x.getCost()+data.getCost(suivant) < l.getCost()){ if (x.getCost()+data.getCost(suivant) <= l.getCost()){
changé = true; changé = true;
} }
if(changé){ if(changé){
if (l.getCost() < data.getCost(pre.getParent())){ if (l.getCost() <= data.getCost(pre.getParent())){
Tas.remove(pre); Tas.remove(pre);
System.out.println("remove");
} }
l.setCost(Math.min(l.getCost(), x.getCost()+data.getCost(suivant))); l.setCost(Math.min(l.getCost(), x.getCost()+data.getCost(suivant)));
@ -61,33 +64,32 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
Tas.insert(l); Tas.insert(l);
l.setParent(suivant); l.setParent(suivant);
l.setMarque(true); l.setMarque(true);
// System.out.println(List); notifyNodeReached(suivant.getDestination());
} }
} }
} }
} }
} }
i++;
} }
Label dest =List.get(data.getDestination().getId()); Label dest =List.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.get(dest.getParent().getOrigin().getId()); dest = List.get(dest.getParent().getOrigin().getId());
nbarc++;
} }
System.out.println("nbarc: "+ nbarc);
System.out.println(data.getDestination()); System.out.println(data.getDestination());
System.out.println(arcs); System.out.println(arcs);
Collections.reverse(arcs);
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs)); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(data.getGraph(), arcs));
return solution; return solution;
} }
public boolean MarqueExiste(ArrayList<Label> list){
boolean existe = false;
for (Label l : list){
if (l.isMarque() == false){
existe = true;
}
}
return existe;
}
} }