Search⌘ K

Solution Review 3: Topological Sorting of a Graph

Explore how to solve topological sorting problems using recursion in graphs. Understand traversal, marking visited nodes, and ordering vertices with a stack to master this common interview algorithm.

We'll cover the following...

Solution: Using Recursion

Let’s have a look at the algorithm to solve this problem:

function helperFunction(currentNode) {
  // mark currentNode visited
  for (each vertex v that has an edge from currentNode to v) {
    helperFunction(v);
  }
  // push currentNode to head
...