Class Graphs
- Author:
- Barak Ugav
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V,
E> boolean containsParallelEdges
(Graph<V, E> g) Check whether a graph contain parallel edges.static <E> E
Get a random edge from the given graph.static int
Get a random edge from the given int graph.static <V> V
randVertex
(Graph<V, ?> g, Random rand) Get a random vertex from the given graph.static int
randVertex
(IntGraph g, Random rand) Get a random vertex from the given int graph.static <V,
E> Set <E> Get a view of all the self edges in a graph.static IntSet
Get a view of all the self edges in an int graph.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.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.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.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.
-
Method Details
-
subGraph
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
isIntGraph
, the returned sub graph is also anIntGraph
.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- the graph to create a sub graph fromvertices
- the vertices of the sub graph- Returns:
- a new graph that is an induced subgraph of the given graph
-
subGraph
Create a new graph that is a subgraph of the given graph.If
edges
isnull
, 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 tosubGraph(Graph, Collection)
.vertices
must not benull
in this case.If
vertices
isnull
, thenedges
must not benull
, and the sub graph will contain all the vertices which are either a source or a target of an edge inedges
.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
isIntGraph
, the returned sub graph is also anIntGraph
.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- the graph to create a sub graph fromvertices
- the vertices of the sub graph, ifnull
thenedges
must not benull
and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge inedges
edges
- the edges of the sub graph, ifnull
thenvertices
must not benull
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 bothvertices
andedges
arenull
-
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
isnull
, 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 tosubGraph(Graph, Collection)
.vertices
must not benull
in this case.If
vertices
isnull
, thenedges
must not benull
, and the sub graph will contain all the vertices which are either a source or a target of an edge inedges
.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
istrue
, then all the vertices weights of the given graph will be copied to the new sub graph. IfcopyEdgesWeights
istrue
, then all the edges weights of the given graph will be copied to the new sub graph.If
g
isIntGraph
, the returned sub graph is also anIntGraph
.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- the graph to create a sub graph fromvertices
- the vertices of the sub graph, ifnull
thenedges
must not benull
and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge inedges
edges
- the edges of the sub graph, ifnull
thenvertices
must not benull
and the sub graph will be an induced subgraph of the given graphcopyVerticesWeights
- iftrue
then all the vertices weights of the given graph will be copied to the new sub graphcopyEdgesWeights
- iftrue
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 bothvertices
andedges
arenull
-
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
isnull
, 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
isnull
, thenedges
must not benull
, and the sub graph will contain all the vertices which are either a source or a target of an edge inedges
.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
istrue
, then all the vertices weights of the given graph will be copied to the new sub graph. IfcopyEdgesWeights
istrue
, 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 fromvertices
- the vertices of the sub graph, ifnull
thenedges
must not benull
and the vertices of the sub graph will be all the vertices which are either a source or a target of an edge inedges
edges
- the edges of the sub graph, ifnull
thenvertices
must not benull
and the sub graph will be an induced subgraph of the given graphcopyVerticesWeights
- iftrue
then all the vertices weights of the given graph will be copied to the new sub graphcopyEdgesWeights
- iftrue
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 bothvertices
andedges
arenull
-
randVertex
Get a random vertex from the given graph.- Type Parameters:
V
- the vertices type- Parameters:
g
- the graphrand
- the random number generator- Returns:
- a random vertex from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no vertices
-
randVertex
Get a random vertex from the given int graph.- Parameters:
g
- the graphrand
- the random number generator- Returns:
- a random vertex from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no vertices
-
randEdge
Get a random edge from the given graph.- Type Parameters:
E
- the edges type- Parameters:
g
- the graphrand
- the random number generator- Returns:
- a random edge from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no edges
-
randEdge
Get a random edge from the given int graph.- Parameters:
g
- the graphrand
- the random number generator- Returns:
- a random edge from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no edges
-
selfEdges
Get a view of all the self edges in a graph.The returned set is a view, namely it will be updated when the graph is updated.
- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graph- Returns:
- a view of all the self edges in the graph
-
selfEdges
Get a view of all the self edges in an int graph.The returned set is a view, namely it will be updated when the graph is updated.
- Parameters:
g
- an int graph- Returns:
- a view of all the self edges in the graph
-
containsParallelEdges
Check whether a graph contain parallel edges.Two parallel edges are edges that have the same source and target vertices.
- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graph- Returns:
true
if the graph contain at least one pair of parallel edges, elsefalse
-