這個提交存在於:
Lea Norgeux 2023-03-22 10:39:32 +01:00
當前提交 75097d40da
共有 2 個檔案被更改,包括 74 行新增5 行删除

查看文件

@ -20,8 +20,8 @@ public class Path {
/** /**
* Create a new path that goes through the given list of nodes (in order), * 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 graph Graph containing the nodes in the list.
* @param nodes List of nodes to build the path. * @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) public static Path createFastestPathFromNodes(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)
{
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); 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) 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)
{
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); return new Path(graph, arcs);
}
else if(nodes.size()==1)
{
return new Path(graph, nodes.get(0));
}
else
{
return new Path(graph);
}
} }
/** /**

查看文件

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