Operators
LightGraphs.jl implements the following graph operators. In general, functions with two graph arguments will require them to be of the same type (either both SimpleGraph
or both SimpleDiGraph
).
Base.intersect
Base.join
Base.reverse
Base.reverse!
Base.union
LightGraphs.cartesian_product
LightGraphs.complement
LightGraphs.crosspath
LightGraphs.difference
LightGraphs.egonet
LightGraphs.induced_subgraph
LightGraphs.merge_vertices
LightGraphs.merge_vertices!
LightGraphs.symmetric_difference
LightGraphs.tensor_product
SparseArrays.blockdiag
Full Docs
Base.intersect
— Method.intersect(g, h)
Return a graph with edges that are only in both graph g
and graph h
.
Implementation Notes
This function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph.
Base.join
— Method.join(g, h)
Return a graph that combines graphs g
and h
using blockdiag
and then adds all the edges between the vertices in g
and those in h
.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
Base.reverse
— Function.reverse(g)
Return a directed graph where all edges are reversed from the original directed graph.
Implementation Notes
Preserves the eltype of the input graph.
Base.reverse!
— Function.reverse!(g)
In-place reverse of a directed graph (modifies the original graph).
Base.union
— Method.union(g, h)
Return a graph that combines graphs g
and h
by taking the set union of all vertices and edges.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
LightGraphs.cartesian_product
— Method.cartesian_product(g, h)
Return the (cartesian product)[https://en.wikipedia.org/wiki/Cartesianproductof_graphs] of g
and h
.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
LightGraphs.complement
— Method.complement(g)
Return the graph complement of a graph
Implementation Notes
Preserves the eltype of the input graph.
LightGraphs.crosspath
— Function.crosspath(len::Integer, g::Graph)
Return a graph that duplicates g
len
times and connects each vertex with its copies in a path.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
LightGraphs.difference
— Method.difference(g, h)
Return a graph with edges in graph g
that are not in graph h
.
Implementation Notes
Note that this function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph.
LightGraphs.egonet
— Method.egonet(g, v, d, distmx=weights(g))
Return the subgraph of g
induced by the neighbors of v
up to distance d
, using weights (optionally) provided by distmx
. This is equivalent to induced_subgraph
(g, neighborhood(g, v, d, dir=dir))[1].
Optional Arguments
dir=:out
: ifg
is directed, this argument specifies the edge direction
with respect to v
(i.e. :in
or :out
).
LightGraphs.induced_subgraph
— Method.induced_subgraph(g, vlist)
induced_subgraph(g, elist)
Return the subgraph of g
induced by the vertices in vlist
or edges in elist
along with a vector mapping the new vertices to the old ones (the vertex i
in the subgraph corresponds to the vertex vmap[i]
in g
.)
The returned graph has length(vlist)
vertices, with the new vertex i
corresponding to the vertex of the original graph in the i
-th position of vlist
.
Usage Examples
julia> g = CompleteGraph(10)
julia> sg, vmap = induced_subgraph(g, 5:8)
julia> @assert g[5:8] == sg
julia> @assert nv(sg) == 4
julia> @assert ne(sg) == 6
julia> @assert vm[4] == 8
julia> sg, vmap = induced_subgraph(g, [2,8,3,4])
julia> @assert sg == g[[2,8,3,4]]
julia> elist = [Edge(1,2), Edge(3,4), Edge(4,8)]
julia> sg, vmap = induced_subgraph(g, elist)
julia> @assert sg == g[elist]
LightGraphs.merge_vertices!
— Method.merge_vertices!(g, vs)
Combine vertices specified in vs
into single vertex whose index will be the lowest value in vs
. All edges connected to vertices in vs
connect to the new merged vertex.
Return a vector with new vertex values are indexed by the original vertex indices.
Implementation Notes
Supports SimpleGraph only.
LightGraphs.merge_vertices
— Method.merge_vertices(g::AbstractGraph, vs)
Create a new graph where all vertices in vs
have been aliased to the same vertex minimum(vs)
.
LightGraphs.symmetric_difference
— Method.symmetric_difference(g, h)
Return a graph with edges from graph g
that do not exist in graph h
, and vice versa.
Implementation Notes
Note that this function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
LightGraphs.tensor_product
— Method.tensor_product(g, h)
Return the (tensor product)[https://en.wikipedia.org/wiki/Tensorproductof_graphs] of g
and h
.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.
SparseArrays.blockdiag
— Method.blockdiag(g, h)
Return a graph with $|V(g)| + |V(h)|$ vertices and $|E(g)| + |E(h)|$ edges where the vertices an edges from graph h
are appended to graph g
.
Implementation Notes
Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype.