Topological sort is a method of labeling and sorting a graph in a partial order.

Steps usually involve:

  • labeling each other vertex’s in-degree (#of inbound edges)
  • Initializing a queue to contain and maintain all vertices with in-degree zero
  • While there are vertices remaining in that queue
    • Pick a vertex v from the queue and output it
    • Reduce the in-degree of all vertices adjacent to v
    • Put any of these with updated zero in-degree vertices on the queue
    • Remove v from the queue