Dijkstra final
This commit is contained in:
parent
112c046292
commit
107f373c06
2 changed files with 23 additions and 21 deletions
|
@ -1,9 +1,11 @@
|
|||
package org.insa.graphs.algorithm.shortestpath;
|
||||
|
||||
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||
|
||||
import java.lang.invoke.LambdaConversionException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.insa.graphs.algorithm.utils.BinaryHeap;
|
||||
import org.insa.graphs.model.Arc;
|
||||
import org.insa.graphs.model.Node;
|
||||
|
@ -25,6 +27,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
BinaryHeap<Label> Tas = new BinaryHeap<Label>();
|
||||
ArrayList<Arc> arcs = new ArrayList<Arc>();
|
||||
|
||||
/* Initialise nos label */
|
||||
for (Node x: data.getGraph().getNodes())
|
||||
{
|
||||
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);
|
||||
Tas.insert(List.get(data.getOrigin().getId()));
|
||||
int i = 1;
|
||||
Label x;
|
||||
Label pre =null;
|
||||
|
||||
while (List.get(data.getDestination().getId()).isMarque()){
|
||||
//System.out.println(List);
|
||||
int i =0;
|
||||
while (!List.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()){
|
||||
if (x.getCost()+data.getCost(suivant) <= l.getCost()){
|
||||
changé = true;
|
||||
}
|
||||
if(changé){
|
||||
if (l.getCost() < data.getCost(pre.getParent())){
|
||||
if (l.getCost() <= data.getCost(pre.getParent())){
|
||||
Tas.remove(pre);
|
||||
System.out.println("remove");
|
||||
}
|
||||
|
||||
l.setCost(Math.min(l.getCost(), x.getCost()+data.getCost(suivant)));
|
||||
|
@ -61,33 +64,32 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
Tas.insert(l);
|
||||
l.setParent(suivant);
|
||||
l.setMarque(true);
|
||||
// System.out.println(List);
|
||||
notifyNodeReached(suivant.getDestination());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
Label dest =List.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++;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
return solution;
|
||||
}
|
||||
|
||||
|
||||
public boolean MarqueExiste(ArrayList<Label> list){
|
||||
boolean existe = false;
|
||||
for (Label l : list){
|
||||
if (l.isMarque() == false){
|
||||
existe = true;
|
||||
}
|
||||
}
|
||||
return existe;
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in a new issue