djikstra à verifier
This commit is contained in:
parent
857b0215f4
commit
8035821299
2 changed files with 61 additions and 20 deletions
|
|
@ -1,8 +1,14 @@
|
||||||
package org.insa.graphs.algorithm.shortestpath;
|
package org.insa.graphs.algorithm.shortestpath;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.insa.graphs.algorithm.AbstractSolution.Status;
|
||||||
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.Graph;
|
import org.insa.graphs.model.Graph;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
|
|
@ -30,29 +36,64 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
labels[node.getId()]=new Label(node);
|
labels[node.getId()]=new Label(node);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
BinaryHeap<Label> tas=new BinaryHeap<>();
|
BinaryHeap<Label> tas=new BinaryHeap<>();
|
||||||
|
labels[data.getOrigin().getId()].setCoutRealise(0);
|
||||||
|
tas.insert(labels[data.getOrigin().getId()]);
|
||||||
|
|
||||||
|
while(!tas.isEmpty()){
|
||||||
|
Label labelMin=tas.deleteMin();
|
||||||
|
labelMin.setMarque();
|
||||||
|
|
||||||
|
if (labelMin.getSommetCourant().equals(data.getDestination())){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Arc arc : labelMin.getSommetCourant().getSuccessors()) {
|
||||||
|
if (!data.isAllowed(arc)) continue;
|
||||||
|
|
||||||
|
Label labelSucc = labels[arc.getDestination().getId()];
|
||||||
|
|
||||||
|
if (!labelSucc.isMarque()) {
|
||||||
|
double nouveauCout = labelMin.getCoutRealise() + data.getCost(arc);
|
||||||
|
|
||||||
|
if (nouveauCout < labelSucc.getCoutRealise()) {
|
||||||
|
boolean dejaDansTas = Double.isFinite(labelSucc.getCoutRealise());
|
||||||
|
|
||||||
|
labelSucc.setCoutRealise(nouveauCout);
|
||||||
|
labelSucc.setPere(arc);
|
||||||
|
|
||||||
|
if (dejaDansTas) {
|
||||||
|
tas.remove(labelSucc);
|
||||||
|
} else {
|
||||||
|
notifyNodeReached(arc.getDestination());
|
||||||
|
}
|
||||||
|
tas.insert(labelSucc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Construction de la solution
|
||||||
|
if (labels[data.getDestination().getId()].getPere() == null) {
|
||||||
|
solution = new ShortestPathSolution(data, Status.INFEASIBLE);
|
||||||
|
} else {
|
||||||
|
notifyDestinationReached(data.getDestination());
|
||||||
|
|
||||||
|
ArrayList<Arc> arcs = new ArrayList<>();
|
||||||
|
Arc arc = labels[data.getDestination().getId()].getPere();
|
||||||
|
while (arc != null) {
|
||||||
|
arcs.add(arc);
|
||||||
|
arc = labels[arc.getOrigin().getId()].getPere();
|
||||||
|
}
|
||||||
|
Collections.reverse(arcs);
|
||||||
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: implement the Dijkstra algorithm
|
|
||||||
|
|
||||||
// when the algorithm terminates, return the solution that has been found
|
|
||||||
return solution;
|
return solution;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ public class Label implements Comparable<Label>{
|
||||||
public Label(Node sommetCourant) {
|
public Label(Node sommetCourant) {
|
||||||
this.sommetCourant=sommetCourant;
|
this.sommetCourant=sommetCourant;
|
||||||
this.marque=false;
|
this.marque=false;
|
||||||
this.coutRealise=0;
|
this.coutRealise=Double.POSITIVE_INFINITY ;
|
||||||
this.pere=null;
|
this.pere=null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue