Path.createShortestPathFromNodes
This commit is contained in:
parent
472b578692
commit
ae6bf5d339
1 changed files with 11 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ package org.insa.graphs.model;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -43,12 +44,20 @@ 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<Node> nodes)
|
||||
throws IllegalArgumentException {
|
||||
if (nodes.size() == 0) {
|
||||
return new Path(graph);
|
||||
}
|
||||
if (nodes.size() == 1) {
|
||||
return new Path(graph, nodes.getFirst());
|
||||
}
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
// TODO:
|
||||
for (int i=0; i<nodes.size()-1; i++) {
|
||||
int j = i;
|
||||
arcs.add(nodes.get(i).getSuccessors().stream().filter(a -> a.getDestination().equals(nodes.get(j+1))).min(Comparator.comparing(Arc::getLength)).orElseThrow(() -> new IllegalArgumentException("Invalid list of nodes")));
|
||||
}
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue