added shortest and fastest paths
This commit is contained in:
parent
1f6999aefc
commit
9b40dc4c4e
1 changed files with 56 additions and 4 deletions
|
@ -30,12 +30,38 @@ public class Path {
|
|||
* @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 createFastestPathFromNodes(Graph graph, List<Node> nodes)
|
||||
throws IllegalArgumentException {
|
||||
if (nodes.size() == 1) {
|
||||
return new Path(graph, nodes.get(0));
|
||||
}
|
||||
if (nodes.size() == 1) {
|
||||
return new Path(graph);
|
||||
}
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
// TODO:
|
||||
for (int i = 0 ; i < nodes.size(); i++) {
|
||||
Node currNode = nodes.get(i);
|
||||
if (i < nodes.size() -1) {
|
||||
Node nextNode = nodes.get(i+1);
|
||||
if (currNode.hasSuccessors()) {
|
||||
Arc better = null;
|
||||
for (Arc j : currNode.getSuccessors()) {
|
||||
if (j.getDestination() == nextNode) {
|
||||
if (better == null || better.getMinimumTravelTime() > j.getMinimumTravelTime()) {
|
||||
better = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (better == null) {
|
||||
throw (new IllegalArgumentException());
|
||||
} else {
|
||||
arcs.add(better);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
|
||||
|
@ -51,12 +77,38 @@ public class Path {
|
|||
* @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() == 1) {
|
||||
return new Path(graph, nodes.get(0));
|
||||
}
|
||||
if (nodes.size() == 1) {
|
||||
return new Path(graph);
|
||||
}
|
||||
List<Arc> arcs = new ArrayList<Arc>();
|
||||
// TODO:
|
||||
for (int i = 0 ; i < nodes.size(); i++) {
|
||||
Node currNode = nodes.get(i);
|
||||
if (i < nodes.size() -1) {
|
||||
Node nextNode = nodes.get(i+1);
|
||||
if (currNode.hasSuccessors()) {
|
||||
Arc better = null;
|
||||
for (Arc j : currNode.getSuccessors()) {
|
||||
if (j.getDestination() == nextNode) {
|
||||
if (better == null || better.getLength() > j.getLength()) {
|
||||
better = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (better == null) {
|
||||
throw (new IllegalArgumentException());
|
||||
} else {
|
||||
arcs.add(better);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Path(graph, arcs);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue