Implemented all functions to pass all tests
This commit is contained in:
parent
52df139bd8
commit
fa236a612f
1 changed files with 77 additions and 46 deletions
|
@ -14,7 +14,6 @@ import java.util.List;
|
||||||
* of {@link Node} due to the multi-graph nature (multiple arcs between two
|
* of {@link Node} due to the multi-graph nature (multiple arcs between two
|
||||||
* nodes) of the considered graphs.
|
* nodes) of the considered graphs.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Path {
|
public class Path {
|
||||||
|
|
||||||
|
@ -24,19 +23,13 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @param graph Graph containing the nodes in the list.
|
* @param graph Graph containing the nodes in the list.
|
||||||
* @param nodes List of nodes to build the path.
|
* @param nodes List of nodes to build the path.
|
||||||
*
|
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
*
|
|
||||||
* @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 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>();
|
return Path.createSmallestPath(graph, nodes, false);
|
||||||
// TODO:
|
|
||||||
return new Path(graph, arcs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,28 +38,72 @@ public class Path {
|
||||||
*
|
*
|
||||||
* @param graph Graph containing the nodes in the list.
|
* @param graph Graph containing the nodes in the list.
|
||||||
* @param nodes List of nodes to build the path.
|
* @param nodes List of nodes to build the path.
|
||||||
*
|
|
||||||
* @return A path that goes through the given list of nodes.
|
* @return A path that goes through the given list of nodes.
|
||||||
*
|
|
||||||
* @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>();
|
return Path.createSmallestPath(graph, nodes, true);
|
||||||
// TODO:
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new path that goes through the given list of nodes (in order),
|
||||||
|
* choosing the shortest or fastest route if multiple are available.
|
||||||
|
*
|
||||||
|
* @param graph Graph containing the nodes in the list.
|
||||||
|
* @param nodes List of nodes to build the path.
|
||||||
|
* @param isLength Should we search the shortest path (true) or the fastest path (false)
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
|
private static Path createSmallestPath(Graph graph, List<Node> nodes, boolean isLength) {
|
||||||
|
List<Arc> arcs = new ArrayList<>();
|
||||||
|
if (nodes.size() != 0 && nodes.size() != 1) {
|
||||||
|
for (int i = 0; i < (nodes.size() - 1); i++) {
|
||||||
|
Arc shortest = Path.getSmallestArc(
|
||||||
|
nodes.get(i).getSuccessors(), nodes.get(i + 1), isLength
|
||||||
|
);
|
||||||
|
arcs.add(shortest);
|
||||||
|
}
|
||||||
|
} else if (nodes.size() == 1)
|
||||||
|
return new Path(graph, nodes.get(0));
|
||||||
return new Path(graph, arcs);
|
return new Path(graph, arcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the smallest arc in the given list.
|
||||||
|
* If an arc destination does not match the given node, it is ignored.
|
||||||
|
*
|
||||||
|
* @param arcs The arcs list to search in
|
||||||
|
* @param nextNode The next node to match the arc destination
|
||||||
|
* @param isLength Should we search the shortest arc (true) or the fastest arc (false)
|
||||||
|
* @return The smallest arc to nextNode
|
||||||
|
* @throws IllegalArgumentException If no node is found
|
||||||
|
*/
|
||||||
|
private static Arc getSmallestArc(List<Arc> arcs, Node nextNode, boolean isLength) {
|
||||||
|
Arc shortestArc = null;
|
||||||
|
double smallestComparator = -1;
|
||||||
|
for (Arc arc : arcs) {
|
||||||
|
if (nextNode.equals(arc.getDestination())) {
|
||||||
|
double comparator = isLength ? arc.getLength() : arc.getMinimumTravelTime();
|
||||||
|
if (comparator < smallestComparator || smallestComparator < 0) {
|
||||||
|
smallestComparator = comparator;
|
||||||
|
shortestArc = arc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (shortestArc == null)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
return shortestArc;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concatenate the given paths.
|
* Concatenate the given paths.
|
||||||
*
|
*
|
||||||
* @param paths Array of paths to concatenate.
|
* @param paths Array of paths to concatenate.
|
||||||
*
|
|
||||||
* @return Concatenated path.
|
* @return Concatenated path.
|
||||||
*
|
|
||||||
* @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
|
* @throws IllegalArgumentException if the paths cannot be concatenated (IDs of
|
||||||
* map do not match, or the end of a path is not the beginning of the
|
* map do not match, or the end of a path is not the beginning of the
|
||||||
* next).
|
* next).
|
||||||
|
@ -83,7 +120,7 @@ public class Path {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ArrayList<Arc> arcs = new ArrayList<>();
|
ArrayList<Arc> arcs = new ArrayList<>();
|
||||||
for (Path path: paths) {
|
for (Path path : paths) {
|
||||||
arcs.addAll(path.getArcs());
|
arcs.addAll(path.getArcs());
|
||||||
}
|
}
|
||||||
Path path = new Path(paths[0].getGraph(), arcs);
|
Path path = new Path(paths[0].getGraph(), arcs);
|
||||||
|
@ -186,7 +223,7 @@ public class Path {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if this path is valid.
|
* Check if this path is valid.
|
||||||
*
|
* <p>
|
||||||
* A path is valid if any of the following is true:
|
* A path is valid if any of the following is true:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>it is empty;</li>
|
* <li>it is empty;</li>
|
||||||
|
@ -197,8 +234,6 @@ public class Path {
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @return true if the path is valid, false otherwise.
|
* @return true if the path is valid, false otherwise.
|
||||||
*
|
|
||||||
* @deprecated Need to be implemented.
|
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
boolean valid = isEmpty() || (this.arcs.size() == 0 && this.origin != null);
|
boolean valid = isEmpty() || (this.arcs.size() == 0 && this.origin != null);
|
||||||
|
@ -206,7 +241,7 @@ public class Path {
|
||||||
valid = this.arcs.get(0).getOrigin().equals(this.origin);
|
valid = this.arcs.get(0).getOrigin().equals(this.origin);
|
||||||
for (int i = 1; i < 3; i++) {
|
for (int i = 1; i < 3; i++) {
|
||||||
if (this.arcs.size() > i)
|
if (this.arcs.size() > i)
|
||||||
valid = valid && (this.arcs.get(i).getOrigin().equals(this.arcs.get(i-1).getDestination()));
|
valid = valid && (this.arcs.get(i).getOrigin().equals(this.arcs.get(i - 1).getDestination()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return valid;
|
return valid;
|
||||||
|
@ -216,7 +251,6 @@ public class Path {
|
||||||
* Compute the length of this path (in meters).
|
* Compute the length of this path (in meters).
|
||||||
*
|
*
|
||||||
* @return Total length of the path (in meters).
|
* @return Total length of the path (in meters).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public float getLength() {
|
public float getLength() {
|
||||||
float length = 0;
|
float length = 0;
|
||||||
|
@ -230,10 +264,8 @@ public class Path {
|
||||||
* Compute the time required to travel this path if moving at the given speed.
|
* Compute the time required to travel this path if moving at the given speed.
|
||||||
*
|
*
|
||||||
* @param speed Speed to compute the travel time.
|
* @param speed Speed to compute the travel time.
|
||||||
*
|
|
||||||
* @return Time (in seconds) required to travel this path at the given speed (in
|
* @return Time (in seconds) required to travel this path at the given speed (in
|
||||||
* kilometers-per-hour).
|
* kilometers-per-hour).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public double getTravelTime(double speed) {
|
public double getTravelTime(double speed) {
|
||||||
float time = 0;
|
float time = 0;
|
||||||
|
@ -248,7 +280,6 @@ public class Path {
|
||||||
* on every arc.
|
* on every arc.
|
||||||
*
|
*
|
||||||
* @return Minimum travel time to travel this path (in seconds).
|
* @return Minimum travel time to travel this path (in seconds).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public double getMinimumTravelTime() {
|
public double getMinimumTravelTime() {
|
||||||
float time = 0;
|
float time = 0;
|
||||||
|
|
Loading…
Reference in a new issue