<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
topological	B-Algorithm
sort	I-Algorithm
or	O
topological	B-Algorithm
ordering	I-Algorithm
of	O
a	O
directed	O
graph	O
is	O
a	O
linear	O
ordering	O
of	O
its	O
vertices	O
such	O
that	O
for	O
every	O
directed	O
edge	O
uv	O
from	O
vertex	O
u	O
to	O
vertex	O
v	O
,	O
u	O
comes	O
before	O
v	O
in	O
the	O
ordering	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
vertices	O
of	O
the	O
graph	O
may	O
represent	O
tasks	O
to	O
be	O
performed	O
,	O
and	O
the	O
edges	O
may	O
represent	O
constraints	O
that	O
one	O
task	O
must	O
be	O
performed	O
before	O
another	O
;	O
in	O
this	O
application	O
,	O
a	O
topological	B-Algorithm
ordering	I-Algorithm
is	O
just	O
a	O
valid	O
sequence	O
for	O
the	O
tasks	O
.	O
</s>
<s>
Precisely	O
,	O
a	O
topological	B-Algorithm
sort	I-Algorithm
is	O
a	O
graph	O
traversal	O
in	O
which	O
each	O
node	O
v	O
is	O
visited	O
only	O
after	O
all	O
its	O
dependencies	O
are	O
visited	O
.	O
</s>
<s>
A	O
topological	B-Algorithm
ordering	I-Algorithm
is	O
possible	O
if	O
and	O
only	O
if	O
the	O
graph	O
has	O
no	O
directed	O
cycles	O
,	O
that	O
is	O
,	O
if	O
it	O
is	O
a	O
directed	O
acyclic	O
graph	O
(	O
DAG	O
)	O
.	O
</s>
<s>
Any	O
DAG	O
has	O
at	O
least	O
one	O
topological	B-Algorithm
ordering	I-Algorithm
,	O
and	O
algorithms	O
are	O
known	O
for	O
constructing	O
a	O
topological	B-Algorithm
ordering	I-Algorithm
of	O
any	O
DAG	O
in	O
linear	O
time	O
.	O
</s>
<s>
Topological	B-Algorithm
sorting	I-Algorithm
has	O
many	O
applications	O
especially	O
in	O
ranking	O
problems	O
such	O
as	O
feedback	O
arc	O
set	O
.	O
</s>
<s>
Topological	B-Algorithm
sorting	I-Algorithm
is	O
possible	O
even	O
when	O
the	O
DAG	O
has	O
disconnected	O
components	O
.	O
</s>
<s>
The	O
canonical	O
application	O
of	O
topological	B-Algorithm
sorting	I-Algorithm
is	O
in	O
scheduling	B-Algorithm
a	O
sequence	O
of	O
jobs	O
or	O
tasks	O
based	O
on	O
their	O
dependencies	O
.	O
</s>
<s>
Then	O
,	O
a	O
topological	B-Algorithm
sort	I-Algorithm
gives	O
an	O
order	O
in	O
which	O
to	O
perform	O
the	O
jobs	O
.	O
</s>
<s>
A	O
closely	O
related	O
application	O
of	O
topological	B-Algorithm
sorting	I-Algorithm
algorithms	O
was	O
first	O
studied	O
in	O
the	O
early	O
1960s	O
in	O
the	O
context	O
of	O
the	O
PERT	O
technique	O
for	O
scheduling	B-Algorithm
in	O
project	O
management	O
.	O
</s>
<s>
Topological	B-Algorithm
sorting	I-Algorithm
forms	O
the	O
basis	O
of	O
linear-time	O
algorithms	O
for	O
finding	O
the	O
critical	O
path	O
of	O
the	O
project	O
,	O
a	O
sequence	O
of	O
milestones	O
and	O
tasks	O
that	O
controls	O
the	O
length	O
of	O
the	O
overall	O
project	O
schedule	O
.	O
</s>
<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
applications	O
of	O
this	O
type	O
arise	O
in	O
instruction	O
scheduling	B-Algorithm
,	O
ordering	O
of	O
formula	O
cell	O
evaluation	O
when	O
recomputing	O
formula	O
values	O
in	O
spreadsheets	B-Application
,	O
logic	O
synthesis	O
,	O
determining	O
the	O
order	O
of	O
compilation	O
tasks	O
to	O
perform	O
in	O
makefiles	O
,	O
data	B-Application
serialization	I-Application
,	O
and	O
resolving	O
symbol	O
dependencies	O
in	O
linkers	B-Application
.	O
</s>
<s>
upright	O
=	O
0.8	O
|left	O
The	O
graph	O
shown	O
to	O
the	O
left	O
has	O
many	O
valid	O
topological	B-Algorithm
sorts	I-Algorithm
,	O
including	O
:	O
</s>
<s>
The	O
usual	O
algorithms	O
for	O
topological	B-Algorithm
sorting	I-Algorithm
have	O
running	O
time	O
linear	O
in	O
the	O
number	O
of	O
nodes	O
plus	O
the	O
number	O
of	O
edges	O
,	O
asymptotically	O
,	O
</s>
<s>
One	O
of	O
these	O
algorithms	O
,	O
first	O
described	O
by	O
,	O
works	O
by	O
choosing	O
vertices	O
in	O
the	O
same	O
order	O
as	O
the	O
eventual	O
topological	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Otherwise	O
,	O
the	O
graph	O
must	O
have	O
at	O
least	O
one	O
cycle	O
and	O
therefore	O
a	O
topological	B-Algorithm
sort	I-Algorithm
is	O
impossible	O
.	O
</s>
<s>
A	O
variation	O
of	O
Kahn	O
's	O
algorithm	O
that	O
breaks	O
ties	O
lexicographically	O
forms	O
a	O
key	O
component	O
of	O
the	O
Coffman	B-Operating_System
–	I-Operating_System
Graham	I-Operating_System
algorithm	I-Operating_System
for	O
parallel	O
scheduling	B-Algorithm
and	O
layered	O
graph	O
drawing	O
.	O
</s>
<s>
An	O
alternative	O
algorithm	O
for	O
topological	B-Algorithm
sorting	I-Algorithm
is	O
based	O
on	O
depth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
loops	O
through	O
each	O
node	O
of	O
the	O
graph	O
,	O
in	O
an	O
arbitrary	O
order	O
,	O
initiating	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
that	O
terminates	O
when	O
it	O
hits	O
any	O
node	O
that	O
has	O
already	O
been	O
visited	O
since	O
the	O
beginning	O
of	O
the	O
topological	B-Algorithm
sort	I-Algorithm
or	O
the	O
node	O
has	O
no	O
outgoing	O
edges	O
(	O
i.e.	O
</s>
<s>
This	O
depth-first-search-based	O
algorithm	O
is	O
the	O
one	O
described	O
by	O
;	O
it	O
seems	O
to	O
have	O
been	O
first	O
described	O
in	O
print	O
by	O
Tarjan	O
in	O
1976	O
.	O
</s>
<s>
On	O
a	O
parallel	B-Operating_System
random-access	I-Operating_System
machine	I-Operating_System
,	O
a	O
topological	B-Algorithm
ordering	I-Algorithm
can	O
be	O
constructed	O
in	O
O(log2 n )	O
time	O
using	O
a	O
polynomial	O
number	O
of	O
processors	O
,	O
putting	O
the	O
problem	O
into	O
the	O
complexity	O
class	O
NC2	O
.	O
</s>
<s>
One	O
method	O
for	O
doing	O
this	O
is	O
to	O
repeatedly	O
square	O
the	O
adjacency	B-Algorithm
matrix	I-Algorithm
of	O
the	O
given	O
graph	O
,	O
logarithmically	O
many	O
times	O
,	O
using	O
min-plus	O
matrix	O
multiplication	O
with	O
maximization	O
in	O
place	O
of	O
minimization	O
.	O
</s>
<s>
Sorting	O
the	O
vertices	O
by	O
the	O
lengths	O
of	O
their	O
longest	O
incoming	O
paths	O
produces	O
a	O
topological	B-Algorithm
ordering	I-Algorithm
.	O
</s>
<s>
An	O
algorithm	O
for	O
parallel	O
topological	B-Algorithm
sorting	I-Algorithm
on	O
distributed	B-Operating_System
memory	I-Operating_System
machines	O
parallelizes	O
the	O
algorithm	O
of	O
Kahn	O
for	O
a	O
DAG	O
.	O
</s>
<s>
On	O
a	O
high	O
level	O
,	O
the	O
algorithm	O
of	O
Kahn	O
repeatedly	O
removes	O
the	O
vertices	O
of	O
indegree	O
0	O
and	O
adds	O
them	O
to	O
the	O
topological	B-Algorithm
sorting	I-Algorithm
in	O
the	O
order	O
in	O
which	O
they	O
were	O
removed	O
.	O
</s>
<s>
they	O
are	O
not	O
adjacent	O
,	O
they	O
can	O
be	O
given	O
in	O
an	O
arbitrary	O
order	O
for	O
a	O
valid	O
topological	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
To	O
assign	O
a	O
global	O
index	O
to	O
each	O
vertex	O
,	O
a	O
prefix	B-Application
sum	I-Application
is	O
calculated	O
over	O
the	O
sizes	O
of	O
.	O
</s>
<s>
So	O
each	O
step	O
,	O
there	O
are	O
vertices	O
added	O
to	O
the	O
topological	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
Below	O
is	O
a	O
high	O
level	O
,	O
single	B-Operating_System
program	I-Operating_System
,	I-Operating_System
multiple	I-Operating_System
data	I-Operating_System
pseudo	O
code	O
overview	O
of	O
this	O
algorithm	O
.	O
</s>
<s>
Note	O
that	O
the	O
prefix	B-Application
sum	I-Application
for	O
the	O
local	O
offsets	O
can	O
be	O
efficiently	O
calculated	O
in	O
parallel	O
.	O
</s>
<s>
As	O
for	O
runtime	O
,	O
on	O
a	O
CRCW-PRAM	B-Operating_System
model	O
that	O
allows	O
fetch-and-decrement	O
in	O
constant	O
time	O
,	O
this	O
algorithm	O
runs	O
in	O
,	O
where	O
is	O
again	O
the	O
longest	O
path	O
in	O
and	O
the	O
maximum	O
degree	O
.	O
</s>
<s>
The	O
topological	B-Algorithm
ordering	I-Algorithm
can	O
also	O
be	O
used	O
to	O
quickly	O
compute	O
shortest	O
paths	O
through	O
a	O
weighted	O
directed	O
acyclic	O
graph	O
.	O
</s>
<s>
If	O
a	O
topological	B-Algorithm
sort	I-Algorithm
has	O
the	O
property	O
that	O
all	O
pairs	O
of	O
consecutive	O
vertices	O
in	O
the	O
sorted	O
order	O
are	O
connected	O
by	O
edges	O
,	O
then	O
these	O
edges	O
form	O
a	O
directed	O
Hamiltonian	O
path	O
in	O
the	O
DAG	O
.	O
</s>
<s>
If	O
a	O
Hamiltonian	O
path	O
exists	O
,	O
the	O
topological	B-Algorithm
sort	I-Algorithm
order	O
is	O
unique	O
;	O
no	O
other	O
order	O
respects	O
the	O
edges	O
of	O
the	O
path	O
.	O
</s>
<s>
Conversely	O
,	O
if	O
a	O
topological	B-Algorithm
sort	I-Algorithm
does	O
not	O
form	O
a	O
Hamiltonian	O
path	O
,	O
the	O
DAG	O
will	O
have	O
two	O
or	O
more	O
valid	O
topological	B-Algorithm
orderings	I-Algorithm
,	O
for	O
in	O
this	O
case	O
it	O
is	O
always	O
possible	O
to	O
form	O
a	O
second	O
valid	O
ordering	O
by	O
swapping	O
two	O
consecutive	O
vertices	O
that	O
are	O
not	O
connected	O
by	O
an	O
edge	O
to	O
each	O
other	O
.	O
</s>
<s>
Topological	B-Algorithm
orderings	I-Algorithm
are	O
also	O
closely	O
related	O
to	O
the	O
concept	O
of	O
a	O
linear	O
extension	O
of	O
a	O
partial	O
order	O
in	O
mathematics	O
.	O
</s>
<s>
Total	O
orders	O
are	O
familiar	O
in	O
computer	B-General_Concept
science	I-General_Concept
as	O
the	O
comparison	O
operators	O
needed	O
to	O
perform	O
comparison	B-Algorithm
sorting	I-Algorithm
algorithms	O
.	O
</s>
<s>
For	O
finite	O
sets	O
,	O
total	O
orders	O
may	O
be	O
identified	O
with	O
linear	O
sequences	O
of	O
objects	O
,	O
where	O
the	O
"	O
≤	O
"	O
relation	O
is	O
true	O
whenever	O
the	O
first	O
object	O
precedes	O
the	O
second	O
object	O
in	O
the	O
order	O
;	O
a	O
comparison	B-Algorithm
sorting	I-Algorithm
algorithm	O
may	O
be	O
used	O
to	O
convert	O
a	O
total	O
order	O
into	O
a	O
sequence	O
in	O
this	O
way	O
.	O
</s>
<s>
With	O
these	O
definitions	O
,	O
a	O
topological	B-Algorithm
ordering	I-Algorithm
of	O
the	O
DAG	O
is	O
the	O
same	O
thing	O
as	O
a	O
linear	O
extension	O
of	O
this	O
partial	O
order	O
.	O
</s>
<s>
By	O
using	O
these	O
constructions	O
,	O
one	O
can	O
use	O
topological	B-Algorithm
ordering	I-Algorithm
algorithms	O
to	O
find	O
linear	O
extensions	O
of	O
partial	O
orders	O
.	O
</s>
<s>
By	O
definition	O
,	O
the	O
solution	O
of	O
a	O
scheduling	B-Algorithm
problem	O
that	O
includes	O
a	O
precedence	O
graph	O
is	O
a	O
valid	O
solution	O
to	O
topological	B-Algorithm
sort	I-Algorithm
(	O
irrespective	O
of	O
the	O
number	O
of	O
machines	O
)	O
,	O
however	O
,	O
topological	B-Algorithm
sort	I-Algorithm
in	O
itself	O
is	O
not	O
enough	O
to	O
optimally	O
solve	O
a	O
scheduling	B-Algorithm
optimisation	O
problem	O
.	O
</s>
<s>
Hu	O
's	O
algorithm	O
is	O
a	O
popular	O
method	O
used	O
to	O
solve	O
scheduling	B-Algorithm
problems	O
that	O
require	O
a	O
precedence	O
graph	O
and	O
involve	O
processing	O
times	O
(	O
where	O
the	O
goal	O
is	O
to	O
minimise	O
the	O
largest	O
completion	O
time	O
amongst	O
all	O
the	O
jobs	O
)	O
.	O
</s>
<s>
Like	O
topological	B-Algorithm
sort	I-Algorithm
,	O
Hu	O
's	O
algorithm	O
is	O
not	O
unique	O
and	O
can	O
be	O
solved	O
using	O
DFS	O
(	O
by	O
finding	O
the	O
largest	O
path	O
length	O
and	O
then	O
assigning	O
the	O
jobs	O
)	O
.	O
</s>
