added shortest and fastest paths

This commit is contained in:
Lacroix Raphael 2022-03-25 18:18:01 +01:00
parent 1f6999aefc
commit 9b40dc4c4e

View file

@ -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);
}