NOT FINISHED : Dijkstra, changed Label
This commit is contained in:
parent
52a8945a91
commit
f42e7edf5f
2 changed files with 82 additions and 8 deletions
|
|
@ -1,5 +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.model.Arc;
|
||||||
|
import org.insa.graphs.model.Graph;
|
||||||
|
import org.insa.graphs.model.Path;
|
||||||
|
|
||||||
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
public DijkstraAlgorithm(ShortestPathData data) {
|
public DijkstraAlgorithm(ShortestPathData data) {
|
||||||
|
|
@ -12,11 +21,58 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
// retrieve data from the input problem (getInputData() is inherited from the
|
// retrieve data from the input problem (getInputData() is inherited from the
|
||||||
// parent class ShortestPathAlgorithm)
|
// parent class ShortestPathAlgorithm)
|
||||||
final ShortestPathData data = getInputData();
|
final ShortestPathData data = getInputData();
|
||||||
|
Graph graph = data.getGraph();
|
||||||
|
|
||||||
|
final int nbNodes = graph.size();
|
||||||
|
|
||||||
// variable that will contain the solution of the shortest path problem
|
// variable that will contain the solution of the shortest path problem
|
||||||
ShortestPathSolution solution = null;
|
ShortestPathSolution solution = null;
|
||||||
|
|
||||||
// TODO: implement the Dijkstra algorithm
|
// Initialize array of labels.
|
||||||
|
ArrayList<Label> labelsList = new ArrayList<Label>();
|
||||||
|
for (int i=0; i<nbNodes; i++) {
|
||||||
|
labelsList.add(new Label(graph.get(i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set cost of origin to zero
|
||||||
|
Label destinationNodeLabel = labelsList.get(data.getDestination().getId());
|
||||||
|
Label concurentNodeLabel = labelsList.get(data.getOrigin().getId());
|
||||||
|
concurentNodeLabel.setCoutRealise(0);
|
||||||
|
concurentNodeLabel.setMarque();
|
||||||
|
|
||||||
|
BinaryHeap<Label> labelsHeap = new BinaryHeap<Label>();
|
||||||
|
|
||||||
|
boolean found = false;
|
||||||
|
for (int i = 0; !found && i < nbNodes; ++i) {
|
||||||
|
for (Arc arc : concurentNodeLabel.getSommetCourant().getSuccessors()) {
|
||||||
|
Label successorLabel = labelsList.get(arc.getDestination().getId());
|
||||||
|
successorLabel.setCoutRealise(arc.getLength());
|
||||||
|
// GERER LA SOMME DES COUTS POUR LE NOUVEAU ALBEL
|
||||||
|
if (arc.getLength() < successorLabel.getCost()) {
|
||||||
|
labelsHeap.remove(successorLabel);
|
||||||
|
}
|
||||||
|
successorLabel.setPere(arc);
|
||||||
|
labelsHeap.insert(successorLabel);
|
||||||
|
}
|
||||||
|
concurentNodeLabel = labelsHeap.deleteMin();
|
||||||
|
concurentNodeLabel.setMarque();
|
||||||
|
if (destinationNodeLabel.getMarque()) {
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyDestinationReached(data.getDestination());
|
||||||
|
ArrayList<Arc> pathArcs = new ArrayList<Arc>();
|
||||||
|
concurentNodeLabel = destinationNodeLabel;
|
||||||
|
while(concurentNodeLabel.getPere() != null) {
|
||||||
|
pathArcs.add(concurentNodeLabel.getPere());
|
||||||
|
concurentNodeLabel = labelsList.get(concurentNodeLabel.getPere().getOrigin().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.reverse(pathArcs);
|
||||||
|
|
||||||
|
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, pathArcs));
|
||||||
|
|
||||||
// when the algorithm terminates, return the solution that has been found
|
// when the algorithm terminates, return the solution that has been found
|
||||||
return solution;
|
return solution;
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,20 @@ package org.insa.graphs.algorithm.shortestpath;
|
||||||
import org.insa.graphs.model.Arc;
|
import org.insa.graphs.model.Arc;
|
||||||
import org.insa.graphs.model.Node;
|
import org.insa.graphs.model.Node;
|
||||||
|
|
||||||
public class Label {
|
public class Label implements Comparable<Label> {
|
||||||
|
|
||||||
private Node sommetCourant;
|
private Node sommetCourant;
|
||||||
|
|
||||||
private Boolean marque;
|
private Boolean marque;
|
||||||
|
|
||||||
private double coutRealise;
|
private float coutRealise;
|
||||||
|
|
||||||
private Arc pere;
|
private Arc pere;
|
||||||
|
|
||||||
public Label(Node sommetCourant) {
|
public Label(Node sommetCourant) {
|
||||||
this.sommetCourant = sommetCourant;
|
this.sommetCourant = sommetCourant;
|
||||||
this.marque = false;
|
this.marque = false;
|
||||||
this.coutRealise = Double.POSITIVE_INFINITY;
|
this.coutRealise = Float.POSITIVE_INFINITY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getSommetCourant() {
|
public Node getSommetCourant() {
|
||||||
|
|
@ -27,16 +27,34 @@ public class Label {
|
||||||
return marque;
|
return marque;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMarque() {
|
||||||
|
this.marque = true;
|
||||||
|
}
|
||||||
|
|
||||||
public Arc getPere() {
|
public Arc getPere() {
|
||||||
return pere;
|
return pere;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPere(Arc pere) {
|
||||||
|
this.pere = pere;
|
||||||
|
}
|
||||||
|
|
||||||
public double getCoutRealise() {
|
public double getCoutRealise() {
|
||||||
return coutRealise;
|
return coutRealise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getCost() {
|
public void setCoutRealise(float nouveauCout) {
|
||||||
|
this.coutRealise = nouveauCout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getCost() {
|
||||||
return coutRealise;
|
return coutRealise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Label o) {
|
||||||
|
return Double.compare(this.coutRealise, o.getCoutRealise()) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue