From 87ae5fc037e8b20217ba2ae4634679d99eb3c82e Mon Sep 17 00:00:00 2001 From: Brendan Saint-Germes Date: Fri, 10 Apr 2026 14:51:21 +0200 Subject: [PATCH] =?UTF-8?q?ajout:=20Impl=C3=A9mentation=20de=20Path#create?= =?UTF-8?q?ShortestPathFromNodes(Graph=20graph,=20List=20nodes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/insa/graphs/model/Path.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java index 97036e8..e12cb47 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Path.java @@ -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 nodes) throws IllegalArgumentException { - List arcs = new ArrayList(); - // 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 arcs = new ArrayList(); + 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; } /**