1
0
Fork 0

NOT FINISHED : Dijkstra, changed Label

This commit is contained in:
Yanis Mahé 2026-04-16 10:44:48 +02:00
parent 52a8945a91
commit f42e7edf5f
2 changed files with 82 additions and 8 deletions

View file

@ -1,5 +1,14 @@
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 DijkstraAlgorithm(ShortestPathData data) {
@ -12,12 +21,59 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.size();
// variable that will contain the solution of the shortest path problem
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
return solution;
}

View file

@ -3,20 +3,20 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Node;
public class Label {
public class Label implements Comparable<Label> {
private Node sommetCourant;
private Boolean marque;
private double coutRealise;
private float coutRealise;
private Arc pere;
public Label(Node sommetCourant) {
this.sommetCourant = sommetCourant;
this.marque = false;
this.coutRealise = Double.POSITIVE_INFINITY;
this.coutRealise = Float.POSITIVE_INFINITY;
}
public Node getSommetCourant() {
@ -26,17 +26,35 @@ public class Label {
public Boolean getMarque() {
return marque;
}
public void setMarque() {
this.marque = true;
}
public Arc getPere() {
return pere;
}
public void setPere(Arc pere) {
this.pere = pere;
}
public double getCoutRealise() {
return coutRealise;
}
public double getCost() {
return coutRealise;
public void setCoutRealise(float nouveauCout) {
this.coutRealise = nouveauCout;
}
public float getCost() {
return coutRealise;
}
@Override
public int compareTo(Label o) {
return Double.compare(this.coutRealise, o.getCoutRealise()) ;
}
}