implementation de isValid() et createShortestPathFromNodes()

This commit is contained in:
Thior Youssouf-Ben-Abdallah 2025-04-13 15:36:54 +02:00
parent c2e656059a
commit 61560998b8

View file

@ -57,7 +57,41 @@ public class Path {
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes) public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException { throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>(); List<Arc> arcs = new ArrayList<Arc>();
// TODO:
if(nodes.size() == 1)
{
return new Path(graph, nodes.get(0));
}
for(int i = 0; i < nodes.size() - 1;i++)
{
Node origin = nodes.get(i);
Node destination = nodes.get(i+1);
List<Arc> successors = origin.getSuccessors();
Arc bestArc = null;
float lengthMin = Float.MAX_VALUE;
for(Arc successor : successors)
{
float legnthSuccessor = successor.getLength();
if(successor.getDestination().equals(destination))
{
if (legnthSuccessor < lengthMin)
{
bestArc = successor;
lengthMin = successor.getLength();
}
}
}
if (bestArc == null)
{
throw new IllegalArgumentException("No arc from " + origin.getId()+ "to " + destination.getId());
}
arcs.add(bestArc);
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -196,7 +230,35 @@ public class Path {
* @deprecated Need to be implemented. * @deprecated Need to be implemented.
*/ */
public boolean isValid() { public boolean isValid() {
// TODO: //it is empty
if(this.isEmpty()) return true;
//it contains a single node (without arcs)
else if((this.getArcs().isEmpty())) return true;
else{
List<Arc> arcs = this.getArcs();
//nombre d arcs dans une variable
int sizeArcs = arcs.size();
if(arcs == null || arcs.size() < 1) return false;
//the first arc has for origin the origin of the path
if(arcs.get(0).getOrigin().equals(origin))
{
/*for two consecutive
* arcs, the destination of the first one is the origin of the second one */
for(int i = 0; i < sizeArcs-1; i++)
{
if(!(arcs.get(i).getDestination().equals(arcs.get(i+1).getOrigin()))) return false;
}
return true;
}
}
return false; return false;
} }
@ -224,13 +286,8 @@ public class Path {
* @deprecated Need to be implemented. * @deprecated Need to be implemented.
*/ */
public double getTravelTime(double speed) { public double getTravelTime(double speed) {
<<<<<<< HEAD
return getLength() * 3600.0 / (speed * 1000.0); return getLength() * 3600.0 / (speed * 1000.0);
=======
// TODO:
return getLength() * 3600.0 / (speed * 1000.0);
>>>>>>> refs/remotes/origin/main
} }
/** /**