Compare commits
No commits in common. "3241ca915a20a29531cf19adcbd24e163595a038" and "c2f4b22b0919294551516cbb5d9d79e8463f666c" have entirely different histories.
3241ca915a
...
c2f4b22b09
2 changed files with 22 additions and 23 deletions
|
@ -1,11 +1,9 @@
|
||||||
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;
|
||||||
|
@ -27,7 +25,6 @@ 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);
|
||||||
|
@ -35,28 +32,29 @@ 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;
|
||||||
|
|
||||||
int i =0;
|
while (List.get(data.getDestination().getId()).isMarque()){
|
||||||
while (!List.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
//System.out.println(List);
|
||||||
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
|
System.out.println("test");
|
||||||
|
|
||||||
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)));
|
||||||
|
@ -64,32 +62,33 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
Tas.insert(l);
|
Tas.insert(l);
|
||||||
l.setParent(suivant);
|
l.setParent(suivant);
|
||||||
l.setMarque(true);
|
l.setMarque(true);
|
||||||
notifyNodeReached(suivant.getDestination());
|
// System.out.println(List);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in a new issue