<s>
Tarjan	B-Algorithm
's	I-Algorithm
strongly	I-Algorithm
connected	I-Algorithm
components	I-Algorithm
algorithm	I-Algorithm
is	O
an	O
algorithm	O
in	O
graph	B-Application
theory	O
for	O
finding	O
the	O
strongly	O
connected	O
components	O
(	O
SCCs	O
)	O
of	O
a	O
directed	O
graph	B-Application
.	O
</s>
<s>
It	O
runs	O
in	O
linear	O
time	O
,	O
matching	O
the	O
time	O
bound	O
for	O
alternative	O
methods	O
including	O
Kosaraju	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
and	O
the	O
path-based	B-Algorithm
strong	I-Algorithm
component	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
takes	O
a	O
directed	O
graph	B-Application
as	O
input	O
,	O
and	O
produces	O
a	O
partition	O
of	O
the	O
graph	B-Application
's	O
vertices	O
into	O
the	O
graph	B-Application
's	O
strongly	O
connected	O
components	O
.	O
</s>
<s>
Each	O
vertex	O
of	O
the	O
graph	B-Application
appears	O
in	O
exactly	O
one	O
of	O
the	O
strongly	O
connected	O
components	O
.	O
</s>
<s>
Any	O
vertex	O
that	O
is	O
not	O
on	O
a	O
directed	O
cycle	O
forms	O
a	O
strongly	O
connected	O
component	O
all	O
by	O
itself	O
:	O
for	O
example	O
,	O
a	O
vertex	O
whose	O
in-degree	O
or	O
out-degree	O
is	O
0	O
,	O
or	O
any	O
vertex	O
of	O
an	O
acyclic	O
graph	B-Application
.	O
</s>
<s>
As	O
usual	O
with	O
depth-first	O
search	O
,	O
the	O
search	O
visits	O
every	O
node	O
of	O
the	O
graph	B-Application
exactly	O
once	O
,	O
declining	O
to	O
revisit	O
any	O
node	O
that	O
has	O
already	O
been	O
visited	O
.	O
</s>
<s>
Thus	O
,	O
the	O
collection	O
of	O
search	O
trees	O
is	O
a	O
spanning	O
forest	O
of	O
the	O
graph	B-Application
.	O
</s>
<s>
Nodes	O
are	O
placed	O
on	O
a	O
stack	B-Application
in	O
the	O
order	O
in	O
which	O
they	O
are	O
visited	O
.	O
</s>
<s>
When	O
the	O
depth-first	O
search	O
recursively	O
visits	O
a	O
node	O
v	O
and	O
its	O
descendants	O
,	O
those	O
nodes	O
are	O
not	O
all	O
necessarily	O
popped	O
from	O
the	O
stack	B-Application
when	O
this	O
recursive	O
call	O
returns	O
.	O
</s>
<s>
The	O
crucial	O
invariant	B-Application
property	I-Application
is	O
that	O
a	O
node	O
remains	O
on	O
the	O
stack	B-Application
after	O
it	O
has	O
been	O
visited	O
if	O
and	O
only	O
if	O
there	O
exists	O
a	O
path	O
in	O
the	O
input	O
graph	B-Application
from	O
it	O
to	O
some	O
node	O
earlier	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
In	O
other	O
words	O
,	O
it	O
means	O
that	O
in	O
the	O
DFS	O
a	O
node	O
would	O
be	O
only	O
removed	O
from	O
the	O
stack	B-Application
after	O
all	O
its	O
connected	O
paths	O
have	O
been	O
traversed	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
the	O
call	O
that	O
visits	O
v	O
and	O
its	O
descendants	O
,	O
we	O
know	O
whether	O
v	O
itself	O
has	O
a	O
path	O
to	O
any	O
node	O
earlier	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
If	O
so	O
,	O
the	O
call	O
returns	O
,	O
leaving	O
v	O
on	O
the	O
stack	B-Application
to	O
preserve	O
the	O
invariant	B-Application
.	O
</s>
<s>
If	O
not	O
,	O
then	O
v	O
must	O
be	O
the	O
root	O
of	O
its	O
strongly	O
connected	O
component	O
,	O
which	O
consists	O
of	O
v	O
together	O
with	O
any	O
nodes	O
later	O
on	O
the	O
stack	B-Application
than	O
v	O
(	O
such	O
nodes	O
all	O
have	O
paths	O
back	O
to	O
v	O
but	O
not	O
to	O
any	O
earlier	O
node	O
,	O
because	O
if	O
they	O
had	O
paths	O
to	O
earlier	O
nodes	O
then	O
v	O
would	O
also	O
have	O
paths	O
to	O
earlier	O
nodes	O
which	O
is	O
false	O
)	O
.	O
</s>
<s>
The	O
connected	O
component	O
rooted	O
at	O
v	O
is	O
then	O
popped	O
from	O
the	O
stack	B-Application
and	O
returned	O
,	O
again	O
preserving	O
the	O
invariant	B-Application
.	O
</s>
<s>
It	O
also	O
maintains	O
a	O
value	O
v.lowlink	O
that	O
represents	O
the	O
smallest	O
index	O
of	O
any	O
node	O
on	O
the	O
stack	B-Application
known	O
to	O
be	O
reachable	O
from	O
v	O
through	O
v	O
's	O
DFS	O
subtree	O
,	O
including	O
v	O
itself	O
.	O
</s>
<s>
Therefore	O
v	O
must	O
be	O
left	O
on	O
the	O
stack	B-Application
if	O
v.lowlink	O
<	O
v.index	O
,	O
whereas	O
v	O
must	O
be	O
removed	O
as	O
the	O
root	O
of	O
a	O
strongly	O
connected	O
component	O
if	O
v.lowlink	O
==	O
v.index	O
.	O
</s>
<s>
Note	O
that	O
the	O
lowlink	O
is	O
different	O
from	O
the	O
lowpoint	O
,	O
which	O
is	O
the	O
smallest	O
index	O
reachable	O
from	O
v	O
through	O
any	O
part	O
of	O
the	O
graph	B-Application
.	O
</s>
<s>
S	O
is	O
the	O
node	O
stack	B-Application
,	O
which	O
starts	O
out	O
empty	O
and	O
stores	O
the	O
history	O
of	O
nodes	O
explored	O
but	O
not	O
yet	O
committed	O
to	O
a	O
strongly	O
connected	O
component	O
.	O
</s>
<s>
Note	O
that	O
this	O
is	O
not	O
the	O
normal	O
depth-first	O
search	O
stack	B-Application
,	O
as	O
nodes	O
are	O
not	O
popped	O
as	O
the	O
search	O
returns	O
up	O
the	O
tree	O
;	O
they	O
are	O
only	O
popped	O
when	O
an	O
entire	O
strongly	O
connected	O
component	O
has	O
been	O
found	O
.	O
</s>
<s>
The	O
function	O
strongconnect	O
performs	O
a	O
single	O
depth-first	O
search	O
of	O
the	O
graph	B-Application
,	O
finding	O
all	O
successors	O
from	O
the	O
node	O
v	O
,	O
and	O
reporting	O
all	O
strongly	O
connected	O
components	O
of	O
that	O
subgraph	O
.	O
</s>
<s>
When	O
each	O
node	O
finishes	O
recursing	O
,	O
if	O
its	O
lowlink	O
is	O
still	O
set	O
to	O
its	O
index	O
,	O
then	O
it	O
is	O
the	O
root	O
node	O
of	O
a	O
strongly	O
connected	O
component	O
,	O
formed	O
by	O
all	O
of	O
the	O
nodes	O
above	O
it	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
algorithm	O
pops	O
the	O
stack	B-Application
up	O
to	O
and	O
including	O
the	O
current	O
node	O
,	O
and	O
presents	O
all	O
of	O
these	O
nodes	O
as	O
a	O
strongly	O
connected	O
component	O
.	O
</s>
<s>
Note	O
that	O
v.lowlink	O
:=	O
min( 	O
v.lowlink	O
,	O
w.index	O
)	O
is	O
the	O
correct	O
way	O
to	O
update	O
v.lowlink	O
if	O
w	O
is	O
on	O
stack	B-Application
.	O
</s>
<s>
Because	O
w	O
is	O
on	O
the	O
stack	B-Application
already	O
,	O
(	O
v	O
,	O
w	O
)	O
is	O
a	O
back-edge	O
in	O
the	O
DFS	O
tree	O
and	O
therefore	O
w	O
is	O
not	O
in	O
the	O
subtree	O
of	O
v	O
.	O
Because	O
v.lowlink	O
takes	O
into	O
account	O
nodes	O
reachable	O
only	O
through	O
the	O
nodes	O
in	O
the	O
subtree	O
of	O
v	O
we	O
must	O
stop	O
at	O
w	O
and	O
use	O
w.index	O
instead	O
of	O
w.lowlink	O
.	O
</s>
<s>
In	O
order	O
to	O
achieve	O
this	O
complexity	O
,	O
the	O
test	O
for	O
whether	O
w	O
is	O
on	O
the	O
stack	B-Application
should	O
be	O
done	O
in	O
constant	O
time	O
.	O
</s>
<s>
This	O
may	O
be	O
done	O
,	O
for	O
example	O
,	O
by	O
storing	O
a	O
flag	O
on	O
each	O
node	O
that	O
indicates	O
whether	O
it	O
is	O
on	O
the	O
stack	B-Application
,	O
and	O
performing	O
this	O
test	O
by	O
examining	O
the	O
flag.Space	O
Complexity	O
:	O
The	O
Tarjan	O
procedure	O
requires	O
two	O
words	O
of	O
supplementary	O
data	O
per	O
vertex	O
for	O
the	O
index	O
and	O
lowlink	O
fields	O
,	O
along	O
with	O
one	O
bit	O
for	O
onStack	O
and	O
another	O
for	O
determining	O
when	O
index	O
is	O
undefined	O
.	O
</s>
<s>
In	O
addition	O
,	O
one	O
word	O
is	O
required	O
on	O
each	O
stack	B-Application
frame	O
to	O
hold	O
v	O
and	O
another	O
for	O
the	O
current	O
position	O
in	O
the	O
edge	O
list	O
.	O
</s>
<s>
Finally	O
,	O
the	O
worst-case	O
size	O
of	O
the	O
stack	B-Application
S	O
must	O
be	O
(	O
i.e.	O
</s>
<s>
when	O
the	O
graph	B-Application
is	O
one	O
giant	O
component	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
order	O
in	O
which	O
the	O
strongly	O
connected	O
components	O
are	O
identified	O
constitutes	O
a	O
reverse	O
topological	B-Algorithm
sort	I-Algorithm
of	O
the	O
DAG	O
formed	O
by	O
the	O
strongly	O
connected	O
components	O
.	O
</s>
<s>
Donald	O
Knuth	O
described	O
Tarjan	B-Algorithm
's	I-Algorithm
SCC	I-Algorithm
algorithm	I-Algorithm
as	O
one	O
of	O
his	O
favorite	O
implementations	O
in	O
the	O
book	O
The	O
Stanford	O
GraphBase''	O
.	O
</s>
