Depth-first search (DFS)
Depth-first search (DFS) --- поиск в глубину.
1. Let be a directed graph.
It is convenient to formulate DFS as a
recursive procedure with a vertex
as a parameter. In general,
we search for unexplored vertices by traversing an unexplored arc from
the most recently reached vertex which still has unexplored arcs. The
set
contains the explored vertices. On these conditions
has the following main structure
The procedure starts with
and marks all vertices reachable from the start vertex .
But
gives some further information about the digraph
. In particular,
computes the so-called depth-first search tree (or
-tree) with a root
,
which consists of all vertices reachable from the
vertex
. If
is a cf-graph with an initial vertex
, then
-tree is a spanning tree
of
.
can be easily extended in such a way that for any cf-graph
with an initial vertex
,
computes in a linear time two correlated basic numberings of
and
makes the corresponding partition of all arcs of
into the four classes of
the tree, forward, backward and cross arcs with respect to
the basic numberings.
2. Let be an undirected graph.
Suppose that in a depth-first search of an
undirected graph we are currently visiting a vertex . The general
step in the search then requires that the next visited is a vertex adjacent
to
which has not yet been visited. If no such vertex exists, then
the search returns to the vertex visited just before
and the
general step is repeated until every vertex in that component of the
graph becomes visited. Such a search cannot revisit a vertex except
by returning to it via edges that have been used since the
previous visit. Hence, the edges traversed in a depth-first search form
a spanning tree for each separate component of the graph. This
set of trees is mathcalled a depth first spanning forest
. Thus,
partitions the edges
into two sets,
and
. The edges in
are mathcalled back-edges.
The time complexity of DFS in a general case
is
References
- Евстигнеев В.А., Касьянов В.Н. Словарь по графам в информатике. — Новосибирск: Сибирское Научное Издательство, 2009.