Skip to content
Computing Medium #topo-sort#dag#dependencies

Topological Sort

Kahn's algorithm peels off zero in-degree nodes. The build/dependency-order foundation.

A free, animated topological sort you can read here or embed on any website, from Scrollchart.

Topological Sort

Topological Sort (Kahn's Algorithm)Zero in-degree nodes enter queue first. Each emission decrements successors. Cycle detected when emitted < n.

A DAG with task dependencies. Nodes with zero in-degree enter a queue; dequeue emits an order, decrementing successors' in-degrees. Cycles are detected when the emitted count is less than n.

Good for

  • Build system dependency resolution (Make, Bazel, Gradle)
  • Package manager install ordering (npm, pip, apt)
  • Course prerequisite scheduling and curriculum planning

Source & accuracy

This topological sort is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Ordering tasks respecting dependency constraints

A topological sort arranges directed acyclic graph (DAG) nodes into a linear order such that every edge points forward. If task B depends on task A, A must appear before B in the topological order. This is essential for build systems, package managers, and workflow schedulers where prerequisites must complete before dependents can start.

Kahn's algorithm, the most intuitive approach, iteratively removes nodes with zero in-degree (no incoming edges). Each removal reduces in-degree of its successors. If a node never reaches zero in-degree, a cycle exists and no valid topological order is possible. This provides both a solution and a cycle-detection mechanism.

Implementation and cycle detection

Kahn's algorithm uses a queue: initialize in-degrees for all nodes, enqueue all zero in-degree nodes, then iteratively dequeue, decrement in-degrees of neighbors, and enqueue newly zeroed nodes. The output order is topological. If fewer than n nodes are processed, a cycle is present and the sort is impossible.

Depth-first search offers an alternative: perform DFS from each unvisited node, pushing to a stack only after visiting all descendants. Reversing the stack yields a topological order. Both approaches run in O(V + E) time and handle the cycle-detection requirement intrinsically.

Embed this diagram

Add this animated topological sort to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated topological sort for any website.
Who uses it
Developer blogs, DevOps / SRE sites, CS educators.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="topological-sort" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "Topological Sort" for my website?
Scrollchart provides "Topological Sort" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a topological sort in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.