For a directed acyclic graph(DAG) G = (V, E)
, topological sort is a sort for all of node in G. The order should be:
- if Gragh G contains (u,v) edge, then node u in topological sort should be at the former of node v.
Pseudocode
// TOPOLOGICAL-SORT(G)
call DFS(G) to compute finishing times v.f for each vertex v
as each vertex is finished, insert it onto the front of a linked list
return the linked list of vertices
We could implement it in O(V+E)
<- which is the DFS time complexity.
Comments
Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.