DebutImplementDijktra
This commit is contained in:
parent
ae605b126a
commit
40d68f76ad
2 changed files with 36 additions and 15 deletions
|
@ -6,6 +6,8 @@ import org.insa.graphs.model.Arc;
|
|||
import org.insa.graphs.model.Graph;
|
||||
import org.insa.graphs.model.Node;
|
||||
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 {
|
||||
|
||||
|
@ -21,25 +23,40 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
|||
// retrieve data from the input problem (getInputData() is inherited from the
|
||||
// parent class ShortestPathAlgorithm)
|
||||
final ShortestPathData data = getInputData();
|
||||
|
||||
// the graph
|
||||
Graph graph = data.getGraph();
|
||||
// node number
|
||||
final int nbNodes = graph.size();
|
||||
// Initialize array of distances.
|
||||
double[] distances = new double[nbNodes];
|
||||
Arrays.fill(distances, Double.POSITIVE_INFINITY);
|
||||
distances[data.getOrigin().getId()] = 0;
|
||||
int nodeId = 0;
|
||||
// initialize the label list
|
||||
Label[] labels = new Label[nbNodes];
|
||||
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
|
||||
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
|
||||
|
||||
boolean found = false;
|
||||
for (int i = 0; !found && i < nbNodes; ++i) {
|
||||
found = true;
|
||||
for (Node node : graph.getNodes()) {
|
||||
for (Arc arc : node.getSuccessors()) {
|
||||
|
||||
for (Label lbl : labels) {
|
||||
|
||||
for (Arc arc : lbl.currentNode.getSuccessors()) {
|
||||
|
||||
// Small test to check allowed roads...
|
||||
if (!data.isAllowed(arc)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -3,19 +3,18 @@ package org.insa.graphs.algorithm.shortestpath;
|
|||
import org.insa.graphs.model.Node;
|
||||
import org.insa.graphs.model.Arc;
|
||||
|
||||
public class Label{
|
||||
public class Label implements Comparable<Label>{
|
||||
|
||||
private Node currentNode;
|
||||
private boolean mark;
|
||||
private double computedCost;
|
||||
private Arc father;
|
||||
public Node currentNode;
|
||||
public boolean mark;
|
||||
public double computedCost;
|
||||
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.mark = mark;
|
||||
this.computedCost = computedCost;
|
||||
this.father = father;
|
||||
}
|
||||
|
||||
public double getCost(){
|
||||
|
@ -42,4 +41,9 @@ public class Label{
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Label other){
|
||||
return Double.compare(this.getCost(), other.getCost());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue