Class Graphs


  • public class Graphs
    extends Object
    Static methods class for graphs.
    Author:
    Barak Ugav
    • Method Detail

      • subGraph

        public static <V,​E> Graph<V,​E> subGraph​(Graph<V,​E> g,
                                                            Collection<V> vertices)
        Create a new graph that is an induced subgraph of the given graph.

        An induced subgraph of a graph \(G=(V,E)\) is a graph \(G'=(V',E')\) where \(V' \subseteq V\) and \(E' = \{\{u,v\} \mid u,v \in V', \{u,v\} \in E\}\). The created graph will have the same type (directed/undirected) as the given graph. The vertices and edges of the created graph will be a subset of the vertices and edges of the given graph.

        The weights of both vertices and edges will not be copied to the new sub graph. For more flexible sub graph creation, see subGraph(Graph, Collection, Collection, boolean, boolean).

        If g is IntGraph, the returned sub graph is also an IntGraph.

        Type Parameters:
        V - the vertices type
        E - the edges type
        Parameters:
        g - the graph to create a sub graph from
        vertices - the vertices of the sub graph
        Returns:
        a new graph that is an induced subgraph of the given graph
      • subGraph

        public static <V,​E> Graph<V,​E> subGraph​(Graph<V,​E> g,
                                                            Collection<V> vertices,
                                                            Collection<E> edges)
        Create a new graph that is a subgraph of the given graph.

        If edges is null, then the created graph will be an induced subgraph of the given graph, namely an induced subgraph of a graph \(G=(V,E)\) is a graph \(G'=(V',E')\) where \(V' \subseteq V\) and \(E' = \{\{u,v\} \mid u,v \in V', \{u,v\} \in E\}\). The behavior is similar to subGraph(Graph, Collection). vertices must not be null in this case.

        If vertices is null, then edges must not be null, and the sub graph will contain all the vertices which are either a source or a target of an edge in edges.

        The created graph will have the same type (directed/undirected) as the given graph. The vertices and edges of the created graph will be a subset of the vertices and edges of the given graph.

        The weights of both vertices and edges will not be copied to the new sub graph. For more flexible sub graph creation, see subGraph(Graph, Collection, Collection, boolean, boolean).

        If g is IntGraph, the returned sub graph is also an IntGraph.

        Type Parameters:
        V - the vertices type
        E - the edges type
        Parameters:
        g - the graph to create a sub graph from
        vertices - the vertices of the sub graph, if null then edges must not be null and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge in edges
        edges - the edges of the sub graph, if null then vertices must not be null and the sub graph will be an induced subgraph of the given graph
        Returns:
        a new graph that is a subgraph of the given graph
        Throws:
        NullPointerException - if both vertices and edges are null
      • subGraph

        public static <V,​E> Graph<V,​E> subGraph​(Graph<V,​E> g,
                                                            Collection<V> vertices,
                                                            Collection<E> edges,
                                                            boolean copyVerticesWeights,
                                                            boolean copyEdgesWeights)
        Create a new graph that is a subgraph of the given graph, with option to copy weights.

        If edges is null, then the created graph will be an induced subgraph of the given graph, namely an induced subgraph of a graph \(G=(V,E)\) is a graph \(G'=(V',E')\) where \(V' \subseteq V\) and \(E' = \{\{u,v\} \mid u,v \in V', \{u,v\} \in E\}\). The behavior is similar to subGraph(Graph, Collection). vertices must not be null in this case.

        If vertices is null, then edges must not be null, and the sub graph will contain all the vertices which are either a source or a target of an edge in edges.

        The created graph will have the same type (directed/undirected) as the given graph. The vertices and edges of the created graph will be a subset of the vertices and edges of the given graph.

        An additional parameter options for copying the weights of the vertices and edges of the given graph to the new sub graph are provided. If copyVerticesWeights is true, then all the vertices weights of the given graph will be copied to the new sub graph. If copyEdgesWeights is true, then all the edges weights of the given graph will be copied to the new sub graph.

        If g is IntGraph, the returned sub graph is also an IntGraph.

        Type Parameters:
        V - the vertices type
        E - the edges type
        Parameters:
        g - the graph to create a sub graph from
        vertices - the vertices of the sub graph, if null then edges must not be null and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge in edges
        edges - the edges of the sub graph, if null then vertices must not be null and the sub graph will be an induced subgraph of the given graph
        copyVerticesWeights - if true then all the vertices weights of the given graph will be copied to the new sub graph
        copyEdgesWeights - if true then all the edges weights of the given graph will be copied to the new sub graph
        Returns:
        a new graph that is a subgraph of the given graph
        Throws:
        NullPointerException - if both vertices and edges are null
      • subGraph

        public static IntGraph subGraph​(IntGraph g,
                                        IntCollection vertices,
                                        IntCollection edges,
                                        boolean copyVerticesWeights,
                                        boolean copyEdgesWeights)
        Create a new graph that is a subgraph of the given int graph, with option to copy weights.

        If edges is null, then the created graph will be an induced subgraph of the given graph, namely an induced subgraph of a graph \(G=(V,E)\) is a graph \(G'=(V',E')\) where \(V' \subseteq V\) and \(E' = \{\{u,v\} \mid u,v \in V', \{u,v\} \in E\}\).

        If vertices is null, then edges must not be null, and the sub graph will contain all the vertices which are either a source or a target of an edge in edges.

        The created graph will have the same type (directed/undirected) as the given graph. The vertices and edges of the created graph will be a subset of the vertices and edges of the given graph.

        An additional parameter options for copying the weights of the vertices and edges of the given graph to the new sub graph are provided. If copyVerticesWeights is true, then all the vertices weights of the given graph will be copied to the new sub graph. If copyEdgesWeights is true, then all the edges weights of the given graph will be copied to the new sub graph.

        Parameters:
        g - the graph to create a sub graph from
        vertices - the vertices of the sub graph, if null then edges must not be null and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge in edges
        edges - the edges of the sub graph, if null then vertices must not be null and the sub graph will be an induced subgraph of the given graph
        copyVerticesWeights - if true then all the vertices weights of the given graph will be copied to the new sub graph
        copyEdgesWeights - if true then all the edges weights of the given graph will be copied to the new sub graph
        Returns:
        a new graph that is a subgraph of the given graph
        Throws:
        NullPointerException - if both vertices and edges are null
      • randVertex

        public static <V> V randVertex​(Graph<V,​?> g,
                                       Random rand)
        Get a random vertex from the given graph.
        Type Parameters:
        V - the vertices type
        Parameters:
        g - the graph
        rand - the random number generator
        Returns:
        a random vertex from the given graph
      • randVertex

        public static int randVertex​(IntGraph g,
                                     Random rand)
        Get a random vertex from the given int graph.
        Parameters:
        g - the graph
        rand - the random number generator
        Returns:
        a random vertex from the given graph
      • randEdge

        public static <E> E randEdge​(Graph<?,​E> g,
                                     Random rand)
        Get a random edge from the given graph.
        Type Parameters:
        E - the edges type
        Parameters:
        g - the graph
        rand - the random number generator
        Returns:
        a random edge from the given graph
      • randEdge

        public static int randEdge​(IntGraph g,
                                   Random rand)
        Get a random edge from the given int graph.
        Parameters:
        g - the graph
        rand - the random number generator
        Returns:
        a random edge from the given graph