Merge remote-tracking branch

'origin/GP-1888_dragonmacher_PR-4079_HakanIlbas_graph_path_javadoc'
(Closes #4079)
This commit is contained in:
Ryan Kurtz
2022-04-04 12:10:55 -04:00
@@ -19,10 +19,10 @@ import java.util.*;
/**
* Class for storing paths with fast "contains" method.
*
* Note: a path can only contain a vertex once.
*
* @param <V>
* <p>Note: a path can only contain a vertex once.
*
* @param <V> the vertex type.
*/
public class GraphPath<V> {
@@ -30,14 +30,26 @@ public class GraphPath<V> {
private Set<V> pathSet = new HashSet<>();
private List<V> pathList = new ArrayList<>();
/**
* Default constructor.
*/
public GraphPath() {
// default constructor
}
/**
* Constructor with a vertex.
*
* @param v the first vertex of the newly initialized GraphPath object
*/
public GraphPath(V v) {
add(v);
}
/**
* Creates a new GraphPath object by performing a shallow copy on another GraphPath object.
*
* @return the new shallow copy of the original GraphPath object
*/
public GraphPath<V> copy() {
GraphPath<V> newPath = new GraphPath<>();
newPath.pathList.addAll(pathList);
@@ -45,6 +57,12 @@ public class GraphPath<V> {
return newPath;
}
/**
* Check if a GraphPath starts with another GraphPath.
*
* @param otherPath the other GraphPath we are checking
* @return true if the current GraphPath starts with otherPath, false otherwise
*/
public boolean startsWith(GraphPath<V> otherPath) {
if (size() < otherPath.size()) {
return false;
@@ -58,6 +76,14 @@ public class GraphPath<V> {
return true;
}
/**
* Return all vertices that two GraphPaths have in common. For example if you have
* a-b-c-d-e-f and a-b-c-d-k-l-z, the common start path will be a-b-c-d. If there is no common
* start path, an empty GraphPath object is returned.
*
* @param other the other GraphPath to get the common start path of
* @return a new GraphPath object containing the common start path vertices
*/
public GraphPath<V> getCommonStartPath(GraphPath<V> other) {
int n = Math.min(size(), other.size());
for (int i = 0; i < n; i++) {
@@ -68,31 +94,69 @@ public class GraphPath<V> {
return subPath(0, n);
}
/**
* Return the size of the GraphPath.
*
* @return size of the GraphPath
*/
public int size() {
return pathList.size();
}
/**
* Check if vertex v is in the GraphPath.
*
* @param v the vertex
* @return true if vertex v is in this GraphPath
*/
public boolean contains(V v) {
return pathSet.contains(v);
}
/**
* Add a vertex to the GraphPath.
*
* @param v the new vertex
*/
public void add(V v) {
pathSet.add(v);
pathList.add(v);
}
/**
* Get last vertex of GraphPath.
*
* @return last vertex of GraphPath
*/
public V getLast() {
return pathList.get(pathList.size() - 1);
}
/**
* Get the depth of the vertex that is specified by the parameter.
*
* @param v the vertex for which we get the depth
* @return the depth of the vertex
*/
public int depth(V v) {
return pathList.indexOf(v);
}
/**
* Get vertex that is specified by the parameter.
*
* @param depth of the vertex to retrieve
* @return the vertex
*/
public V get(int depth) {
return pathList.get(depth);
}
/**
* Remove the last vertex of the GraphPath.
*
* @return the removed vertex
*/
public V removeLast() {
V v = pathList.remove(pathList.size() - 1);
pathSet.remove(v);
@@ -100,11 +164,10 @@ public class GraphPath<V> {
}
/**
* Returns all entries that are before the given vertex in this path. The results will
* include the vertex.
*
* @param v the vertex
* @return the predecessors
* Return a set with all of the predecessors of the vertex in the GraphPath.
*
* @param v the vertex we want to get the predecessors of
* @return the predecessors of the vertex as a set, return empty set if there are none
*/
public Set<V> getPredecessors(V v) {
Set<V> set = new HashSet<>();
@@ -118,11 +181,10 @@ public class GraphPath<V> {
}
/**
* Returns all entries that are later in this path than the given vertex. The results will
* include the vertex.
*
* @param v the vertex
* @return the successors
* Return a set with all of the successors of the vertex in the GraphPath.
*
* @param v the vertex we want to get the successors of
* @return the successors of the vertex as a set, return empty set if there are none
*/
public Set<V> getSuccessors(V v) {
Set<V> set = new HashSet<>();
@@ -141,6 +203,13 @@ public class GraphPath<V> {
return pathList.toString();
}
/**
* Get a part of the whole GraphPath, similar to substring with strings.
*
* @param start the start of the sub-path of the GraphPath
* @param end the end of the sub-path of the GraphPath
* @return a new GraphPath which is a sub-path of the original GraphPath from start to end
*/
public GraphPath<V> subPath(int start, int end) {
GraphPath<V> subPath = new GraphPath<>();
subPath.pathList = new ArrayList<>(pathList.subList(start, end));