Topological Sorting

 



Topological sorting of a graph is a linear ordering of its vertices so that for each directed edge (u, v) in the ordering, vertex u comes before v.


  1. Topological sort is an ordering of vertices in a directed acyclic graph [DAG] in which each node comes before all nodes to which it has outgoing edges.
  2. Indegree is computed for all vertices, starting with the vertices which are having indegree 0. 
  3. Queue can be used to keep track of vertices with indegree zero. While the queue is not empty, a vertex is removed, and all edges adjacent to it have their indegrees decremented.
  4. Topological sorting for a graph is not possible if graph is not DAG.
  5. If a Hamiltonian path exists, the topological sort order is unique. If a topological sort does not form a Hamiltonian path, DAG can have two or more topological orderings.

Applications


Comments