Graph access
The following is an overview of functions for accessing graph properties.
Global graph properties
nv(g)
returns the number of vertices ing
.ne(g)
returns the number of edges ing
.vertices(g)
returns an iterable object containing all the vertices ing
.edges(g)
returns an iterable object containing all the edges ing
.has_vertex(g, v)
checks whether graphg
includes a vertex numberedv
.has_edge(g, s, d)
checks whether graphg
includes an edge from the source vertexs
to the destination vertexd
.has_edge(g, e)
returns true if there is an edge in g that satisfiese == f
for anyf ∈ edges(g)
. This is a strict equality test that may require all properties ofe
are the same. This definition of equality depends on the implementation. For testing whether an edge exists between two verticess,d
usehas_edge(g, s, d)
. Note: to use thehas_edge(g, e)
method safely, it is important to understand the conditions under which edges are equal to each other. These conditions are defined by thehas_edge(g::G,e)
method as defined by the graph typeG
. The default behavior is to checkhas_edge(g,src(e),dst(e))
. This distinction exists to allow new graph types such as MetaGraphs or MultiGraphs to distinguish between edges with the same source and destination but potentially different properties.has_self_loops(g)
checks for self-loops ing
.is_directed(g)
checks ifg
is a directed graph.eltype(g)
returns the type of the vertices ofg
.
Vertex properties
neighbors(g, v)
returns the neighbors of vertexv
in an iterable (ifg
is directed, only outneighbors are returned).all_neighbors(
returns all the neighbors of vertexv
(ifg
is directed, both inneighbors and outneighbors are returned).inneighbors
return the inneighbors of vertexv
(equivalent toneighbors
for undirected graphs).outneighbors
returns the outneighbors of vertexv
(equivalent toneighbors
for undirected graphs).
Edge properties
src(e)
gives the source vertexs
of an edge(s, d)
.dst(e)
gives the destination vertexd
of an edge(s, d)
.reverse(e)
creates a new edge(d, s)
from edge(s, d)
.
Persistence of vertex indices
Adding a vertex to the graph with add_vertex!(g)
adds it (if successful) to the end of the "vertex-list". Therefore, it is possible to access the index of the recently added vertex by using nv(g)
:
julia> g = SimpleGraph(10)
{10, 0} undirected simple Int64 graph
julia> add_vertex!(g)
true
julia> last_added_vertex = nv(g)
11
Note that this index is NOT persistent if vertices added earlier are removed. When rem_vertex!(g, v)
is called, v
is "switched" with the last vertex before being deleted. As edges are identified by vertex indices, one has to be careful with edges as well. An edge added as add_edge!(g, 3, 11)
can not be expected to always pass the has_edge(g, 3, 11)
check:
julia> g = SimpleGraph(10)
{10, 0} undirected simple Int64 graph
julia> add_vertex!(g)
true
julia> add_edge!(g, 3, 11)
true
julia> g
{11, 1} undirected simple Int64 graph
julia> has_edge(g, 3, 11)
true
julia> rem_vertex!(g, 7)
true
julia> has_edge(g, 3, 11)
false
julia> has_edge(g, 3, 7) # vertex number 11 "renamed" to vertex number 7
true