Package org.jgrapht

Class Graphs

  • Direct Known Subclasses:
    GraphHelper

    public abstract class Graphs
    extends java.lang.Object
    A collection of utilities to assist with graph manipulation.
    Since:
    Jul 31, 2003
    Author:
    Barak Naveh
    • Constructor Summary

      Constructors 
      Constructor Description
      Graphs()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <V,​E>
      boolean
      addAllEdges​(Graph<? super V,​? super E> destination, Graph<V,​E> source, java.util.Collection<? extends E> edges)
      Adds a subset of the edges of the specified source graph to the specified destination graph.
      static <V,​E>
      boolean
      addAllVertices​(Graph<? super V,​? super E> destination, java.util.Collection<? extends V> vertices)
      Adds all of the specified vertices to the destination graph.
      static <V,​E>
      E
      addEdge​(Graph<V,​E> g, V sourceVertex, V targetVertex, double weight)
      Creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
      static <V,​E>
      boolean
      addEdgeWithVertices​(Graph<V,​E> targetGraph, Graph<V,​E> sourceGraph, E edge)
      Adds the specified edge to the graph, including its vertices if not already included.
      static <V,​E>
      E
      addEdgeWithVertices​(Graph<V,​E> g, V sourceVertex, V targetVertex)
      Adds the specified source and target vertices to the graph, if not already included, and creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
      static <V,​E>
      E
      addEdgeWithVertices​(Graph<V,​E> g, V sourceVertex, V targetVertex, double weight)
      Adds the specified source and target vertices to the graph, if not already included, and creates a new weighted edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
      static <V,​E>
      boolean
      addGraph​(Graph<? super V,​? super E> destination, Graph<V,​E> source)
      Adds all the vertices and all the edges of the specified source graph to the specified destination graph.
      static <V,​E>
      void
      addGraphReversed​(DirectedGraph<? super V,​? super E> destination, DirectedGraph<V,​E> source)
      Adds all the vertices and all the edges of the specified source digraph to the specified destination digraph, reversing all of the edges.
      static <V,​E>
      V
      getOppositeVertex​(Graph<V,​E> g, E e, V v)
      Gets the vertex opposite another vertex across an edge.
      static <V,​E>
      java.util.List<V>
      getPathVertexList​(GraphPath<V,​E> path)
      Gets the list of vertices visited by a path.
      static <V,​E>
      java.util.List<V>
      neighborListOf​(Graph<V,​E> g, V vertex)
      Returns a list of vertices that are the neighbors of a specified vertex.
      static <V,​E>
      java.util.List<V>
      predecessorListOf​(DirectedGraph<V,​E> g, V vertex)
      Returns a list of vertices that are the direct predecessors of a specified vertex.
      static <V,​E>
      java.util.List<V>
      successorListOf​(DirectedGraph<V,​E> g, V vertex)
      Returns a list of vertices that are the direct successors of a specified vertex.
      static <V,​E>
      boolean
      testIncidence​(Graph<V,​E> g, E e, V v)
      Tests whether an edge is incident to a vertex.
      static <V,​E>
      UndirectedGraph<V,​E>
      undirectedGraph​(Graph<V,​E> g)
      Returns an undirected view of the specified graph.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Graphs

        public Graphs()
    • Method Detail

      • addEdge

        public static <V,​E> E addEdge​(Graph<V,​E> g,
                                            V sourceVertex,
                                            V targetVertex,
                                            double weight)
        Creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
        Parameters:
        g - the graph for which the edge to be added.
        sourceVertex - source vertex of the edge.
        targetVertex - target vertex of the edge.
        weight - weight of the edge.
        Returns:
        The newly created edge if added to the graph, otherwise null.
        See Also:
        Graph.addEdge(Object, Object)
      • addEdgeWithVertices

        public static <V,​E> E addEdgeWithVertices​(Graph<V,​E> g,
                                                        V sourceVertex,
                                                        V targetVertex)
        Adds the specified source and target vertices to the graph, if not already included, and creates a new edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
        Parameters:
        g - the graph for which the specified edge to be added.
        sourceVertex - source vertex of the edge.
        targetVertex - target vertex of the edge.
        Returns:
        The newly created edge if added to the graph, otherwise null.
      • addEdgeWithVertices

        public static <V,​E> boolean addEdgeWithVertices​(Graph<V,​E> targetGraph,
                                                              Graph<V,​E> sourceGraph,
                                                              E edge)
        Adds the specified edge to the graph, including its vertices if not already included.
        Parameters:
        targetGraph - the graph for which the specified edge to be added.
        sourceGraph - the graph in which the specified edge is already present
        edge - edge to add
        Returns:
        true if the target graph did not already contain the specified edge.
      • addEdgeWithVertices

        public static <V,​E> E addEdgeWithVertices​(Graph<V,​E> g,
                                                        V sourceVertex,
                                                        V targetVertex,
                                                        double weight)
        Adds the specified source and target vertices to the graph, if not already included, and creates a new weighted edge and adds it to the specified graph similarly to the Graph.addEdge(Object, Object) method.
        Parameters:
        g - the graph for which the specified edge to be added.
        sourceVertex - source vertex of the edge.
        targetVertex - target vertex of the edge.
        weight - weight of the edge.
        Returns:
        The newly created edge if added to the graph, otherwise null.
      • addGraph

        public static <V,​E> boolean addGraph​(Graph<? super V,​? super E> destination,
                                                   Graph<V,​E> source)
        Adds all the vertices and all the edges of the specified source graph to the specified destination graph. First all vertices of the source graph are added to the destination graph. Then every edge of the source graph is added to the destination graph. This method returns true if the destination graph has been modified as a result of this operation, otherwise it returns false.

        The behavior of this operation is undefined if any of the specified graphs is modified while operation is in progress.

        Parameters:
        destination - the graph to which vertices and edges are added.
        source - the graph used as source for vertices and edges to add.
        Returns:
        true if and only if the destination graph has been changed as a result of this operation.
      • addGraphReversed

        public static <V,​E> void addGraphReversed​(DirectedGraph<? super V,​? super E> destination,
                                                        DirectedGraph<V,​E> source)
        Adds all the vertices and all the edges of the specified source digraph to the specified destination digraph, reversing all of the edges. If you want to do this as a linked view of the source graph (rather than by copying to a destination graph), use EdgeReversedGraph instead.

        The behavior of this operation is undefined if any of the specified graphs is modified while operation is in progress.

        Parameters:
        destination - the graph to which vertices and edges are added.
        source - the graph used as source for vertices and edges to add.
        See Also:
        EdgeReversedGraph
      • addAllEdges

        public static <V,​E> boolean addAllEdges​(Graph<? super V,​? super E> destination,
                                                      Graph<V,​E> source,
                                                      java.util.Collection<? extends E> edges)
        Adds a subset of the edges of the specified source graph to the specified destination graph. The behavior of this operation is undefined if either of the graphs is modified while the operation is in progress. addEdgeWithVertices(org.jgrapht.Graph<V, E>, V, V) is used for the transfer, so source vertexes will be added automatically to the target graph.
        Parameters:
        destination - the graph to which edges are to be added
        source - the graph used as a source for edges to add
        edges - the edges to be added
        Returns:
        true if this graph changed as a result of the call
      • addAllVertices

        public static <V,​E> boolean addAllVertices​(Graph<? super V,​? super E> destination,
                                                         java.util.Collection<? extends V> vertices)
        Adds all of the specified vertices to the destination graph. The behavior of this operation is undefined if the specified vertex collection is modified while the operation is in progress. This method will invoke the Graph.addVertex(Object) method.
        Parameters:
        destination - the graph to which edges are to be added
        vertices - the vertices to be added to the graph.
        Returns:
        true if graph changed as a result of the call
        Throws:
        java.lang.NullPointerException - if the specified vertices contains one or more null vertices, or if the specified vertex collection is null.
        See Also:
        Graph.addVertex(Object)
      • neighborListOf

        public static <V,​E> java.util.List<V> neighborListOf​(Graph<V,​E> g,
                                                                   V vertex)
        Returns a list of vertices that are the neighbors of a specified vertex. If the graph is a multigraph vertices may appear more than once in the returned list.
        Parameters:
        g - the graph to look for neighbors in.
        vertex - the vertex to get the neighbors of.
        Returns:
        a list of the vertices that are the neighbors of the specified vertex.
      • predecessorListOf

        public static <V,​E> java.util.List<V> predecessorListOf​(DirectedGraph<V,​E> g,
                                                                      V vertex)
        Returns a list of vertices that are the direct predecessors of a specified vertex. If the graph is a multigraph, vertices may appear more than once in the returned list.
        Parameters:
        g - the graph to look for predecessors in.
        vertex - the vertex to get the predecessors of.
        Returns:
        a list of the vertices that are the direct predecessors of the specified vertex.
      • successorListOf

        public static <V,​E> java.util.List<V> successorListOf​(DirectedGraph<V,​E> g,
                                                                    V vertex)
        Returns a list of vertices that are the direct successors of a specified vertex. If the graph is a multigraph vertices may appear more than once in the returned list.
        Parameters:
        g - the graph to look for successors in.
        vertex - the vertex to get the successors of.
        Returns:
        a list of the vertices that are the direct successors of the specified vertex.
      • undirectedGraph

        public static <V,​E> UndirectedGraph<V,​E> undirectedGraph​(Graph<V,​E> g)
        Returns an undirected view of the specified graph. If the specified graph is directed, returns an undirected view of it. If the specified graph is already undirected, just returns it.
        Parameters:
        g - the graph for which an undirected view is to be returned.
        Returns:
        an undirected view of the specified graph, if it is directed, or or the specified graph itself if it is already undirected.
        Throws:
        java.lang.IllegalArgumentException - if the graph is neither DirectedGraph nor UndirectedGraph.
        See Also:
        AsUndirectedGraph
      • testIncidence

        public static <V,​E> boolean testIncidence​(Graph<V,​E> g,
                                                        E e,
                                                        V v)
        Tests whether an edge is incident to a vertex.
        Parameters:
        g - graph containing e and v
        e - edge in g
        v - vertex in g
        Returns:
        true iff e is incident on v
      • getOppositeVertex

        public static <V,​E> V getOppositeVertex​(Graph<V,​E> g,
                                                      E e,
                                                      V v)
        Gets the vertex opposite another vertex across an edge.
        Parameters:
        g - graph containing e and v
        e - edge in g
        v - vertex in g
        Returns:
        vertex opposite to v across e
      • getPathVertexList

        public static <V,​E> java.util.List<V> getPathVertexList​(GraphPath<V,​E> path)
        Gets the list of vertices visited by a path.
        Parameters:
        path - path of interest
        Returns:
        corresponding vertex list