Topological Sort
Order tasks with dependencies! Visualize Kahn's Algorithm using In-Degrees. Crucial for build systems, course scheduling, and resolving dependencies.
Topological Sort (Kahn's Algorithm)
Linear ordering of vertices such that for every directed edge u -> v, vertex u comes before v. Uses In-Degree logic.
Calculate in-degrees for all nodes.
Kahn’s algorithm
Order the steps of a topological sort by in-degree.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
Topological Sort is only possible for which type of graph?
Write the code
Course Schedule II
The short version
Think of getting dressed. You must put on socks before shoes, and underwear before pants. You can't just put things on in random order; dependencies must be respected.
In more depth
Order of Operations
Topological Sorting is a linear ordering of vertices such that for every directed edge u -> v, vertex u comes before v in the ordering.
Prerequisite: The graph must be a Directed Acyclic Graph (DAG). If there is a cycle (A -> B -> A), no linear ordering is possible.
Kahn's Algorithm (BFS):
When to reach for this
- Scheduling tasks with dependencies
- Build systems (compiling dependencies first)
- Detecting cycles in a directed graph
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required