Package org.jgrapht.graph
Class GraphWalk<V,E>
- java.lang.Object
-
- org.jgrapht.graph.GraphWalk<V,E>
-
- Type Parameters:
V
- the graph vertex typeE
- the graph edge type
- All Implemented Interfaces:
java.io.Serializable
,GraphPath<V,E>
public class GraphWalk<V,E> extends java.lang.Object implements GraphPath<V,E>, java.io.Serializable
A walk in a graph is an alternating sequence of vertices and edges, starting and ending at a vertex, in which each edge is adjacent in the sequence to its two endpoints. More precisely, a walk is a connected sequence of vertices and edges in a graph $v_0, e_0, v_1, e_1, v_2, \dotso, v_{k-1}, e_{k-1}, v_{k}$, such that for $1 \leq i \leq k$, the edge $e_i$ has endpoints $v_{i-1}$ and $v_i$. The class makes no assumptions with respect to the shape of the walk: edges may be repeated, and the start and end point of the walk may be different.See http://mathworld.wolfram.com/Walk.html
GraphWalk is the default implementation of
GraphPath
.Two special cases exist:
- A singleton GraphWalk has an empty edge list (the length of the path equals 0), the vertex list contains a single vertex v, and the start and end vertex equal v.
- An empty Graphwalk has empty edge and vertex lists, and the start and end vertex are both null.
This class is implemented as a light-weight data structure; this class does not verify whether the sequence of edges or the sequence of vertices provided during construction forms an actual walk. It is the responsibility of the invoking class to provide correct input data.
Note: Serialization of a GraphWalk implies the serialization of the entire underlying graph.
- Author:
- Joris Kinable
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GraphWalk(Graph<V,E> graph, java.util.List<V> vertexList, double weight)
Creates a walk defined by a sequence of vertices.GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, java.util.List<E> edgeList, double weight)
Creates a walk defined by a sequence of edges.GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, java.util.List<V> vertexList, java.util.List<E> edgeList, double weight)
Creates a walk defined by both a sequence of edges and a sequence of vertices.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description GraphWalk<V,E>
concat(GraphWalk<V,E> extension, java.util.function.Function<GraphWalk<V,E>,java.lang.Double> walkWeightCalculator)
Concatenates the specified GraphWalk to the end of this GraphWalk.static <V,E>
GraphWalk<V,E>emptyWalk(Graph<V,E> graph)
Convenience method which creates an empty walk.boolean
equals(java.lang.Object o)
java.util.List<E>
getEdgeList()
Returns the edges making up the path.V
getEndVertex()
Returns the end vertex in the path.Graph<V,E>
getGraph()
Returns the graph over which this path is defined.int
getLength()
Returns the length of the path, measured in the number of edges.V
getStartVertex()
Returns the start vertex in the path.java.util.List<V>
getVertexList()
Returns the path as a sequence of vertices.double
getWeight()
Returns the weight assigned to the path.int
hashCode()
boolean
isEmpty()
Returns true if the path is an empty path, that is, a path with startVertex=endVertex=null and with an empty vertex and edge list.GraphWalk<V,E>
reverse()
Reverses the direction of the walk.GraphWalk<V,E>
reverse(java.util.function.Function<GraphWalk<V,E>,java.lang.Double> walkWeightCalculator)
Reverses the direction of the walk.void
setWeight(double weight)
Updates the weight of this walkstatic <V,E>
GraphWalk<V,E>singletonWalk(Graph<V,E> graph, V v)
Convenience method which creates a walk consisting of a single vertex with weight 0.0.static <V,E>
GraphWalk<V,E>singletonWalk(Graph<V,E> graph, V v, double weight)
Convenience method which creates a walk consisting of a single vertex.java.lang.String
toString()
void
verify()
Convenience method which verifies whether the given path is feasible wrt the input graph and forms an actual path.
-
-
-
Constructor Detail
-
GraphWalk
public GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, java.util.List<E> edgeList, double weight)
Creates a walk defined by a sequence of edges. A walk defined by its edges can be specified for non-simple graphs. Edge repetition is permitted, the start and end point points ($v_0$ and $v_k$) can be different.- Parameters:
graph
- the graphstartVertex
- the starting vertexendVertex
- the last vertex of the pathedgeList
- the list of edges of the pathweight
- the total weight of the path
-
GraphWalk
public GraphWalk(Graph<V,E> graph, java.util.List<V> vertexList, double weight)
Creates a walk defined by a sequence of vertices. Note that the input graph must be simple, otherwise the vertex sequence does not necessarily define a unique path. Furthermore, all vertices must be pairwise adjacent.- Parameters:
graph
- the graphvertexList
- the list of vertices of the pathweight
- the total weight of the path
-
GraphWalk
public GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, java.util.List<V> vertexList, java.util.List<E> edgeList, double weight)
Creates a walk defined by both a sequence of edges and a sequence of vertices. Note that both the sequence of edges and the sequence of vertices must describe the same path! This is not verified during the construction of the walk. This constructor makes it possible to store both a vertex and an edge view of the same walk, thereby saving computational overhead when switching from one to the other.- Parameters:
graph
- the graphstartVertex
- the starting vertexendVertex
- the last vertex of the pathvertexList
- the list of vertices of the pathedgeList
- the list of edges of the pathweight
- the total weight of the path
-
-
Method Detail
-
getGraph
public Graph<V,E> getGraph()
Description copied from interface:GraphPath
Returns the graph over which this path is defined. The path may also be valid with respect to other graphs.
-
getStartVertex
public V getStartVertex()
Description copied from interface:GraphPath
Returns the start vertex in the path.- Specified by:
getStartVertex
in interfaceGraphPath<V,E>
- Returns:
- the start vertex
-
getEndVertex
public V getEndVertex()
Description copied from interface:GraphPath
Returns the end vertex in the path.- Specified by:
getEndVertex
in interfaceGraphPath<V,E>
- Returns:
- the end vertex
-
getEdgeList
public java.util.List<E> getEdgeList()
Description copied from interface:GraphPath
Returns the edges making up the path. The first edge in this path is incident to the start vertex. The last edge is incident to the end vertex. The vertices along the path can be obtained by traversing from the start vertex, finding its opposite across the first edge, and then doing the same successively across subsequent edges; seeGraphPath.getVertexList()
.Whether or not the returned edge list is modifiable depends on the path implementation.
- Specified by:
getEdgeList
in interfaceGraphPath<V,E>
- Returns:
- list of edges traversed by the path
-
getVertexList
public java.util.List<V> getVertexList()
Description copied from interface:GraphPath
Returns the path as a sequence of vertices.- Specified by:
getVertexList
in interfaceGraphPath<V,E>
- Returns:
- path, denoted by a list of vertices
-
getWeight
public double getWeight()
Description copied from interface:GraphPath
Returns the weight assigned to the path. Typically, this will be the sum of the weights of the edge list entries (as defined by the containing graph), but some path implementations may use other definitions.
-
setWeight
public void setWeight(double weight)
Updates the weight of this walk- Parameters:
weight
- weight of the walk
-
getLength
public int getLength()
Description copied from interface:GraphPath
Returns the length of the path, measured in the number of edges.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
reverse
public GraphWalk<V,E> reverse()
Reverses the direction of the walk. In case of directed/mixed graphs, the arc directions will be reversed. An exception is thrown if reversing an arc $(u,v)$ is impossible because arc $(v,u)$ is not present in the graph. The weight of the resulting walk equals the sum of edge weights in the walk.- Returns:
- a reversed GraphWalk
- Throws:
org.jgrapht.graph.InvalidGraphWalkException
- if the path is invalid
-
reverse
public GraphWalk<V,E> reverse(java.util.function.Function<GraphWalk<V,E>,java.lang.Double> walkWeightCalculator)
Reverses the direction of the walk. In case of directed/mixed graphs, the arc directions will be reversed. An exception is thrown if reversing an arc $(u,v)$ is impossible because arc $(v,u)$ is not present in the graph.- Parameters:
walkWeightCalculator
- Function used to calculate the weight of the reversed GraphWalk- Returns:
- a reversed GraphWalk
- Throws:
org.jgrapht.graph.InvalidGraphWalkException
- if the path is invalid
-
concat
public GraphWalk<V,E> concat(GraphWalk<V,E> extension, java.util.function.Function<GraphWalk<V,E>,java.lang.Double> walkWeightCalculator)
Concatenates the specified GraphWalk to the end of this GraphWalk. This action can only be performed if the end vertex of this GraphWalk is the same as the start vertex of the extending GraphWalk- Parameters:
extension
- GraphPath used for the concatenation.walkWeightCalculator
- Function used to calculate the weight of the GraphWalk obtained after the concatenation.- Returns:
- a GraphWalk that represents the concatenation of this object's walk followed by the walk specified in the extension argument.
-
isEmpty
public boolean isEmpty()
Returns true if the path is an empty path, that is, a path with startVertex=endVertex=null and with an empty vertex and edge list.- Returns:
- Returns true if the path is an empty path.
-
verify
public void verify()
Convenience method which verifies whether the given path is feasible wrt the input graph and forms an actual path.- Throws:
org.jgrapht.graph.InvalidGraphWalkException
- if the path is invalid
-
emptyWalk
public static <V,E> GraphWalk<V,E> emptyWalk(Graph<V,E> graph)
Convenience method which creates an empty walk.- Type Parameters:
V
- vertex typeE
- edge type- Parameters:
graph
- input graph- Returns:
- an empty walk
-
singletonWalk
public static <V,E> GraphWalk<V,E> singletonWalk(Graph<V,E> graph, V v)
Convenience method which creates a walk consisting of a single vertex with weight 0.0.- Type Parameters:
V
- vertex typeE
- edge type- Parameters:
graph
- input graphv
- single vertex- Returns:
- an empty walk
-
singletonWalk
public static <V,E> GraphWalk<V,E> singletonWalk(Graph<V,E> graph, V v, double weight)
Convenience method which creates a walk consisting of a single vertex.- Type Parameters:
V
- vertex typeE
- edge type- Parameters:
graph
- input graphv
- single vertexweight
- weight of the path- Returns:
- an empty walk
-
-