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.
- 1Count the incoming arrows of every node.
- 2Add every node with zero incoming arrows to the queue.
- 3Take a node from the queue and add it to the order.
- 4Remove its arrows and queue any neighbour that drops to zero.
- 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.
Practise Graphs Beyond Topological Sort
Watching shows the queue and solving trains you to spot dependencies.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory checks your level, from graph basics to interview ready.
- 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
- 3You decide. No pressure, no payment on the call.