...

/

Topological Sort in Graphs

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 has edges that are incoming or outgoing, meaning that they have a specific direction. An acyclic graph has no cycles, i.e., a node is not reachable from its ancestors. A topological sort takes a graph and finds the order of its nodes so it always starts from a node that has no incoming edges and then traverses the adjacent nodes. Note that the current node is before its adjacent. The illustration below will help explain this concept better.

Implementing the Code

The code below illustrates how to implement this process using recursion. First, let’s examine the code and then move on to its explanation.

Modify the edges by using the addEdge function and the number of vertices, nVertices ...