TOPLOGICAL-SORT(G)
- call DFS(G)
- As each vertex finishes, insert it into a list L
- Return L
KHAN-TOPO(G)
- L ← {} // an empty list
- // make S the set of all vertexes with no incoming edges
- S ← {v | v ∈ G.V and there is no e = (x,v) ∈ G.E}
- While S is not empty do
- remove a node n from S
- add n to L
- for v ∈ Adj(n)
- remove (v,n) from G
- if v has no other incoming edges
- insert m into S
- If G.V ≠ {}
- return error (the graph has a cycle)
- else
- return l