Class GraphWalk<V,​E>

  • Type Parameters:
    V - the graph vertex type
    E - 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:

    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:
    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 walk
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • graph

        protected Graph<V,​E> graph
      • vertexList

        protected java.util.List<V> vertexList
      • edgeList

        protected java.util.List<E> edgeList
      • startVertex

        protected V startVertex
      • endVertex

        protected V endVertex
      • weight

        protected double weight
    • 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 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,
                         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 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,
                         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 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 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.
        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 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; 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 java.util.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.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 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