dijkstra functional

This commit is contained in:
Clement Lacau 2024-04-26 17:31:33 +02:00
parent 73a1e4a058
commit e5be8087f9

View file

@ -95,45 +95,41 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
// We will find the path using the parent nodes, from the destination to the // We will find the path using the parent nodes, from the destination to the
// origin // origin
Node current_node = data.getDestination(); Label current_label = labels.get(data.getDestination().getId());
Label current_label = new Label(current_node); Label parent_label = current_label;
// System.out.println(current_node.getId());
while(current_node!=null&&current_node!=data.getOrigin()) while(current_label != null && current_label.getNode() != data.getOrigin())
{ {
// Find the label matching the parent node // Find the label matching the parent node
for (Label label : labels) { parent_label = labels.get(current_label.getParentNode().getId());
if (label.getNode() == current_node) {
current_label = label;
current_node = current_label.getParentNode();
}
}
// Knowing the parent node, get the arc between the parent and // Knowing the parent node, get the arc between the parent and
// current node and add it to the path // current node and add it to the path
if (current_node != null) { if (parent_label != null) {
for (Arc arc : current_node.getSuccessors()) { for (Arc arc : parent_label.getNode().getSuccessors()) {
if (arc.getDestination().getId() == current_label.getNode().getId()) { if (arc.getDestination().getId() == current_label.getNode().getId()) {
arcs_path.add(arc); arcs_path.add(arc);
break; break;
} }
} }
} }
current_label = parent_label;
} }
notifyDestinationReached(data.getDestination()); notifyDestinationReached(data.getDestination());
// Reverse the path... // Reverse the path...
Collections.reverse(arcs_path); Collections.reverse(arcs_path);
// Create the final solution. // Create the final solution.
solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs_path)); solution = new ShortestPathSolution(data, Status.OPTIMAL, new Path(graph, arcs_path));
/*System.out.println("watch here" + "\n"); /*System.out.println("watch here" + "\n");
for (Arc a : arcs_path){ for (Arc a : arcs_path){
System.out.println(a.getOrigin().getId() + " --- " + a.getDestination().getId() + "\n"); System.out.println(a.getOrigin().getId() + " --- " + a.getDestination().getId() + "\n");
}*/ }*/
return solution; return solution;
} }
} }