digraph
Directed Graphs
The digraph
module implements a version of labeled
directed graphs. What makes the graphs implemented here
non-proper directed graphs is that multiple edges between
vertices are allowed. However, the customary definition of
directed graphs will be used in the text that follows.
A directed graph (or just "digraph") is a pair (V, E) of a finite set V of vertices and a finite set E of directed edges (or just "edges"). The set of edges E is a subset of V × V (the Cartesian product of V with itself). In this module, V is allowed to be empty; the so obtained unique digraph is called the empty digraph. Both vertices and edges are represented by unique Erlang terms.
Digraphs can be annotated with additional information. Such information may be attached to the vertices and to the edges of the digraph. A digraph which has been annotated is called a labeled digraph, and the information attached to a vertex or an edge is called a label. Labels are Erlang terms.
An edge e = (v, w) is said to emanate from vertex v and to be incident on vertex w. The out-degree of a vertex is the number of edges emanating from that vertex. The in-degree of a vertex is the number of edges incident on that vertex. If there is an edge emanating from v and incident on w, then w is said to be an out-neighbour of v, and v is said to be an in-neighbour of w. A path P from v[1] to v[k] in a digraph (V, E) is a non-empty sequence v[1], v[2], ..., v[k] of vertices in V such that there is an edge (v[i],v[i+1]) in E for 1 <= i < k. The length of the path P is k-1. P is simple if all vertices are distinct, except that the first and the last vertices may be the same. P is a cycle if the length of P is not zero and v[1] = v[k]. A loop is a cycle of length one. A simple cycle is a path that is both a cycle and simple. An acyclic digraph is a digraph that has no cycles.
A digraph as returned by new/0,1
.
Functions
add_edge/3
add_edge/4
add_edge/5
add_edge/5
creates (or modifies) the edge
of the digraph
, using
as the (new)
label of the edge. The
edge is emanating from
and incident
on
. Returns
.
add_edge(
is
equivalent to
add_edge(
,
where
is a created edge. The created edge is
represented by the term ['$e' | N]
, where N
is an integer >= 0.
add_edge(
is equivalent to
add_edge(
.
If the edge would create a cycle in
an acyclic digraph,
then {error, {bad_edge,
is returned. If
either of
or
is not a vertex of the
digraph
, then
{error, {bad_vertex,
}}
is
returned,
or
.
add_vertex/1
add_vertex/2
add_vertex/3
add_vertex/3
creates (or modifies) the vertex
of the digraph
, using
as the (new)
label of the
vertex. Returns
.
add_vertex(
is equivalent to
add_vertex(
.
add_vertex/1
creates a vertex using the empty list
as label, and returns the created vertex. The created vertex
is represented by the term ['$v' | N]
,
where N is an integer >= 0.
del_edge/2
Deletes the edge
from the digraph
.
del_edges/2
Deletes the edges in the list
from the digraph
.
del_path/3
Deletes edges from the digraph
until there are no
paths from the vertex
to the vertex
.
A sketch of the procedure employed: Find an arbitrary
simple path
v[1], v[2], ..., v[k] from
to
in
. Remove all edges of
emanating from v[i]
and incident to v[i+1] for
1 <= i < k (including multiple
edges). Repeat until there is no path between
and
.
del_vertex/2
del_vertices/2
Deletes the vertices in the list
from the
digraph
.
delete/1
Deletes the digraph
. This call is important
because digraphs are implemented with ETS
. There is
no garbage collection of ETS
tables. The digraph
will, however, be deleted if the process that created the
digraph terminates.
edge/2
edges/1
Returns a list of all edges of the digraph
, in
some unspecified order.
edges/2
get_cycle/2
If there is
a simple cycle of
length two or more through the vertex
, then the cycle is returned as a list
[
of vertices, otherwise if there
is a loop through
, then the loop is returned as a list [
. If
there are no cycles through
, then false
is
returned.
get_path/3
is used for finding a simple cycle
through
.
get_path/3
Tries to find
a simple path from
the vertex
to the vertex
of the digraph
. Returns the path as a
list [
of vertices, or
false
if no simple path from
to
of length one or more exists.
The digraph
is traversed in a depth-first manner,
and the first path found is returned.
get_short_cycle/2
Tries to find an as short as
possible simple cycle through
the vertex
of the digraph G
. Returns the cycle
as a list [
of vertices, or
false
if no simple cycle through
exists.
Note that a loop through
is returned as the list [
.
get_short_path/3
is used for finding a simple cycle
through
.
get_short_path/3
Tries to find an as short as
possible simple path from
the vertex
to the vertex
of the digraph
.
Returns the path as a list [
of
vertices, or false
if no simple path from
to
of length one or more exists.
The digraph
is traversed in a breadth-first
manner, and the first path found is returned.
in_degree/2
Returns the in-degree of the vertex
of the digraph
.
in_edges/2
Returns a list of all
edges incident on
of the digraph
, in some unspecified order.
in_neighbours/2
Returns a list of
all in-neighbours of
of the digraph
, in some unspecified order.
info/1
Returns a list of {Tag, Value}
pairs describing the
digraph
. The following pairs are returned:
-
{cyclicity,
, whereCyclicity }
isCyclicity cyclic
oracyclic
, according to the options given tonew
. -
{memory,
, whereNoWords }
is the number of words allocated to theNoWords ETS
tables. -
{protection,
, whereProtection }
isProtection protected
orprivate
, according to the options given tonew
.
new/0
Equivalent to new([])
.
new/1
Returns
an empty digraph with
properties according to the options in
:
cyclic
acyclic
protected
private
If an unrecognized type option T
is given or
is not a proper list, there will be a badarg
exception.
no_edges/1
Returns the number of edges of the digraph
.
no_vertices/1
Returns the number of vertices of the digraph
.
out_degree/2
Returns the out-degree of the vertex
of the digraph
.
out_edges/2
Returns a list of all
edges emanating from
of the digraph
, in some unspecified order.
out_neighbours/2
Returns a list of
all out-neighbours of
of the digraph
, in some unspecified order.
vertex/2
Returns {
where
is the
label of the vertex
of the digraph
, or false
if there
is no vertex
of the digraph
.
vertices/1
Returns a list of all vertices of the digraph
, in
some unspecified order.