Algorithm. Kahn's Algorithm [00NI]
The resulting algorithm is known as Kahn’s Algorithm, and was first published by Arthur Kahn in 1962.
\mathsf {Kahn}(G):
T \gets empty list
Z \gets empty queue/stack/whatever (with \Theta (1) add/remove)
\mathit {in} \gets dictionary mapping all vertices to 0
\mathsf {for} each v \in V:
\mathsf {for} each u adjacent to v:
increment \mathit {in}[v]
\mathsf {for} each v \in V:
\mathsf {if} \mathit {in}[v] = 0:
add v to Z
\mathsf {while} Z is not empty:
v \gets Z.\mathit {dequeue}
append v to T
\mathsf {for} each u adjacent to v:
decrement \mathit {in}[u]
\mathsf {if} \mathit {in}[u] = 0:
add u to Z