DebutImplementDijktra

This commit is contained in:
Gasson-Betuing Danyl 2025-05-09 12:19:51 +02:00
parent ae605b126a
commit 40d68f76ad
2 changed files with 36 additions and 15 deletions

View file

@ -6,6 +6,8 @@ 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; import org.insa.graphs.model.Path;
import org.insa.graphs.algorithm.shortestpath.Label;
import org.insa.graphs.algorithm.utils.BinaryHeap;
public class DijkstraAlgorithm extends ShortestPathAlgorithm { public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@ -21,25 +23,40 @@ 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();
// the graph // the graph
Graph graph = data.getGraph(); Graph graph = data.getGraph();
// node number // node number
final int nbNodes = graph.size(); final int nbNodes = graph.size();
// Initialize array of distances. int nodeId = 0;
double[] distances = new double[nbNodes]; // initialize the label list
Arrays.fill(distances, Double.POSITIVE_INFINITY); Label[] labels = new Label[nbNodes];
distances[data.getOrigin().getId()] = 0; for(Node node : graph.getNodes()){
nodeId = node.getId();
labels[nodeId] = new Label(node, false, Double.POSITIVE_INFINITY);
}
labels[data.getOrigin().getId()].computedCost = 0;
// 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;
//binary heap for selected lowest cost
BinaryHeap<Label> minHeap = new BinaryHeap<>();
minHeap.insert(labels[data.getOrigin().getId()]);
// TODO: implement the Dijkstra algorithm // TODO: implement the Dijkstra algorithm
boolean found = false; boolean found = false;
for (int i = 0; !found && i < nbNodes; ++i) { for (int i = 0; !found && i < nbNodes; ++i) {
found = true;
for (Node node : graph.getNodes()) { for (Label lbl : labels) {
for (Arc arc : node.getSuccessors()) {
for (Arc arc : lbl.currentNode.getSuccessors()) {
// Small test to check allowed roads...
if (!data.isAllowed(arc)) {
continue;
}
} }

View file

@ -3,19 +3,18 @@ package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
public class Label{ public class Label implements Comparable<Label>{
private Node currentNode; public Node currentNode;
private boolean mark; public boolean mark;
private double computedCost; public double computedCost;
private Arc father; public Arc father;
public Label(Node currentNode, boolean mark, double computedCost, Arc father){ public Label(Node currentNode, boolean mark, double computedCost){
this.currentNode = currentNode; this.currentNode = currentNode;
this.mark = mark; this.mark = mark;
this.computedCost = computedCost; this.computedCost = computedCost;
this.father = father;
} }
public double getCost(){ public double getCost(){
@ -42,4 +41,9 @@ public class Label{
} }
@Override
public int compareTo(Label other){
return Double.compare(this.getCost(), other.getCost());
}
} }