Class GraphWalk<V,E>

java.lang.Object
org.jgrapht.graph.GraphWalk<V,E>
Type Parameters:
V - the graph vertex type
E - the graph edge type
All Implemented Interfaces:
Serializable, GraphPath<V,E>

public class GraphWalk<V,E> extends Object implements GraphPath<V,E>, 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:

  1. 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.
  2. 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:
  • Field Details

    • graph

      protected Graph<V,E> graph
    • vertexList

      protected List<V> vertexList
    • edgeList

      protected List<E> edgeList
    • startVertex

      protected V startVertex
    • endVertex

      protected V endVertex
    • weight

      protected double weight
  • Constructor Details

    • GraphWalk

      public GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, 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 graph
      startVertex - the starting vertex
      endVertex - the last vertex of the path
      edgeList - the list of edges of the path
      weight - the total weight of the path
    • GraphWalk

      public GraphWalk(Graph<V,E> graph, 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 graph
      vertexList - the list of vertices of the path
      weight - the total weight of the path
    • GraphWalk

      public GraphWalk(Graph<V,E> graph, V startVertex, V endVertex, List<V> vertexList, 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 graph
      startVertex - the starting vertex
      endVertex - the last vertex of the path
      vertexList - the list of vertices of the path
      edgeList - the list of edges of the path
      weight - the total weight of the path
  • Method Details

    • 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.
      Specified by:
      getGraph in interface GraphPath<V,E>
      Returns:
      the containing graph
    • getStartVertex

      public V getStartVertex()
      Description copied from interface: GraphPath
      Returns the start vertex in the path.
      Specified by:
      getStartVertex in interface GraphPath<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 interface GraphPath<V,E>
      Returns:
      the end vertex
    • getEdgeList

      public 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; see GraphPath.getVertexList().

      Whether or not the returned edge list is modifiable depends on the path implementation.

      Specified by:
      getEdgeList in interface GraphPath<V,E>
      Returns:
      list of edges traversed by the path
    • getVertexList

      public List<V> getVertexList()
      Description copied from interface: GraphPath
      Returns the path as a sequence of vertices.
      Specified by:
      getVertexList in interface GraphPath<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.
      Specified by:
      getWeight in interface GraphPath<V,E>
      Returns:
      the weight of the path
    • 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.
      Specified by:
      getLength in interface GraphPath<V,E>
      Returns:
      the length of the path, measured in the number of edges
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class 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:
      InvalidGraphWalkException - if the path is invalid
    • reverse

      public GraphWalk<V,E> reverse(Function<GraphWalk<V,E>,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:
      InvalidGraphWalkException - if the path is invalid
    • concat

      public GraphWalk<V,E> concat(GraphWalk<V,E> extension, Function<GraphWalk<V,E>,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:
      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 type
      E - 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 type
      E - edge type
      Parameters:
      graph - input graph
      v - 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 type
      E - edge type
      Parameters:
      graph - input graph
      v - single vertex
      weight - weight of the path
      Returns:
      an empty walk