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; } /**