Topological ordering with DFS · Compiler
Topological ordering with DFS
Produce a topological ordering of a directed acyclic graph using DFS finishing times. Consider starting vertices in increasing ID order, skipping visited vertices. Mark each vertex on entry and recursively visit its unvisited outgoing neighbors in increasing ID order. Append the vertex when its call returns. Reverse the complete list after all searches finish.
- Input
- The first line contains n m. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v for an edge from u to v. Endpoints differ and each ordered pair occurs at most once. The input is guaranteed acyclic.
- Output
- Print the reversed finishing list as one line of n space-separated vertex IDs.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…