The digraph_utils module implements some algorithms
based on depth-first traversal of directed graphs. See the
digraph module for basic functions on directed graphs.
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).
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.
An edge e = (v, w) is said
to emanate from vertex v and
to be incident on vertex w.
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 a cycle if the length of P
is not zero and v[1] = v[k].
A loop is a cycle of length one.
An acyclic digraph is
a digraph that has no cycles.
A depth-first
traversal of a directed digraph can be viewed as a process
that visits all vertices of the digraph. Initially, all vertices
are marked as unvisited. The traversal starts with an
arbitrarily chosen vertex, which is marked as visited, and
follows an edge to an unmarked vertex, marking that vertex. The
search then proceeds from that vertex in the same fashion, until
there is no edge leading to an unvisited vertex. At that point
the process backtracks, and the traversal continues as long as
there are unexamined edges. If there remain unvisited vertices
when all edges from the first vertex have been examined, some
hitherto unvisited vertex is chosen, and the process is
repeated.
A partial ordering of
a set S is a transitive, antisymmetric and reflexive relation
between the objects of S. The problem
of topological sorting is to
find a total
ordering of S that is a superset of the partial ordering. A
digraph G = (V, E) is equivalent to a relation E
on V (we neglect the fact that the version of directed graphs
implemented in the digraph module allows multiple edges
between vertices). If the digraph has no cycles of length two or
more, then the reflexive and transitive closure of E is a
partial ordering.
A subgraph G' of G is a
digraph whose vertices and edges form subsets of the vertices
and edges of G. G' is maximal with respect to a
property P if all other subgraphs that include the vertices of
G' do not have the property P. A strongly connected
component is a maximal subgraph such that there is a path
between each pair of vertices. A connected component is a
maximal subgraph such that there is a path between each pair of
vertices, considering all edges undirected. An arborescence is an acyclic
digraph with a vertex V, the root, such that there is a unique
path from V to every other vertex of G. A tree is an acyclic non-empty digraph
such that there is a unique path between every pair of vertices,
considering all edges undirected.