ajout: Implémentation de Path#createShortestPathFromNodes(Graph graph, List<Node> nodes)

This commit is contained in:
Brendan Saint Germes 2026-04-10 14:51:21 +02:00
parent b820931b8f
commit 87ae5fc037

View file

@ -63,13 +63,33 @@ public class Path {
* @return A path that goes through the given list of nodes.
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
* consecutive nodes in the list are not connected in the graph.
* @deprecated Need to be implemented.
*/
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
return new Path(graph, arcs);
Path path = null;
if (nodes.size() <= 1) {
path = new Path(graph, nodes.isEmpty() ? null : nodes.get(0));
} else {
List<Arc> arcs = new ArrayList<Arc>();
Node prev = null;
for (Node next : nodes) {
if (prev != null) {
Arc choosen = null;
for (Arc arc : prev.getSuccessors()) {
if (arc.getDestination().equals(next)) {
if (choosen == null || choosen.getLength() > arc.getLength()) {
choosen = arc;
}
}
}
if (choosen == null) throw new IllegalArgumentException("Deux Nodes de la liste n'étaient pas connectés!");
arcs.add(choosen);
}
prev = next;
}
path = new Path(graph, arcs);
}
return path;
}
/**