From 8ca8f98b7e1c20b14fe452ab31d85194b7f135ad Mon Sep 17 00:00:00 2001 From: Raph Date: Wed, 29 Mar 2023 15:56:11 +0200 Subject: [PATCH] shortest path --- .../main/java/org/insa/graphs/model/Path.java | 20 +++++++++++++++---- 1 file changed, 16 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 367e97c..b6e9a89 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 @@ -51,14 +51,23 @@ public class Path { * @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(); - Arc candidat = null; - Node deb = nodes.remove(0); - for(Node fin : nodes){ + List nodes2 = nodes; + Arc candidat = null; //arc candidat à être le plus court entre deux nodes + + if(nodes.size()==0){ + return new Path(graph); + } + else if(nodes.size()==1){ + return new Path(graph,nodes.get(0)); + } + else{ + Node deb = nodes2.get(0); + for(Node fin : nodes2){ + if(!fin.equals(deb)){ float dist = Float.MAX_VALUE; for(Arc a : deb.getSuccessors()){ if(a.getDestination().equals(fin)&&dist>a.getLength()){ @@ -66,12 +75,15 @@ public class Path { candidat=a; } } + if(dist==Float.MAX_VALUE){ throw new IllegalArgumentException("Liste de noeuds non valide"); } arcs.add(candidat); deb = fin; } + } + } return new Path(graph, arcs); }