Solution: Topological Sorting of a Graph
Learn how to implement topological sorting in a graph using two methods: a modified depth-first search with a stack and Kahn's algorithm based on vertex indegree. Understand the pseudocode and analyze the time complexity O(V+E) while handling directed acyclic graphs and cycle detection.
We'll cover the following...
We'll cover the following...
Solution #1: Using Graph Traversal
We can modify graph traversal algorithms to find Topological Sorting of a graph.
While traversing a graph, we start from a vertex. First, print it and then recursively call the utility function for its adjacent vertices.
In topological sorting, we use a temporary stack. We do not print the vertex immediately; before that, we recursively call topological sorting for all its adjacent vertices, then push it to a stack. Finally, print ...