SIAM J. COMPUT.
Vol. 1, No. 2, June 1972
DEPTHFIRST SEARCH
AND LINEAR
GRAPH ALGORITHMS*
ROBERT
TARJAN"
Abstract. The value of depthfirst search or "bacltracking" as a technique for solving problems is
illustrated by two examples.
An improved
version of an algorithm for finding the strongly connected
components of a directed graph and ar algorithm for finding the biconnected components of an un
direct graph are presented. The space and time requirements
of both algorithms are bounded by
k
1V +
k2E
d
k
for some constants kl, k2, and k
a,
where Vis the number ofvertices and E is the number
of edges of the graph being examined.
Key words. Algorithm, backtracking, biconnectivity, connectivity, depthfirst, graph, search,
spanning tree, strongconnectivity.
1.
Introduction. Consider a
graph G, consisting of a set of vertices
U and
a
set of
edges
g.
The graph may either be directed
(the edges are ordered pairs (v, w)
of vertices; v is the tail and w is the head of the edge) or undirected (the edges are
unordered pairs of vertices, also represented as (v, w)). Graphs form a suitable
abstraction for problems in many areas; chemistry, electrical engineering, and
sociology, for example. Thus it is important to have the most economical algo
rithms for answering graphtheoretical questions.
In studying graph algorithms we cannot avoid at least a few definitions.
These definitions are moreorless standard in the literature. (See Harary [3],
for instance.) If G
(, g)
is a
graph, a path p’v
w in G is a sequence ofvertices
and edges leading from v to w.
A path
is simple if all its vertices are distinct.
A path
p’v
v is called a closed path.
A
closed path p’v
v is a cycle if all its
edges are
distinct and the only vertex to occur twice in p is v, which occurs exactly twice.
Two cycles
which are cyclic permutations of each other are considered to be the
same
cycle. The undirected version of a directed graph
is
the graph formed by
converting each edge of the directed graph into an undirected edge and removing
duplicate edges.
An
undirected graph is connected if there is a path between every
pair of vertices.
A (directed rooted)
tree
T
is
a directed graph whose undirected version is
connected, having one vertex which
is
the head of no edges (called the root),
and such that all vertices except the root are the head of exactly one edge. The
relation "(v, w) is an
edge of T"
is
denoted by
v w. The relation "There is a
path from v to
w in T" is denoted
by v
w.
If v

w, v
is the
father
of w and w is a
son of v. If v
w, v is an ancestor of w and w is a descendant of
v.
Every vertex
is an
ancestor and a descendant of itself. If v is a vertex in a tree
T, T
is the subtree of T
having as vertices all the descendants of v in T. If G is a directed graph, a tree T
is a spanning tree of G if
T
is a
subgraph of G and T contains all the vertices of G.
If R and S