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) {
|
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);
|
||||||
List.get(data.getOrigin().getId()).setCost(0);
|
}
|
||||||
Tas.insert(List.get(data.getOrigin().getId()));
|
else{
|
||||||
Label x;
|
Label a = new Label(data.getOrigin(), 0, null);
|
||||||
Label pre =null;
|
List_Label.add(data.getOrigin().getId(), a);
|
||||||
|
}
|
||||||
|
|
||||||
int i =0;
|
}
|
||||||
while (!List.get(data.getDestination().getId()).isMarque() && !Tas.isEmpty()){
|
Tas.insert(List_Label.get(data.getOrigin().getId()));
|
||||||
|
Label x;
|
||||||
|
|
||||||
|
while (!List_Label.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(l);
|
||||||
Tas.remove(pre);
|
}
|
||||||
}
|
l.setCost(x.getCost()+data.getCost(suivant));
|
||||||
|
Tas.insert(l);
|
||||||
|
l.setParent(suivant);
|
||||||
|
|
||||||
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());
|
notifyNodeReached(suivant.getDestination());
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
Label dest =List.get(data.getDestination().getId());
|
|
||||||
|
|
||||||
int nbarc =0;
|
}
|
||||||
|
Label dest =List_Label.get(data.getDestination().getId());
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Binary file not shown.
Loading…
Reference in a new issue