Skip to content

Topological sort (Kahn)
one step at a time.

Keep placing a node that nothing is waiting on and remove its arrows; if you run out while nodes remain, there is a cycle.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

Keep placing a node that nothing is waiting on and remove its arrows; if you run out while nodes remain, there is a cycle. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the frontier spreading, and which nodes are settled for good. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. Topological sort (Kahn) on a clean example teaches you the shape; topological sort (kahn) on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Start from a source and mark it seen.
  2. 2Take the next node from the frontier: a queue spreads in layers, a stack goes deep.
  3. 3Look at its neighbours, and add the ones never seen before.
  4. 4Never revisit a node, so every edge is examined once.

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

  • Course prerequisitesB needs both A and C, so it has to wait for C.
  • Three starting pointsThe queue starts with three nodes.
  • Has a cycleThe queue runs dry with nodes left over.
  • One straight lineOnly one valid order exists.
  • No prerequisites at allEvery node is ready from the start.

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(V + E)counted as real operations while it runs, not quoted from a table
Space
O(V)what the algorithm holds besides the input itself
Pattern
GraphsExplore in layers, or go deep, and never visit twice.

Everything in the app, in one place.

Practice, interviews and the tools, all free to start. Swipe to see the rest.

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About topological sort (kahn).

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the topological sort (kahn) visualiser show?

Every step of the algorithm on input you choose: the frontier spreading, and which nodes are settled for good, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of topological sort (kahn)?

O(V + E) time and O(V) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.