Interface Path<V,E>
-
- Type Parameters:
V
- the vertices typeE
- the edges type
- All Known Subinterfaces:
IPath
public interface Path<V,E>
A path of edges in a graph.A path is a list of edges \(e_1,e_2,\ldots\) where each target vertex of edge \(e_i\) is the source vertex of the next edge \(e_{i+1}\). If the graph is undirected the definition of a 'source' and 'target' are interchangeable, and each pair of consecutive edges simply share an endpoint.
A Path object might be used to represent a cycle as well, if the source and target of the path are the same vertex.
If the underlying graph was modified after the Path object was created, the Path object should not be used.
Graph<String, Integer> g = ...; String sourceVertex = ...; String targetVertex = ...; Path<String, Integer> p = Path.findPath(g, sourceVertex, targetVertex); System.out.println("The path between u and v consist of the following edges:"); for (EdgeIter<String, Integer> it = p.edgeIter(); it.hasNext();) { Integer e = it.next(); String u = it.source(), v = it.target(); System.out.println(" " + e + "(" + u + ", " + v + ")"); }
- Author:
- Barak Ugav
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description EdgeIter<V,E>
edgeIter()
Get anEdgeIter
that iterate over the edges of the path.List<E>
edges()
Get the edges forming this path.static <V,E>
Path<V,E>findPath(Graph<V,E> g, V source, V target)
Find a valid path from \(u\) to \(v\).default boolean
isCycle()
Check whether this path form a cycle.static <V,E>
booleanisPath(Graph<V,E> g, V source, V target, List<E> edges)
Check whether the given edge list is a valid path in the given graph.boolean
isSimple()
Check whether this path is simple.static <V,E>
Path<V,E>newInstance(Graph<V,E> g, V source, V target, List<E> edges)
Create a new path from an edge list, a source and a target vertices.static <V,E>
Set<V>reachableVertices(Graph<V,E> g, Iterator<V> sources)
Find all the vertices reachable from a given set of source vertices.static <V,E>
Set<V>reachableVertices(Graph<V,E> g, V source)
Find all the vertices reachable from a given source vertex.V
source()
Get the source vertex of the path.V
target()
Get the target vertex of the path.List<V>
vertices()
Get the vertices forming this path.
-
-
-
Method Detail
-
source
V source()
Get the source vertex of the path.If the returned vertex is the same as
target()
, the represented path is actually a cycle.- Returns:
- the source vertex of the path.
-
target
V target()
Get the target vertex of the path.If the returned vertex is the same as
source()
, the represented path is actually a cycle.- Returns:
- the target vertex of the path.
-
isCycle
default boolean isCycle()
Check whether this path form a cycle.A cycle is a path which start and ends at the same vertex.
- Returns:
true
if this path form a cycle, elsefalse
-
isSimple
boolean isSimple()
Check whether this path is simple.A path is simple if the path does not visit the same vertex twice. Specifically, a cycle is not simple.
- Returns:
true
if this path is simple, elsefalse
-
edgeIter
EdgeIter<V,E> edgeIter()
Get anEdgeIter
that iterate over the edges of the path.- Returns:
- an
EdgeIter
that iterate over the edges of the path.
-
edges
List<E> edges()
Get the edges forming this path.The path is defined as a list of edges \(e_1,e_2,\ldots\), where each target vertex of an edge \(e_i\) is the source vertex of the next edge \(e_{i+1}\).
- Returns:
- the edges forming this path, by the path order
-
vertices
List<V> vertices()
Get the vertices forming this path.The path is defined as a list of edges \(e_1,e_2,\ldots\), where each target vertex of an edge \(e_i\) is the source vertex of the next edge \(e_{i+1}\). The list of vertices of this path is the vertices visited by this path, ordered by their visit order. If this path form a cycle, the vertices list size is the same as the edge list, otherwise it is greater by one.
- Returns:
- the vertices visited by this path, by the path order
-
newInstance
static <V,E> Path<V,E> newInstance(Graph<V,E> g, V source, V target, List<E> edges)
Create a new path from an edge list, a source and a target vertices.Note that this function does not check whether the given edge list is a valid path in the given graph. To check for validity, use
isPath(Graph, Object, Object, List)
.If an
IntGraph
is passed as argument, the returned path will be anIPath
object.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- the graphsource
- a source vertextarget
- a target vertexedges
- a list of edges that form a path from thesource
to thetarget
vertices in the- Returns:
- a new path
-
isPath
static <V,E> boolean isPath(Graph<V,E> g, V source, V target, List<E> edges)
Check whether the given edge list is a valid path in the given graph.A list of edges is a valid path in the graph if it is a list of edges \(e_1,e_2,\ldots\) where each target vertex of an edge \(e_i\) is the source vertex of the next edge \(e_{i+1}\). If the graph is undirected the definition of a 'source' and 'target' are interchangeable, and each pair of consecutive edges simply share an endpoint. In addition, the edge list must start with the
source
vertex and end with thetarget
vertex.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- a source vertextarget
- a target vertexedges
- a list of edges that form a path from thesource
to thetarget
vertices in the graph.- Returns:
true
if the given edge list is a valid path in the given graph, elsefalse
-
findPath
static <V,E> Path<V,E> findPath(Graph<V,E> g, V source, V target)
Find a valid path from \(u\) to \(v\).This function uses BFS, which will result in the shortest path in the number of edges.
If an
IntGraph
is passed as argument, the returned path will be anIPath
object.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- source vertextarget
- target vertex- Returns:
- a path from \(u\) to \(v\), or
null
if no such path was found
-
reachableVertices
static <V,E> Set<V> reachableVertices(Graph<V,E> g, V source)
Find all the vertices reachable from a given source vertex.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- a source vertex- Returns:
- a set of all the vertices reachable from the given source vertex
-
reachableVertices
static <V,E> Set<V> reachableVertices(Graph<V,E> g, Iterator<V> sources)
Find all the vertices reachable from a given set of source vertices.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsources
- an iterator over a set of source vertices- Returns:
- a set of all the vertices reachable from the given source vertices
-
-