<s>
Breadth-first	B-Algorithm
search	I-Algorithm
(	O
BFS	O
)	O
is	O
an	O
algorithm	O
for	O
searching	O
a	O
tree	B-Application
data	I-Application
structure	I-Application
for	O
a	O
node	O
that	O
satisfies	O
a	O
given	O
property	O
.	O
</s>
<s>
It	O
starts	O
at	O
the	O
tree	B-Application
root	O
and	O
explores	O
all	O
nodes	O
at	O
the	O
present	O
depth	O
prior	O
to	O
moving	O
on	O
to	O
the	O
nodes	O
at	O
the	O
next	O
depth	O
level	O
.	O
</s>
<s>
Extra	O
memory	O
,	O
usually	O
a	O
queue	B-Application
,	O
is	O
needed	O
to	O
keep	O
track	O
of	O
the	O
child	O
nodes	O
that	O
were	O
encountered	O
but	O
not	O
yet	O
explored	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
a	O
chess	O
endgame	O
a	O
chess	B-Application
engine	I-Application
may	O
build	O
the	O
game	O
tree	B-Application
from	O
the	O
current	O
position	O
by	O
applying	O
all	O
possible	O
moves	O
,	O
and	O
use	O
breadth-first	B-Algorithm
search	I-Algorithm
to	O
find	O
a	O
win	O
position	O
for	O
white	O
.	O
</s>
<s>
Implicit	O
trees	O
(	O
such	O
as	O
game	O
trees	O
or	O
other	O
problem-solving	O
trees	O
)	O
may	O
be	O
of	O
infinite	O
size	O
;	O
breadth-first	B-Algorithm
search	I-Algorithm
is	O
guaranteed	O
to	O
find	O
a	O
solution	O
node	O
if	O
one	O
exists	O
.	O
</s>
<s>
In	O
contrast	O
,	O
(	O
plain	O
)	O
depth-first	B-Algorithm
search	I-Algorithm
,	O
which	O
explores	O
the	O
node	O
branch	O
as	O
far	O
as	O
possible	O
before	O
backtracking	O
and	O
expanding	O
other	O
nodes	O
,	O
may	O
get	O
lost	O
in	O
an	O
infinite	O
branch	O
and	O
never	O
make	O
it	O
to	O
the	O
solution	O
node	O
.	O
</s>
<s>
Iterative	B-Algorithm
deepening	I-Algorithm
depth-first	I-Algorithm
search	I-Algorithm
avoids	O
the	O
latter	O
drawback	O
at	O
the	O
price	O
of	O
exploring	O
the	O
tree	B-Application
's	O
top	O
parts	O
over	O
and	O
over	O
again	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
both	O
depth-first	B-Algorithm
algorithms	O
get	O
along	O
without	O
extra	O
memory	O
.	O
</s>
<s>
Breadth-first	B-Algorithm
search	I-Algorithm
can	O
be	O
generalized	O
to	O
graphs	O
,	O
when	O
the	O
start	O
node	O
(	O
sometimes	O
referred	O
to	O
as	O
a	O
'	O
search	O
key	O
 '	O
)	O
is	O
explicitly	O
given	O
,	O
and	O
precautions	O
are	O
taken	O
against	O
following	O
a	O
vertex	O
twice	O
.	O
</s>
<s>
BFS	O
and	O
its	O
application	O
in	O
finding	O
connected	O
components	O
of	O
graphs	O
were	O
invented	O
in	O
1945	O
by	O
Konrad	O
Zuse	O
,	O
in	O
his	O
(	O
rejected	O
)	O
Ph.D.	O
thesis	O
on	O
the	O
Plankalkül	B-Language
programming	O
language	O
,	O
but	O
this	O
was	O
not	O
published	O
until	O
1972	O
.	O
</s>
<s>
It	O
was	O
reinvented	O
in	O
1959	O
by	O
Edward	O
F	O
.	O
Moore	O
,	O
who	O
used	O
it	O
to	O
find	O
the	O
shortest	O
path	O
out	O
of	O
a	O
maze	O
,	O
and	O
later	O
developed	O
by	O
C	O
.	O
Y	O
.	O
Lee	O
into	O
a	O
wire	B-Algorithm
routing	I-Algorithm
algorithm	O
(	O
published	O
1961	O
)	O
.	O
</s>
<s>
This	O
non-recursive	O
implementation	O
is	O
similar	O
to	O
the	O
non-recursive	O
implementation	O
of	O
depth-first	B-Algorithm
search	I-Algorithm
,	O
but	O
differs	O
from	O
it	O
in	O
two	O
ways	O
:	O
</s>
<s>
it	O
checks	O
whether	O
a	O
vertex	O
has	O
been	O
explored	O
before	O
enqueueing	O
the	O
vertex	O
rather	O
than	O
delaying	O
this	O
check	O
until	O
the	O
vertex	O
is	O
dequeued	O
from	O
the	O
queue	B-Application
.	O
</s>
<s>
If	O
is	O
a	O
tree	B-Application
,	O
replacing	O
the	O
queue	B-Application
of	O
this	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	B-Application
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>
The	O
Q	O
queue	B-Application
contains	O
the	O
frontier	O
along	O
which	O
the	O
algorithm	O
is	O
currently	O
searching	O
.	O
</s>
<s>
Breadth-first	B-Algorithm
search	I-Algorithm
produces	O
a	O
so-called	O
breadth	O
first	O
tree	B-Application
.	O
</s>
<s>
You	O
can	O
see	O
how	O
a	O
breadth	O
first	O
tree	B-Application
looks	O
in	O
the	O
following	O
example	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
example	O
of	O
the	O
breadth-first	B-Algorithm
tree	B-Application
obtained	O
by	O
running	O
a	O
BFS	O
on	O
German	O
cities	O
starting	O
from	O
Frankfurt	O
:	O
</s>
<s>
is	O
the	O
number	O
of	O
vertices	O
and	O
is	O
the	O
number	O
of	O
edges	O
in	O
the	O
graph	B-Application
.	O
</s>
<s>
Note	O
that	O
may	O
vary	O
between	O
and	O
,	O
depending	O
on	O
how	O
sparse	O
the	O
input	O
graph	B-Application
is	O
.	O
</s>
<s>
When	O
the	O
number	O
of	O
vertices	O
in	O
the	O
graph	B-Application
is	O
known	O
ahead	O
of	O
time	O
,	O
and	O
additional	O
data	O
structures	O
are	O
used	O
to	O
determine	O
which	O
vertices	O
have	O
already	O
been	O
added	O
to	O
the	O
queue	B-Application
,	O
the	O
space	O
complexity	O
can	O
be	O
expressed	O
as	O
,	O
where	O
is	O
the	O
number	O
of	O
vertices	O
.	O
</s>
<s>
required	O
for	O
the	O
graph	B-Application
itself	O
,	O
which	O
may	O
vary	O
depending	O
on	O
the	O
graph	B-Application
representation	I-Application
used	O
by	O
an	O
implementation	O
of	O
the	O
algorithm	O
.	O
</s>
<s>
When	O
working	O
with	O
graphs	O
that	O
are	O
too	O
large	O
to	O
store	O
explicitly	O
(	O
or	O
infinite	O
)	O
,	O
it	O
is	O
more	O
practical	O
to	O
describe	O
the	O
complexity	O
of	O
breadth-first	B-Algorithm
search	I-Algorithm
in	O
different	O
terms	O
:	O
to	O
find	O
the	O
nodes	O
that	O
are	O
at	O
distance	O
from	O
the	O
start	O
node	O
(	O
measured	O
in	O
number	O
of	O
edge	O
traversals	O
)	O
,	O
BFS	O
takes	O
time	O
and	O
memory	O
,	O
where	O
is	O
the	O
"	O
branching	B-Data_Structure
factor	I-Data_Structure
"	O
of	O
the	O
graph	B-Application
(	O
the	O
average	O
out-degree	O
)	O
.	O
</s>
<s>
In	O
the	O
analysis	O
of	O
algorithms	O
,	O
the	O
input	O
to	O
breadth-first	B-Algorithm
search	I-Algorithm
is	O
assumed	O
to	O
be	O
a	O
finite	O
graph	B-Application
,	O
represented	O
as	O
an	O
adjacency	B-Data_Structure
list	I-Data_Structure
,	O
adjacency	B-Algorithm
matrix	I-Algorithm
,	O
or	O
similar	O
representation	O
.	O
</s>
<s>
However	O
,	O
in	O
the	O
application	O
of	O
graph	B-Application
traversal	O
methods	O
in	O
artificial	B-Application
intelligence	I-Application
the	O
input	O
may	O
be	O
an	O
implicit	B-Data_Structure
representation	I-Data_Structure
of	O
an	O
infinite	O
graph	B-Application
.	O
</s>
<s>
Breadth-first	B-Algorithm
search	I-Algorithm
is	O
complete	O
,	O
but	O
depth-first	B-Algorithm
search	I-Algorithm
is	O
not	O
.	O
</s>
<s>
When	O
applied	O
to	O
infinite	O
graphs	O
represented	O
implicitly	O
,	O
breadth-first	B-Algorithm
search	I-Algorithm
will	O
eventually	O
find	O
the	O
goal	O
state	O
,	O
but	O
depth	B-Algorithm
first	I-Algorithm
search	I-Algorithm
may	O
get	O
lost	O
in	O
parts	O
of	O
the	O
graph	B-Application
that	O
have	O
no	O
goal	O
state	O
and	O
never	O
return	O
.	O
</s>
<s>
An	O
enumeration	O
of	O
the	O
vertices	O
of	O
a	O
graph	B-Application
is	O
said	O
to	O
be	O
a	O
BFS	O
ordering	O
if	O
it	O
is	O
the	O
possible	O
output	O
of	O
the	O
application	O
of	O
BFS	O
to	O
this	O
graph	B-Application
.	O
</s>
<s>
Let	O
be	O
a	O
graph	B-Application
with	O
vertices	O
.	O
</s>
<s>
Breadth-first	B-Algorithm
search	I-Algorithm
can	O
be	O
used	O
to	O
solve	O
many	O
problems	O
in	O
graph	B-Application
theory	O
,	O
for	O
example	O
:	O
</s>
<s>
Serialization/Deserialization	O
of	O
a	O
binary	O
tree	B-Application
vs	O
serialization	O
in	O
sorted	O
order	O
,	O
allows	O
the	O
tree	B-Application
to	O
be	O
re-constructed	O
in	O
an	O
efficient	O
manner	O
.	O
</s>
<s>
Construction	O
of	O
the	O
failure	O
function	O
of	O
the	O
Aho-Corasick	B-Algorithm
pattern	O
matcher	O
.	O
</s>
<s>
Testing	O
bipartiteness	O
of	O
a	O
graph	B-Application
.	O
</s>
<s>
Implementing	O
parallel	O
algorithms	O
for	O
computing	O
a	O
graph	B-Application
's	O
transitive	O
closure	O
.	O
</s>
