This commit is contained in:
Lea Norgeux 2023-03-22 10:39:32 +01:00
commit 75097d40da
2 changed files with 74 additions and 5 deletions

View file

@ -20,8 +20,8 @@ public class Path {
/**
* Create a new path that goes through the given list of nodes (in order),
* choosing the fastest route if multiple are available.
*
* choosing the fastest route
return new Path(graph, arcs);
* @param graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path.
*
@ -35,8 +35,43 @@ public class Path {
public static Path createFastestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
if(nodes.size()>1)
{
for(int i = 0; i< nodes.size()-1; i++)
{
double fastestPath = -1;
for(Arc arc : nodes.get(i).getSuccessors())
{
if(arc.getDestination().equals(nodes.get(i+1)))
{
if(fastestPath == -1)
{
fastestPath = arc.getMinimumTravelTime();
arcs.add(arc);
}
else if (fastestPath > arc.getMinimumTravelTime())
{
fastestPath = arc.getMinimumTravelTime();
arcs.set(arcs.size()-1, arc);
}
}
}
if(fastestPath == -1)
{
throw(new IllegalArgumentException());
}
}
return new Path(graph, arcs);
}
else if(nodes.size()==1)
{
return new Path(graph, nodes.get(0));
}
else
{
return new Path(graph);
}
}
/**
@ -56,8 +91,42 @@ public class Path {
public static Path createShortestPathFromNodes(Graph graph, List<Node> nodes)
throws IllegalArgumentException {
List<Arc> arcs = new ArrayList<Arc>();
// TODO:
if(nodes.size()>1)
{
for(int i = 0; i< nodes.size()-1; i++)
{
double shortestPath = -1;
for(Arc arc : nodes.get(i).getSuccessors())
{
if(arc.getDestination().equals(nodes.get(i+1)))
{
if(shortestPath == -1)
{
shortestPath = arc.getLength();
arcs.add(arc);
}
else if (shortestPath > arc.getLength())
{
shortestPath = arc.getLength();
arcs.set(arcs.size()-1, arc);
}
}
}
if(shortestPath == -1)
{
throw(new IllegalArgumentException());
}
}
return new Path(graph, arcs);
}
else if(nodes.size()==1)
{
return new Path(graph, nodes.get(0));
}
else
{
return new Path(graph);
}
}
/**

View file

@ -18,7 +18,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
public class PathTest {
//test
// Small graph use for tests
private static Graph graph;