Hello! In our last lesson, we mastered using Depth-First Search to find connected components and, critically, to detect cycles in directed graphs. We established that a cycle represents a circular dependency, making a logical ordering impossible. This insight serves as the perfect launchpad for our current topic.
This lesson introduces Topological Sort, an algorithmic pattern for creating a linear ordering of items that have dependencies. You'll learn how to implement this pattern to solve scheduling and dependency graph problems, a common requirement in both technical interviews and real-world system architecture. We will explore two primary methods for this: a recursive approach based on DFS and an iterative one known as Kahn's algorithm. Your prior work on cycle detection will be essential, as a topological sort is only possible on a Directed Acyclic Graph (DAG).
1. What is a Topological Sort?
At its core, a topological sort of a DAG is a linear ordering of its vertices such that for every directed edge from vertex u to vertex v, u comes before v in the ordering. Think of it as a valid sequence for performing a set of tasks. For example, if you're building a software project, you must compile dependencies before you can compile the main application. If you're following a recipe, you must chop the vegetables before you can cook them.
A key property is that a topological ordering is not always unique. If two tasks don't depend on each other, their relative order can vary. This flexibility is important in systems that can perform tasks concurrently.
The following video provides an excellent introduction to the concept, explains why cycles make it impossible, and gives a high-level overview of the two main algorithms we'll be studying.
Topological Sort | Kahn vs DFS | Graphs | Data Structure
This video from the "Fit Coder" channel defines topological sort and discusses its many practical applications in software engineering.
Watch the introduction from the beginning to the explanation of why a topological sort is only possible for a DAG. Then, jump to the section on real-world applications. Pay attention to how it's used in OS job scheduling, Maven dependency resolution, and database table creation. These examples directly relate to the backend systems you've built and want to scale.
2. Approach 1: The DFS-Based Algorithm
Our first approach is a natural extension of the DFS traversal and cycle detection you learned in the previous lesson. The core idea is to perform a DFS on the graph. A node is considered "finished" only after we have recursively visited all its neighbors (its dependencies). Once a node is finished, we add it to our sorted list.
The trick is in when we add the node to the final list. A node is added only after the recursive calls for all its neighbors have returned. This ensures that all dependencies are processed before the node itself. By adding nodes to the front of a list (or, more commonly, to a stack), we build the topological order in reverse.
The following article gives a clear JavaScript implementation of this logic. As you read, notice how the stack.push(node) call happens after the recursive loop over the neighbors.
JavaScript for Implementing Topological Sort | Reintech media
This part of the article from Reintech media explains the DFS-based algorithm and shows a complete JavaScript implementation.
Read the sections titled Implementing Depth-First Search and Complete Topological Sort Implementation. The code is quite straightforward and should feel familiar given your JS background. The key takeaway is the post-order processing: a node is pushed to the stack only after its entire dependency chain has been explored.
Interview Focus: Course Schedule II
This DFS-based approach is the foundation for solving many classic interview problems. One of the most common is "Course Schedule II" (LeetCode 210), where you must return a valid order of courses to take given a list of prerequisites.
The following video from NeetCode provides a masterful walkthrough of this problem. It uses the exact three-state logic (unvisited, visiting, visited) that you practiced for cycle detection in our previous lesson, demonstrating how these patterns directly translate to interview success.
Course Schedule II - Topological Sort - Leetcode 210
This video solves the "Course Schedule II" problem using a DFS-based topological sort, explaining the data structures and logic step-by-step.
Watch from the main algorithm explanation to see how the output order is built by adding courses after their prerequisites are processed. Next, watch the Python code implementation. The use of a visit set and a cycle set is identical to tracking "visited" and "in recursion stack" states. Notice how a course is appended to the output list only after the DFS for its prerequisites has successfully completed. Finally, observe how the main loop in the concluding code iterates through all courses to handle disconnected components in the graph and how it returns an empty list if a cycle is detected.
3. Approach 2: Kahn's Algorithm (BFS-Based)
The second common method for topological sorting is Kahn's algorithm. It's an iterative approach that uses Breadth-First Search (BFS) and the concept of a node's in-degree—the number of incoming edges. This method often feels more intuitive for scheduling problems, as it processes nodes as soon as their dependencies are met.
The algorithm proceeds as follows:
- Compute In-Degrees: First, calculate the in-degree for every vertex in the graph.
- Initialize Queue: Find all vertices with an in-degree of 0 and add them to a queue. These are the starting points with no prerequisites.
- Process Queue: While the queue is not empty:
a. Dequeue a vertex and add it to your topological order list.
b. For each neighbor of the dequeued vertex, decrement its in-degree by 1 (since we have now "completed" its dependency).
c. If a neighbor's in-degree becomes 0, add it to the queue. - Check for Cycles: After the loop, if the number of vertices in your topological order list is less than the total number of vertices in the graph, the graph must contain a cycle.
This image provides a clear, step-by-step visualization of Kahn's algorithm in action.

For a code implementation, we can refer back to the Reintech article, which also covers Kahn's algorithm.
JavaScript for Implementing Topological Sort | Reintech media
This section provides a JavaScript implementation of Kahn's algorithm, using a queue and an in-degree array.
Read the section on Kahn's Algorithm. Compare this iterative, queue-based logic to the recursive, stack-based logic of the DFS approach. Note the elegant way it detects cycles by checking the length of the final result.

4. Application: A Concurrent Task Scheduler in Go
Moving from abstract algorithms to practical systems, topological sort is the engine behind many task scheduling and workflow systems. Given your proficiency in Go and interest in scalable systems, this article on building a dependency-aware task scheduler is highly relevant. It shows how these concepts are applied in a real-world library designed for concurrent execution.
The library described uses an approach very similar to Kahn's algorithm to manage a worker pool.
Using Go To Schedule And Run Tasks with Dependencies
This article by Alain Drolet details a Go library for running tasks with dependencies, using a topological sort to drive a concurrent execution engine.
Please read the following sections: Data Model: Understand how tasks and dependencies are modeled as a DAG. Topological Sort: See how topological sort is defined as the core requirement for creating a valid execution sequence. Algorithm High-Level View: This is the most crucial part. The description of initializing a "Ready nodes" list and processing them from a queue is a direct application of Kahn's algorithm's principles to drive a concurrent worker pool system.
This Go example elevates topological sort from a simple array-ordering puzzle to a core component of a resilient, concurrent system—a key step in your journey toward designing scalable backend services.
Conclusion
In this lesson, we explored the essential pattern of Topological Sort for ordering tasks within a dependency graph. You now have two powerful techniques in your algorithmic toolbox to solve this class of problems.
Key Takeaways:
- Topological Sort provides a linear ordering of nodes in a Directed Acyclic Graph (DAG) based on their dependencies.
- It is fundamental to solving problems like task scheduling, build system ordering, and dependency resolution.
- The DFS-based approach builds the order by adding nodes to a stack in post-order, ensuring dependencies are processed first. This method is closely related to cycle detection.
- Kahn's Algorithm is an iterative, BFS-based approach that uses a queue and node in-degrees. It processes nodes as soon as their dependencies are met, making it a natural fit for concurrent schedulers.
- A topological sort is impossible if the graph contains a cycle. Both algorithms provide a way to detect this condition.
In our next lesson, we will shift gears to another powerful problem-solving paradigm: Dynamic Programming. You will learn to identify problems that can be broken down into smaller, overlapping subproblems and solve them efficiently using techniques like memoization and tabulation.