Skip to content

Topological Sort With Kahn's Algorithm
Step by Step

Kahn's algorithm places a node that nothing is waiting on, removes its arrows and repeats. Nodes left over mean a cycle.

Step through every queue move in both directions, then share a link to the step where the order broke.

What You Watch While the Queue Empties

The queue, the arrow counts and the final order change on every step.

Every node waits until all of its prerequisites are placed.

CodeStory's visualiser shows the queue filling and emptying while the order grows. Marked code lines follow every move, and you can step back, step forward or play.

Open Course prerequisites first. Then load Has a cycle to see the queue empty early.

How Kahn's Algorithm Orders Nodes

Steps below follow the queue on your screen.

  1. 1Count the incoming arrows of every node.
  2. 2Add every node with zero incoming arrows to the queue.
  3. 3Take a node from the queue and add it to the order.
  4. 4Remove its arrows and queue any neighbour that drops to zero.
  5. 5Stop when the queue is empty. Fewer nodes in the order than in the graph means a cycle.

Topological Sort Inputs Worth Trying

Five presets cover prerequisites, starting points and a cycle.

  • Course prerequisitesB needs both A and C, so B waits for C.
  • Three starting pointsThe queue starts with three nodes.
  • Has a cycleThe queue empties while nodes are left over.
  • One straight lineOnly one valid order exists.
  • No prerequisites at allEvery node is ready from the start.

Topological Sort Cost in Time and Space

Interviewers ask why Kahn's algorithm is linear, and the visualiser counts real operations.

Time
O(V + E)counted from the nodes and arrows you step through
Space
O(V)extra memory for the counts and the queue
Pattern
GraphsDependencies decide the order.

Everything in the App in One Place

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

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Topological Sort Visualiser FAQs

Open the visualiser and follow the queue node by node.

Open the visualiser
What does the topological sort visualiser show?

CodeStory's visualiser plays Kahn's algorithm on a graph you choose, with the queue, the arrow counts and the highlighted line of code in view. Move back and forth through every step, or let it play.

What is the time complexity of topological sort?

O(V + E) time and O(V) space. Live operation counts show the work growing with nodes and arrows instead of a formula on a slide.

What is Kahn's algorithm?

Kahn's algorithm keeps placing a node that nothing is waiting on and removes its arrows. If nodes remain after the queue empties, the graph has a cycle.

How do you detect a cycle with topological sort?

Count the nodes in the final order. Fewer than the graph holds means a cycle, and the Has a cycle preset shows the queue emptying early.

Can I use my own input?

Yes. Enter your own graph and the visualiser plays Kahn's algorithm on it.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Graphs? Get a Free Callback

Tell CodeStory which graph step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from graph basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.