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
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
Kruskal's algorithm is easiest to understand when you watch the cheapest network build itself.
This animation runs a real kruskal and find, step by step: the edges sort themselves lightest
first, then each one is picked up and tried. If its two ends are already in the same component it
would close a cycle and is refused, struck through and dashed. Otherwise it is taken, the smaller
tree goes under the bigger one, and the two colours become one. The parent and size arrays sit
under the graph and change in step, the line of code that is running lights up beside it, and a
running total climbs until a single gold tree connects everything.
- The listing's own logic: a stable sort, union by size, an early stop once the tree is full, checked against a differently written Kruskal, Prim's algorithm and a brute-force search on seeded random graphs
- A sorted edge list you can read: chips glide into place, then show taken, refused or never reached
- Union-find as colour: one colour per component, the root's number under every vertex, and a table of parent and size
- A running total weight and a count of tree edges out of n − 1
- Four seeded graphs (a fibre network, a cycle trap, a tie-break and a forest of islands), or your own edges typed in
- 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 · 12
- Animates kruskal and its find exactly as the panel lists them: a stable sort by weight, find on both ends of every edge, a refusal when the roots match, otherwise the smaller tree goes under the bigger (union by size); the listing is run as code in the tests and checked against the animation
- The edges sit in a grid that sorts itself: chips glide into lightest-first order, then each one is picked up, tried (both ends and their roots light up), and taken, struck through and dashed as a cycle, or never reached once the tree is complete
- Union-find components as colour: every vertex wears its tree's colour with its root's number underneath, and when two trees merge the smaller one flashes and takes the bigger one's colour
- A running total that ticks up with every edge taken, the tree edges counted out of n − 1, and a finished tree that glows gold in a wave across the nodes
- A parent and size table under the graph, in step with every frame, so the disjoint-set arrays are visible, not just implied
- Edge cases shown honestly: equal weights are taken in written order, a graph in pieces ends as a spanning forest with one tree per island, and a complete tree stops early with the leftover edges marked
- Four seeded graphs: a fibre network, a cycle trap of three triangles, a tie-break and a forest of islands, with a New button for fresh weights
- Type your own graph like 0-1:4; 1-2:3 (up to 12 vertices and 24 edges, weights 0–99, parallel edges allowed)
- The code panel shows kruskal and find with the lines that are running lit, and the call stack changes while find runs
- 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 20 KB gzipped (script and CSS together)
How to use it
Link the stylesheet and the script once, then add an empty element with
data-kruskal-animation wherever you want a player:
<link rel="stylesheet" href="kruskal-animation.css" />
<script src="kruskal-animation.js" defer></script>
<div class="kruskal-animation" data-kruskal-animation></div>
<div
class="kruskal-animation"
data-kruskal-animation
data-kruskal-animation-scenario="cycles"
data-kruskal-animation-seed="3"
></div>
<div
class="kruskal-animation"
data-kruskal-animation
data-kruskal-animation-edges="0-1:4; 0-2:1; 2-1:2; 1-3:1; 2-3:5; 3-4:3"
></div>Options as data attributes, all starting with data-kruskal-animation-:
| Attribute | Default | What it does |
|---|---|---|
-scenario |
network |
network, cycles, ties or islands: the graph to run |
-edges |
the preset's | Your own graph: a-b:w for an edge between a and b that costs w, separated by ;, , or new lines (up to 24, weights 0–99, vertices 0–11; the weight defaults to 1) |
-vertices |
as many as the edges name | How many vertices your own graph has (2–12); the ones no edge names stay alone |
-seed |
21 |
Which weights the preset builds: the same seed always gives the same graph |
-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 answer |
-code |
true |
false hides the code panel |
-inputs |
true |
false hides the rows of inputs (graph, edges) |
Elements added after the page loads: KruskalAnimation.init(container); it returns a function
that undoes it. For full control, KruskalAnimation.createKruskalAnimation(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, shuffle, player (play, pause, seek…),
trace() and destroy.
The algorithm itself is pure and lives in core/trace.ts: traceKruskal(n, edges) takes the
vertex count and a list of [u, v, weight] triples and returns every frame (state, caption and
the lines running), the edge ids of the tree in the order they were taken, and its total weight.
Copy KruskalAnimation.tsx, kruskal-animation.css and the core folder into your project:
import { KruskalAnimation } from "./KruskalAnimation";
<KruskalAnimation />
<KruskalAnimation scenario="ties" autoplay={false} />
<KruskalAnimation edges="0-1:4; 0-2:1; 2-1:2; 1-3:1" loop inputs={false} onFrame={(frame) => console.log(frame.caption)} />The props are the options above (scenario, edges, vertices, seed, 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 .kruskal-animation (or a class of your own):
| Property | What it changes |
|---|---|
--kru-tone-0 … --kru-tone-4 |
The accent colours (cyan, violet, pink, amber, mint) |
--kru-comp-light, --kru-comp-dark |
The lightness and chroma, as L C, of the colour each component is drawn in, in light and in dark; the hue comes from the component's root, one of twelve around the wheel |
--kru-accept, --kru-reject, --kru-final |
An edge that is taken, an edge refused as a cycle, and the finished tree |
--kru-lit |
The edge being tried and its two ends |
--kru-orb |
The ink every node is mixed with |
--kru-bg, --kru-surface, --kru-surface-strong, --kru-blur |
The background and the frosted glass |
--kru-edge, --kru-shadow, --kru-code-bg |
Borders, shadows, the code panel |
--kru-text, --kru-muted, --kru-on-accent, --kru-error, --kru-ring |
Text, secondary text, text on the coloured chips and buttons, the error message, focus ring |
--kru-font, --kru-mono |
The fonts for words and for code and numbers |
--kru-radius |
Corners of the player |
--kru-chip-h, --kru-chip-gap |
The height of an edge chip in the sorted list, and the gap between chips |
--kru-duration, --kru-ease |
How fast and how softly colours, glows and chips change and glide |
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 Kruskal minimum spanning tree animation: the edges sort themselves lightest first, each one is tried, taken or refused as a cycle, union-find components blend into one colour as islands merge, a running total climbs and the finished tree glows, a live parent / size table, four seeded graphs (one with ties, one in pieces) or your own edges, player controls, light and dark.
- Fixes and new features land behind the same download, which always serves the latest version.
Reviews
No reviews yet
Used it? Be the first to say how it went.
Write a review
How did it work for you? Reviews appear once we have read them.
You might also like
- AnimationGraphs
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
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



