AlgoFrames

From finishing order to topological order

Standard · Predict the result · about 4 min

DFS ran on this graph from 0. The labels show the order the nodes finished: 4 finished first (#1) and 0 last (#5). Write the topological order DFS produces, as node numbers separated by spaces.

Example 1

Input:

{"edges":[[0,1],[0,2],[1,3],[2,3],[3,4]]}

Output:

0 2 1 3 4

Constraints

  • Answer with node numbers separated by spaces.

Learn the idea first: Topological sort: Kahn's algorithm and DFS finishing order, a visual lesson.

More Topological sort: Kahn's algorithm and DFS finishing order problems