7. Topological Sort

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge (u v) , vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.

For the sample graph shown above, one of the possible ordering of courses is: C6 βž” C4 βž” C1 βž” C5 βž” C2 βž” C3 and another possible ordering of subjects is C6 βž” C4 βž” C5 βž” C1 βž” C2 βž” C3.

Using DFS

Using Node Indegree

Last updated

Was this helpful?