<s>
Depth-first	B-Algorithm
search	I-Algorithm
(	O
DFS	O
)	O
is	O
an	O
algorithm	O
for	O
traversing	O
or	O
searching	O
tree	B-Application
or	O
graph	B-Application
data	I-Application
structures	I-Application
.	O
</s>
<s>
The	O
algorithm	O
starts	O
at	O
the	O
root	B-Application
node	I-Application
(	O
selecting	O
some	O
arbitrary	O
node	O
as	O
the	O
root	B-Application
node	I-Application
in	O
the	O
case	O
of	O
a	O
graph	B-Application
)	O
and	O
explores	O
as	O
far	O
as	O
possible	O
along	O
each	O
branch	O
before	O
backtracking	O
.	O
</s>
<s>
Extra	O
memory	O
,	O
usually	O
a	O
stack	B-Application
,	O
is	O
needed	O
to	O
keep	O
track	O
of	O
the	O
nodes	O
discovered	O
so	O
far	O
along	O
a	O
specified	O
branch	O
which	O
helps	O
in	O
backtracking	O
of	O
the	O
graph	B-Application
.	O
</s>
<s>
A	O
version	O
of	O
depth-first	B-Algorithm
search	I-Algorithm
was	O
investigated	O
in	O
the	O
19th	O
century	O
by	O
French	O
mathematician	O
Charles	O
Pierre	O
Trémaux	O
as	O
a	O
strategy	O
for	O
solving	B-Algorithm
mazes	I-Algorithm
.	O
</s>
<s>
The	O
time	O
and	O
space	B-General_Concept
analysis	O
of	O
DFS	O
differs	O
according	O
to	O
its	O
application	O
area	O
.	O
</s>
<s>
In	O
theoretical	O
computer	O
science	O
,	O
DFS	O
is	O
typically	O
used	O
to	O
traverse	O
an	O
entire	O
graph	B-Application
,	O
and	O
takes	O
time	O
where	O
is	O
the	O
number	O
of	O
vertices	O
and	O
the	O
number	O
of	O
edges	O
.	O
</s>
<s>
This	O
is	O
linear	O
in	O
the	O
size	O
of	O
the	O
graph	B-Application
.	O
</s>
<s>
In	O
these	O
applications	O
it	O
also	O
uses	O
space	B-General_Concept
in	O
the	O
worst	O
case	O
to	O
store	O
the	O
stack	B-Application
of	O
vertices	O
on	O
the	O
current	O
search	O
path	O
as	O
well	O
as	O
the	O
set	O
of	O
already-visited	O
vertices	O
.	O
</s>
<s>
Thus	O
,	O
in	O
this	O
setting	O
,	O
the	O
time	O
and	O
space	B-General_Concept
bounds	O
are	O
the	O
same	O
as	O
for	O
breadth-first	B-Algorithm
search	I-Algorithm
and	O
the	O
choice	O
of	O
which	O
of	O
these	O
two	O
algorithms	O
to	O
use	O
depends	O
less	O
on	O
their	O
complexity	O
and	O
more	O
on	O
the	O
different	O
properties	O
of	O
the	O
vertex	O
orderings	O
the	O
two	O
algorithms	O
produce	O
.	O
</s>
<s>
For	O
applications	O
of	O
DFS	O
in	O
relation	O
to	O
specific	O
domains	O
,	O
such	O
as	O
searching	O
for	O
solutions	O
in	O
artificial	B-Application
intelligence	I-Application
or	O
web-crawling	O
,	O
the	O
graph	B-Application
to	O
be	O
traversed	O
is	O
often	O
either	O
too	O
large	O
to	O
visit	O
in	O
its	O
entirety	O
or	O
infinite	O
(	O
DFS	O
may	O
suffer	O
from	O
non-termination	O
)	O
.	O
</s>
<s>
In	O
such	O
cases	O
,	O
search	O
is	O
only	O
performed	O
to	O
a	O
limited	B-Algorithm
depth	I-Algorithm
;	O
due	O
to	O
limited	O
resources	O
,	O
such	O
as	O
memory	O
or	O
disk	O
space	B-General_Concept
,	O
one	O
typically	O
does	O
not	O
use	O
data	O
structures	O
to	O
keep	O
track	O
of	O
the	O
set	O
of	O
all	O
previously	O
visited	O
vertices	O
.	O
</s>
<s>
When	O
search	O
is	O
performed	O
to	O
a	O
limited	B-Algorithm
depth	I-Algorithm
,	O
the	O
time	O
is	O
still	O
linear	O
in	O
terms	O
of	O
the	O
number	O
of	O
expanded	O
vertices	O
and	O
edges	O
(	O
although	O
this	O
number	O
is	O
not	O
the	O
same	O
as	O
the	O
size	O
of	O
the	O
entire	O
graph	B-Application
because	O
some	O
vertices	O
may	O
be	O
searched	O
more	O
than	O
once	O
and	O
others	O
not	O
at	O
all	O
)	O
but	O
the	O
space	B-General_Concept
complexity	O
of	O
this	O
variant	O
of	O
DFS	O
is	O
only	O
proportional	O
to	O
the	O
depth	O
limit	O
,	O
and	O
as	O
a	O
result	O
,	O
is	O
much	O
smaller	O
than	O
the	O
space	B-General_Concept
needed	O
for	O
searching	O
to	O
the	O
same	O
depth	O
using	O
breadth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
For	O
such	O
applications	O
,	O
DFS	O
also	O
lends	O
itself	O
much	O
better	O
to	O
heuristic	B-Algorithm
methods	O
for	O
choosing	O
a	O
likely-looking	O
branch	O
.	O
</s>
<s>
When	O
an	O
appropriate	O
depth	O
limit	O
is	O
not	O
known	O
a	O
priori	O
,	O
iterative	B-Algorithm
deepening	I-Algorithm
depth-first	I-Algorithm
search	I-Algorithm
applies	O
DFS	O
repeatedly	O
with	O
a	O
sequence	O
of	O
increasing	O
limits	O
.	O
</s>
<s>
In	O
the	B-Application
artificial	I-Application
intelligence	I-Application
mode	O
of	O
analysis	O
,	O
with	O
a	O
branching	B-Data_Structure
factor	I-Data_Structure
greater	O
than	O
one	O
,	O
iterative	B-Algorithm
deepening	I-Algorithm
increases	O
the	O
running	O
time	O
by	O
only	O
a	O
constant	O
factor	O
over	O
the	O
case	O
in	O
which	O
the	O
correct	O
depth	O
limit	O
is	O
known	O
due	O
to	O
the	O
geometric	O
growth	O
of	O
the	O
number	O
of	O
nodes	O
per	O
level	O
.	O
</s>
<s>
DFS	O
may	O
also	O
be	O
used	O
to	O
collect	O
a	O
sample	O
of	O
graph	B-Application
nodes	O
.	O
</s>
<s>
For	O
the	O
following	O
graph	B-Application
:	O
</s>
<s>
a	O
depth-first	B-Algorithm
search	I-Algorithm
starting	O
at	O
the	O
node	O
A	O
,	O
assuming	O
that	O
the	O
left	O
edges	O
in	O
the	O
shown	O
graph	B-Application
are	O
chosen	O
before	O
right	O
edges	O
,	O
and	O
assuming	O
the	O
search	O
remembers	O
previously	O
visited	O
nodes	O
and	O
will	O
not	O
repeat	O
them	O
(	O
since	O
this	O
is	O
a	O
small	O
graph	B-Application
)	O
,	O
will	O
visit	O
the	O
nodes	O
in	O
the	O
following	O
order	O
:	O
A	O
,	O
B	O
,	O
D	O
,	O
F	O
,	O
E	O
,	O
C	O
,	O
G	O
.	O
The	O
edges	O
traversed	O
in	O
this	O
search	O
form	O
a	O
Trémaux	O
tree	B-Application
,	O
a	O
structure	O
with	O
important	O
applications	O
in	O
graph	B-Application
theory	O
.	O
</s>
<s>
Iterative	B-Algorithm
deepening	I-Algorithm
is	O
one	O
technique	O
to	O
avoid	O
this	O
infinite	O
loop	O
and	O
would	O
reach	O
all	O
nodes	O
.	O
</s>
<s>
The	O
result	O
of	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
of	O
a	O
graph	B-Application
can	O
be	O
conveniently	O
described	O
in	O
terms	O
of	O
a	O
spanning	O
tree	B-Application
of	O
the	O
vertices	O
reached	O
during	O
the	O
search	O
.	O
</s>
<s>
Based	O
on	O
this	O
spanning	O
tree	B-Application
,	O
the	O
edges	O
of	O
the	O
original	O
graph	B-Application
can	O
be	O
divided	O
into	O
three	O
classes	O
:	O
forward	O
edges	O
,	O
which	O
point	O
from	O
a	O
node	O
of	O
the	O
tree	B-Application
to	O
one	O
of	O
its	O
descendants	O
,	O
back	O
edges	O
,	O
which	O
point	O
from	O
a	O
node	O
to	O
one	O
of	O
its	O
ancestors	O
,	O
and	O
cross	O
edges	O
,	O
which	O
do	O
neither	O
.	O
</s>
<s>
Sometimes	O
tree	B-Application
edges	O
,	O
edges	O
which	O
belong	O
to	O
the	O
spanning	O
tree	B-Application
itself	O
,	O
are	O
classified	O
separately	O
from	O
forward	O
edges	O
.	O
</s>
<s>
If	O
the	O
original	O
graph	B-Application
is	O
undirected	O
then	O
all	O
of	O
its	O
edges	O
are	O
tree	B-Application
edges	O
or	O
back	O
edges	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
use	O
depth-first	B-Algorithm
search	I-Algorithm
to	O
linearly	O
order	O
the	O
vertices	O
of	O
a	O
graph	B-Application
or	O
tree	B-Application
.	O
</s>
<s>
A	O
preordering	O
is	O
a	O
list	O
of	O
the	O
vertices	O
in	O
the	O
order	O
that	O
they	O
were	O
first	O
visited	O
by	O
the	O
depth-first	B-Algorithm
search	I-Algorithm
algorithm	O
.	O
</s>
<s>
A	O
preordering	O
of	O
an	O
expression	O
tree	B-Application
is	O
the	O
expression	O
in	O
Polish	O
notation	O
.	O
</s>
<s>
A	O
postordering	O
of	O
an	O
expression	O
tree	B-Application
is	O
the	O
expression	O
in	O
reverse	O
Polish	O
notation	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
searching	O
the	O
directed	O
graph	B-Application
below	O
beginning	O
at	O
node	O
A	O
,	O
the	O
sequence	O
of	O
traversals	O
is	O
either	O
A	O
B	O
D	O
B	O
A	O
C	O
A	O
or	O
A	O
C	O
D	O
C	O
A	O
B	O
A	O
(	O
choosing	O
to	O
first	O
visit	O
B	O
or	O
C	O
from	O
A	B-Application
is	I-Application
up	O
to	O
the	O
algorithm	O
)	O
.	O
</s>
<s>
Reverse	O
postordering	O
produces	O
a	O
topological	B-Algorithm
sorting	I-Algorithm
of	O
any	O
directed	O
acyclic	O
graph	B-Application
.	O
</s>
<s>
The	O
graph	B-Application
above	O
might	O
represent	O
the	O
flow	O
of	O
control	O
in	O
the	O
code	O
fragment	O
below	O
,	O
and	O
it	O
is	O
natural	O
to	O
consider	O
this	O
code	O
in	O
the	O
order	O
A	O
B	O
C	O
D	O
or	O
A	O
C	O
B	O
D	O
but	O
not	O
natural	O
to	O
use	O
the	O
order	O
A	O
B	O
D	O
C	O
or	O
A	O
C	O
D	O
B	O
.	O
</s>
<s>
A	O
non-recursive	O
implementation	O
of	O
DFS	O
with	O
worst-case	O
space	B-General_Concept
complexity	O
,	O
with	O
the	O
possibility	O
of	O
duplicate	O
vertices	O
on	O
the	O
stack	B-Application
:	O
</s>
<s>
The	O
recursive	O
implementation	O
will	O
visit	O
the	O
nodes	O
from	O
the	O
example	O
graph	B-Application
in	O
the	O
following	O
order	O
:	O
A	O
,	O
B	O
,	O
D	O
,	O
F	O
,	O
E	O
,	O
C	O
,	O
G	O
.	O
The	O
non-recursive	O
implementation	O
will	O
visit	O
the	O
nodes	O
as	O
:	O
A	O
,	O
E	O
,	O
F	O
,	O
B	O
,	O
D	O
,	O
C	O
,	O
G	O
.	O
</s>
<s>
The	O
non-recursive	O
implementation	O
is	O
similar	O
to	O
breadth-first	B-Algorithm
search	I-Algorithm
but	O
differs	O
from	O
it	O
in	O
two	O
ways	O
:	O
</s>
<s>
it	O
delays	O
checking	O
whether	O
a	O
vertex	O
has	O
been	O
discovered	O
until	O
the	O
vertex	O
is	O
popped	O
from	O
the	O
stack	B-Application
rather	O
than	O
making	O
this	O
check	O
before	O
adding	O
the	O
vertex	O
.	O
</s>
<s>
If	O
is	O
a	O
tree	B-Application
,	O
replacing	O
the	O
queue	O
of	O
the	O
breadth-first	B-Algorithm
search	I-Algorithm
algorithm	O
with	O
a	O
stack	B-Application
will	O
yield	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
algorithm	O
.	O
</s>
<s>
For	O
general	O
graphs	O
,	O
replacing	O
the	O
stack	B-Application
of	O
the	O
iterative	O
depth-first	B-Algorithm
search	I-Algorithm
implementation	O
with	O
a	O
queue	O
would	O
also	O
produce	O
a	O
breadth-first	B-Algorithm
search	I-Algorithm
algorithm	O
,	O
although	O
a	O
somewhat	O
nonstandard	O
one	O
.	O
</s>
<s>
Another	O
possible	O
implementation	O
of	O
iterative	O
depth-first	B-Algorithm
search	I-Algorithm
uses	O
a	O
stack	B-Application
of	O
iterators	O
of	O
the	O
list	O
of	O
neighbors	O
of	O
a	O
node	O
,	O
instead	O
of	O
a	O
stack	B-Application
of	O
nodes	O
.	O
</s>
<s>
Algorithms	O
that	O
use	O
depth-first	B-Algorithm
search	I-Algorithm
as	O
a	O
building	O
block	O
include	O
:	O
</s>
<s>
Topological	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
Finding	O
the	O
bridges	O
of	O
a	O
graph	B-Application
.	O
</s>
<s>
Determining	O
whether	O
a	O
species	O
is	O
closer	O
to	O
one	O
species	O
or	O
another	O
in	O
a	O
phylogenetic	O
tree	B-Application
.	O
</s>
<s>
Solving	O
puzzles	O
with	O
only	O
one	O
solution	O
,	O
such	O
as	O
mazes	B-Application
.	O
</s>
<s>
(	O
DFS	O
can	O
be	O
adapted	O
to	O
find	O
all	O
solutions	O
to	O
a	O
maze	B-Application
by	O
only	O
including	O
nodes	O
on	O
the	O
current	O
path	O
in	O
the	O
visited	O
set	O
.	O
)	O
</s>
<s>
Maze	B-Application
generation	O
may	O
use	O
a	O
randomized	O
DFS	O
.	O
</s>
<s>
The	O
computational	B-General_Concept
complexity	I-General_Concept
of	O
DFS	O
was	O
investigated	O
by	O
John	O
Reif	O
.	O
</s>
<s>
More	O
precisely	O
,	O
given	O
a	O
graph	B-Application
,	O
let	O
be	O
the	O
ordering	O
computed	O
by	O
the	O
standard	O
recursive	O
DFS	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
This	O
ordering	O
is	O
called	O
the	O
lexicographic	O
depth-first	B-Algorithm
search	I-Algorithm
ordering	O
.	O
</s>
<s>
John	O
Reif	O
considered	O
the	O
complexity	O
of	O
computing	O
the	O
lexicographic	O
depth-first	B-Algorithm
search	I-Algorithm
ordering	O
,	O
given	O
a	O
graph	B-Application
and	O
a	O
source	O
.	O
</s>
<s>
A	O
decision	O
version	O
of	O
the	O
problem	O
(	O
testing	O
whether	O
some	O
vertex	O
occurs	O
before	O
some	O
vertex	O
in	O
this	O
order	O
)	O
is	O
P-complete	O
,	O
meaning	O
that	O
it	O
is	O
"	O
a	O
nightmare	O
for	O
parallel	B-Operating_System
processing	I-Operating_System
"	O
.	O
</s>
<s>
A	O
depth-first	B-Algorithm
search	I-Algorithm
ordering	O
(	O
not	O
necessarily	O
the	O
lexicographic	O
one	O
)	O
,	O
can	O
be	O
computed	O
by	O
a	O
randomized	O
parallel	B-Operating_System
algorithm	I-Operating_System
in	O
the	O
complexity	O
class	O
RNC	O
.	O
</s>
<s>
As	O
of	O
1997	O
,	O
it	O
remained	O
unknown	O
whether	O
a	O
depth-first	B-Algorithm
traversal	I-Algorithm
could	O
be	O
constructed	O
by	O
a	O
deterministic	O
parallel	B-Operating_System
algorithm	I-Operating_System
,	O
in	O
the	O
complexity	O
class	O
NC	O
.	O
</s>
