Topological Sort
00:00
AmazonGoogle
Return topological ordering of vertices in a DAG (Directed Acyclic Graph). Vertex u comes before v if there is edge u→v.
Examples
Input → 6 nodes, edges: 5
Output → 2,5→0,4→0,4→1,2→3,3→1 → [5,4,2,3,1,0]
Note: one valid order
Input → Linear chain 0
Output → 1→2→3 → [0,1,2,3]
Input → Graph with cycle
Output → impossible
Note: return [] or detect