feat(astar): create labels by algorithm

This commit is contained in:
Paul Alnet 2024-05-03 15:02:21 +02:00
parent 632662ee75
commit f77aea0371
2 changed files with 14 additions and 1 deletions

View file

@ -1,9 +1,18 @@
package org.insa.graphs.algorithm.shortestpath; package org.insa.graphs.algorithm.shortestpath;
import org.insa.graphs.model.Node;
public class AStarAlgorithm extends DijkstraAlgorithm { public class AStarAlgorithm extends DijkstraAlgorithm {
private Node destination;
@Override
protected Label createLabel(Node node) {
return new LabelStar(node, destination);
}
public AStarAlgorithm(ShortestPathData data) { public AStarAlgorithm(ShortestPathData data) {
super(data); super(data);
destination = data.getDestination();
} }
} }

View file

@ -16,6 +16,10 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data); super(data);
} }
protected Label createLabel(Node node) {
return new Label(node);
}
@Override @Override
protected ShortestPathSolution doRun() { protected ShortestPathSolution doRun() {
@ -38,7 +42,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
for (Node node : graph.getNodes()) { for (Node node : graph.getNodes()) {
// Luckily they are ordered by id. // Luckily they are ordered by id.
// ArrayList.set only works if the value is already initialized (because why Java?) // ArrayList.set only works if the value is already initialized (because why Java?)
labels.add(new Label(node)); labels.add(this.createLabel(node));
} }
Label s = labels.get(data.getOrigin().getId()); Label s = labels.get(data.getOrigin().getId());