Topological Sort in Graphs

This lesson will teach you how to write a recursive code for topological sorting in graphs.

What is Topological Sort?

Topological Sort is a way to order a directed acyclic graph. A directed graph is one that has arrows that are either incoming or outgoing meaning they have direction. An acyclic graph is one that has no cycles, i.e., a node is not reachable from its own ancestors. A topological sort takes such a graph and finds the order of the nodes so that it always starts from a node with no incoming edges and then traverses its adjacent nodes. Note that this time, the current node is printed first and then its neighbors are printed. The illustration below will help you understand this concept better.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.