How to use the Topological Sort Visualizer
- 1. Set it upEnter one edge per line, like C->DSA (C must come before DSA).
- 2. RunPress Sort.
- 3. ReplayStep through: each node shows its remaining in-degree, then its position once taken.
Worked example: a course plan
The sample says Maths and C come before DSA, C before OOP, DSA before Algorithms and DBMS, OOP before Java, and Java before Spring. Kahn's algorithm starts with the nodes that have no prerequisites, C and Maths, and produces C → Maths → OOP → DSA → Java → Algorithms → DBMS → Spring. Taking C frees OOP; DSA is freed only after both C and Maths are taken. When several nodes are free at once, the tool takes them alphabetically, so other valid orders exist.
How Kahn's algorithm works
Count each node's incoming edges (its in-degree). Put every node with in-degree 0 in a queue. Repeatedly take a node from the queue, add it to the order, and subtract one from the in-degree of each node it points to; any that reach 0 join the queue. It runs in O(V + E). If the queue empties while some nodes remain, every remaining node still has a prerequisite among the others, so there's a cycle and no valid order. Try adding Spring->C to the sample to see it.
Where it's used
- Course Schedule and Alien Dictionary in coding interviews.
- Build tools ordering compile steps, and package managers installing dependencies.
- Spreadsheet recalculation, task scheduling and deciding the order to run database migrations.
Frequently asked questions
Can I use DFS instead?
Yes. Reverse post-order of a DFS also gives a topological order. This tool uses Kahn's method because the queue makes each step visible.
Do node names need to be single letters?
No. Use any names of letters, digits and underscores.
What's the node limit?
14 nodes, so the drawing stays readable.
Is the topological sort visualizer free?
Yes. You can use the topological sort visualizer without signing up. The first time you run it, we ask once for your name and WhatsApp number; after that the topological sort visualizer and every other Uncodemy tool open straight away.