Topological Sort Animation
A glowing dependency graph that peels itself in order: Kahn's algorithm with live in-degree badges, a visible FIFO queue and the order filling up
5.0{count} review.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
Topological sort is easiest to understand when you watch the graph peel itself. This animation runs Kahn's algorithm from the free algorithm page, step by step: every task is a glowing vertex with a live in-degree badge, the vertices with nothing left to wait for fly into a visible FIFO queue, and taking one appends it to the order strip, crosses its outgoing edges out and ticks its neighbours' badges down. The line of code that is running lights up beside it, and when the graph has a cycle the queue runs dry early, the leftovers turn red and the caption explains why the order is short.
- Kahn's algorithm exactly as the listing writes it, including the
headindex that makes the queue O(1) - Live in-degree badges, an
inDegree[]array under the graph, and a queue and an order drawn as real arrays - Five ready-made graphs, course prerequisites, build steps, a diamond, a cycle and the page's two-source example, or your own edges typed in
- Counters for pops, edges removed and queued vertices, and the +1s of counting against the −1s of crossing out
- A full player with a scrubber and 0.5×–4× speed, built to look good on a slide or in a recording
- Every colour, radius, font and timing is a CSS custom property; light and dark follow your page
- Nothing to install: one CSS file and one script, or one React component
Features · 11
- Animates Kahn's algorithm exactly as the free algorithm page writes it: in-degree counting, a FIFO queue with a head index, and the order built one pop at a time
- A layered directed graph with arrows, a live in-degree badge on every vertex that ticks up while counting and down as edges are crossed out, and a glow on every vertex that is ready
- The queue shown as the real array: ready vertices fly into their slots, the head marker moves along, taken entries stay behind dimmed, and each pop slides into the order strip
- Crossed-out edges fade as their neighbours' badges tick down; each placed vertex keeps a colour from its place in the order, so the sweep of colours shows the order
- On a cycle the queue runs dry early: the leftover vertices and the edges never crossed out turn red, the empty order slots are marked, and the caption says why the order is short
- inDegree[] under the graph in step with every frame, counters for pops, edges removed and queued vertices, and two bars that set the +1s of counting against the −1s of crossing out
- Five ready-made graphs (the two-source example, course prerequisites, build steps, a diamond, a cycle), or type your own edges like 0>1; 0>2; 1>3 (up to 10 vertices and 20 edges, repeated edges and self-loops included)
- The code panel shows the listing line for line, with the lines the frame is on lit, and the values of u, v and head above it
- Player: play, pause, step back and forward, reset, scrub, 0.5×–4× speed, optional loop
- Plain HTML/CSS/JS and React (Next.js ready) builds, light and dark
- Zero dependencies, about 19 KB gzipped (script and CSS together)
How to use it
Link the stylesheet and the script once, then add an empty element with
data-topological-sort-animation wherever you want a player:
<link rel="stylesheet" href="topological-sort-animation.css" />
<script src="topological-sort-animation.js" defer></script>
<div class="topological-sort-animation" data-topological-sort-animation></div>
<div
class="topological-sort-animation"
data-topological-sort-animation
data-topological-sort-animation-scenario="cycle"
></div>
<div
class="topological-sort-animation"
data-topological-sort-animation
data-topological-sort-animation-edges="0>1; 0>2; 1>3; 2>3"
data-topological-sort-animation-vertices="5"
></div>Options as data attributes, all starting with data-topological-sort-animation-:
| Attribute | Default | What it does |
|---|---|---|
-scenario |
two-sources |
two-sources, courses, build, diamond or cycle: the graph to run |
-edges |
the scenario's | Your own graph: edges like 0>1, 0->1, 0→1 or 0 1, separated by ;, , or new lines (up to 20, vertices 0–9). Repeated edges and self-loops are fine |
-vertices |
just enough | How many vertices your edges live on (2–10); made bigger when your edges name more, and the extra ones start with nothing pointing at them |
-speed |
1 |
0.25 to 4 |
-autoplay |
true |
Plays on its own once loaded (never with reduced motion) |
-loop |
false |
Starts again after a pause on the summary |
-code |
true |
false hides the code panel |
-inputs |
true |
false hides the rows of inputs (graph buttons, edges, vertices) |
Edges are written in the order the listing meets them: the vertices a vertex points to are
visited in the order you type them, so 0>2; 0>1 queues 2 before 1.
Elements added after the page loads: TopologicalSortAnimation.init(container); it returns a
function that undoes it. For full control, TopologicalSortAnimation.createTopologicalSortAnimation(element, options)
returns a handle with setScenario, setEdges(text) (it returns false and says why on the
page when the edges cannot be read), setVertices, player (play, pause, seek…),
trace() and destroy.
Copy TopologicalSortAnimation.tsx, topological-sort-animation.css and the core folder into
your project:
import { TopologicalSortAnimation } from "./TopologicalSortAnimation";
<TopologicalSortAnimation />
<TopologicalSortAnimation scenario="courses" speed={2} loop />
<TopologicalSortAnimation edges="0>1; 0>2; 1>3; 2>3" vertices={5} inputs={false} onFrame={(frame) => console.log(frame.caption)} />The props are the options above (scenario, edges, vertices, speed, autoplay, loop,
code, inputs), plus onFrame(frame, index), called with every frame drawn. Changing a prop
starts the run again. Every other prop goes to the root <div> (className, id, aria-*…).
React 18 or 19.
The file starts with "use client", so use it from any page or server component. With the App
Router nothing else is needed. With the Pages Router, move the CSS import into pages/_app.
Set these on .topological-sort-animation (or a class of your own):
| Property | What it changes |
|---|---|
--topo-cyan, --topo-violet, --topo-pink, --topo-mint, --topo-amber, --topo-rose |
The palette every state is made from |
--topo-ready, --topo-waiting, --topo-cross, --topo-stuck |
A vertex that is ready (in-degree 0), one still waiting, the edge being crossed out, and everything stuck on a cycle |
--topo-seq-from, --topo-seq-mid, --topo-seq-to |
The three colours placed vertices and the order strip sweep through, from first to last |
--topo-link, --topo-lit, --topo-ink |
Edges, a vertex that was just taken, and the ink every vertex is mixed with |
--topo-bg, --topo-surface, --topo-surface-strong, --topo-blur |
The background and the frosted glass |
--topo-border, --topo-shadow, --topo-code-bg |
Borders, shadows, the code panel |
--topo-text, --topo-muted, --topo-on-accent, --topo-error, --topo-ring |
Text, secondary text, text on the coloured chips and buttons, the error message, focus ring |
--topo-font, --topo-mono |
The fonts for words and for code and numbers |
--topo-radius |
Corners of the player |
--topo-duration, --topo-ease |
How fast and how softly vertices glow and tokens fly |
Colours use light-dark(): they follow color-scheme on your page. With
prefers-reduced-motion, nothing plays on its own and frames change instantly; every control
still works.
Ask about this product
Not sure it fits your project? Ask — the answer comes from this page.
Changelog
1.0.0First releaseLatest
Plain HTML/CSS/JS and React builds of a step-by-step topological sort (Kahn's algorithm) animation: a layered directed graph with live in-degree badges, a visible FIFO queue with its head index, an order strip filling up as edges are crossed out, stuck vertices lighting up on a cycle, five ready-made graphs or your own edges, live code, counters, player controls, light and dark.
- Fixes and new features land behind the same download, which always serves the latest version.
Reviews
- 5 stars: 1
- 4 stars: 0
- 3 stars: 0
- 2 stars: 0
- 1 stars: 0
tu
Write a review
How did it work for you? Reviews appear once we have read them.
You might also like
- AnimationGraphs
Kruskal Animation
A glowing graph where the cheapest network builds itself: Kruskal's minimum spanning tree with a sorted edge list and union-find colours, for lessons and slides
Free - AnimationGraphs
Graph Traversal Animation
Queue or stack, watch the search choose: BFS and DFS on a glowing graph, side by side, ready for your lessons, slides and posts
Free



