ADDED Shortestpath Fastestpath

This commit is contained in:
Guillaume Vincent 2020-03-25 18:51:42 +01:00
parent cab5bb631d
commit b4275ae5a6

View file

@ -32,12 +32,41 @@ public class Path {
* *
* @deprecated Need to be implemented. * @deprecated Need to be implemented.
*/ */
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 // TODO:
if (nodes.size() == 1) {
return new Path(graph,nodes.get(0));
}
else if (nodes.size() == 0) {
return new Path(graph);
}
if (nodes.size() > 1) {
Node nodep = null;
for (Node node : nodes) {
double arcMinTime = Double.MAX_VALUE;
Arc arcMin = null;
if (nodep != null) {
for (Arc arc : nodep.getSuccessors()) {
if (arc.getDestination().equals(node)) {
if (arc.getMinimumTravelTime() < arcMinTime) {
arcMin = arc;
arcMinTime = arc.getLength();
}
}
}
if (arcMin == null) {
throw new IllegalArgumentException("No arc between nodes");
}
}
if (arcMin != null) {
arcs.add(arcMin);
}
nodep = node;
}
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
@ -58,10 +87,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: // DONE:
if (nodes.size() == 1) {
return new Path(graph,nodes.get(0));
}
else if (nodes.size() == 0) {
return new Path(graph);
}
if (nodes.size() > 1) {
Node nodep = null;
for (Node node : nodes) {
float arcMinLength = Float.MAX_VALUE;
Arc arcMin = null;
if (nodep != null) {
for (Arc arc : nodep.getSuccessors()) {
if (arc.getDestination().equals(node)) {
if (arc.getLength() < arcMinLength) {
arcMin = arc;
arcMinLength = arc.getLength();
}
}
}
if (arcMin == null) {
throw new IllegalArgumentException("No arc between nodes");
}
}
if (arcMin != null) {
arcs.add(arcMin);
}
nodep = node;
}
}
return new Path(graph, arcs); return new Path(graph, arcs);
} }
/** /**
* Concatenate the given paths. * Concatenate the given paths.
* *