feat(path): implement getShortestPathFromNodes
This commit is contained in:
parent
f1398ed125
commit
89fe1293d3
1 changed files with 26 additions and 4 deletions
|
@ -72,14 +72,36 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
* @throws IllegalArgumentException If the list of nodes is not valid, i.e. two
|
||||||
* consecutive nodes in the list are not connected in the graph.
|
* consecutive nodes in the list are not connected in the graph.
|
||||||
*
|
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
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:
|
|
||||||
return new Path(graph, arcs);
|
if (nodes.size() == 0)
|
||||||
|
return new Path(graph);
|
||||||
|
else if (nodes.size() == 1)
|
||||||
|
return new Path(graph, nodes.get(0)); //test
|
||||||
|
else {
|
||||||
|
// skip the last node because it is the destination
|
||||||
|
for (int i = 0; i < nodes.size() - 1; i++) {
|
||||||
|
Node current = nodes.get(i);
|
||||||
|
Node next = nodes.get(i + 1);
|
||||||
|
Arc fastest = null;
|
||||||
|
|
||||||
|
for (Arc arc : current.getSuccessors()) {
|
||||||
|
if (arc.getDestination() == next
|
||||||
|
&& (fastest == null
|
||||||
|
|| arc.getLength() < fastest.getLength()))
|
||||||
|
fastest = arc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fastest == null)
|
||||||
|
throw new IllegalArgumentException("Requested nodes are not adjacent");
|
||||||
|
else
|
||||||
|
arcs.add(fastest);
|
||||||
|
}
|
||||||
|
return new Path(graph, arcs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue