Class Graphs
- java.lang.Object
-
- com.jgalgo.graph.Graphs
-
public class Graphs extends Object
Static methods class for graphs.- Author:
- Barak Ugav
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V,E>
booleancontainsParallelEdges(Graph<V,E> g)
Check whether a graph contain parallel edges.static <E> E
randEdge(Graph<?,E> g, Random rand)
Get a random edge from the given graph.static int
randEdge(IntGraph g, Random rand)
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>selfEdges(Graph<V,E> g)
Get a view of all the self edges in a graph.static IntSet
selfEdges(IntGraph g)
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 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
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
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
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
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 graphrand
- the random number generator- Returns:
- a random vertex from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no vertices
-
randVertex
public static int randVertex(IntGraph g, Random rand)
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
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 graphrand
- the random number generator- Returns:
- a random edge from the given graph
- Throws:
IllegalArgumentException
- if the graph is contains no edges
-
randEdge
public static int randEdge(IntGraph g, Random rand)
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
public static <V,E> Set<E> selfEdges(Graph<V,E> g)
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
public static IntSet selfEdges(IntGraph g)
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
public static <V,E> boolean containsParallelEdges(Graph<V,E> g)
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
-
-