From ba0702c092f30cbe290267fb81cae8a96d659749 Mon Sep 17 00:00:00 2001 From: knzrd Date: Tue, 29 Apr 2025 18:20:32 +0200 Subject: [PATCH] shortest --- .../main/java/org/insa/graphs/model/Path.java | 115 ++++++++++++++++-- 1 file changed, 104 insertions(+), 11 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 cc5a5c9..b48792a 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 @@ -1,5 +1,7 @@ package org.insa.graphs.model; +import java.awt.desktop.PrintFilesEvent; +import java.time.chrono.ThaiBuddhistChronology; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -25,12 +27,13 @@ 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 createFastestPathFromNodes(Graph graph, List nodes) throws IllegalArgumentException { List arcs = new ArrayList(); // TODO: + + return new Path(graph, arcs); } @@ -43,13 +46,77 @@ 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); + + if (nodes.size() == 1) { + Node node = nodes.get(0); + return new Path(graph, node); + + } + + else { + + + List arcs = new ArrayList(); + + // Node leorigin = nodes.get(0); + Node ledest = nodes.get(nodes.size() - 1); + boolean hsein = true; + int i = 0; + + while (hsein && i < nodes.size()) { + float longueurmin = Float.MAX_VALUE; + if (!(nodes.get(i).hasSuccessors()) && nodes.get(i) != ledest) { + hsein = false; + throw new IllegalArgumentException( + "The path is invalid: node " + nodes.get(i) + + " has no successors but is not the destination."); + + } + boolean bonsuccesseur = false; + // parcours de la liste des successeurs verification destinataire + if (nodes.get(i) != ledest) { + for (int k = 0; k < nodes.get(i).getSuccessors().size(); k++) { + if (i < nodes.size()) { + if (nodes.get(i).getSuccessors().get(k) + .getDestination() == nodes.get(i + 1)) { + bonsuccesseur = true; + } + } + + } + + } + + // on continue si on a le bonsuccesseur sinon on a une exception + if (bonsuccesseur) { + Arc arcajouter = null; + for (int j = 0; j < nodes.get(i).getSuccessors().size(); j++) { + if (nodes.get(i).getSuccessors().get(j) + .getDestination() == nodes.get(i + 1)) { + if ((float) nodes.get(i).getSuccessors().get(j) + .getLength() < longueurmin) { + longueurmin = + nodes.get(i).getSuccessors().get(j).getLength(); + arcajouter = nodes.get(i).getSuccessors().get(j); + // arcs.add(arcajouter); + // System.out.println(arcs); + } + } + + } + arcs.add(arcajouter); + } + + i++; + + } + + return new Path(graph, arcs); + } + } /** @@ -150,7 +217,8 @@ public class Path { } /** - * @return List of arcs in the path. + * @return List of arcs in the// System.out.println("aaaaaaaaaaaaa"); // + * System.out.println(arcs); path. */ public List getArcs() { return Collections.unmodifiableList(arcs); @@ -184,13 +252,38 @@ public class Path { * * * @return true if the path is valid, false otherwise. - * @deprecated Need to be implemented. */ - public boolean isValid() { - if (this.isEmpty() || !(this.origin.hasSuccessors()) || (this.origin()==this.arcs[0].getOrigin() && (for(i=0;i