Dijkstra Animation
A glowing weighted graph where the shortest path spreads one settled node at a time: Dijkstra with a live priority queue, for lessons and slides
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
Dijkstra's algorithm is easiest to understand when you watch the shortest path spread. This
animation runs the real dijkstra and shortestPath from the free algorithm page, step by step:
the vertex the queue hands out locks in, its edges are tried one by one, and every edge that
beats a distance flows, updates the number on its node and pushes a fresh entry into the queue.
Old entries are not removed. They stay in the queue, struck through, until they come out and are
skipped. The dist and prev arrays sit under the graph and change in step, the line of code
that is running lights up beside it, and in the end the shortest-path tree glows and the path to
your target lights up.
- The listing's own logic: lazy deletion, the
(distance, vertex)tie-break and a strict<, checked against the listing on seeded random graphs - A priority queue you can read: pop order, the entry just popped, stale entries marked
- Four seeded graphs (a city map, a longer route that is cheaper, a tie-break and an unreachable island), or your own edges typed in
- Pick any source and any target; counters for pops, relaxations and stale pops
- 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 dijkstra and shortestPath exactly as the free algorithm page's JavaScript listing writes them: a lazy priority queue ordered by distance then vertex, the stale check on every pop, a strict < to relax
- A node-link graph with weighted edges: the frontier glows amber, every relaxed edge flows and the tentative distance on its node changes, and a settled node locks in a colour that shows how far it is from the source
- A live priority queue in pop order, with the entry just popped beside it, the newest entry glowing and stale entries struck through and dashed until they are skipped
- A dist and prev table under the graph in step with every frame, and a prev tree that grows (dashed while a vertex is only reached, solid once it is final)
- At the end the shortest-path tree lights up, then shortestPath walks back from the target one vertex at a time and the path glows gold with its cost; a target nothing leads to ends on an empty path
- Counters for pops, relaxations and stale pops, and the listing's own variables (u and v) gliding over the nodes as chips
- Four seeded graphs: a city map, a graph where the longer route is cheaper, a tie-break and an unreachable island, with a New button for fresh weights
- Pick the source and the target, or type your own graph like 0-1:4; 1>2:3 (a dash for two-way edges, an arrow for one-way, up to 12 vertices and 24 edges)
- The code panel shows dijkstra, then shortestPath, with the lines that are running lit
- 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 21 KB gzipped (script and CSS together)
How to use it
Link the stylesheet and the script once, then add an empty element with
data-dijkstra-animation wherever you want a player:
<link rel="stylesheet" href="dijkstra-animation.css" />
<script src="dijkstra-animation.js" defer></script>
<div class="dijkstra-animation" data-dijkstra-animation></div>
<div
class="dijkstra-animation"
data-dijkstra-animation
data-dijkstra-animation-scenario="shortcut"
data-dijkstra-animation-seed="3"
></div>
<div
class="dijkstra-animation"
data-dijkstra-animation
data-dijkstra-animation-edges="0-1:4; 0-2:1; 2-1:2; 1-3:1; 2-3:5; 3-4:3"
data-dijkstra-animation-source="0"
data-dijkstra-animation-target="4"
></div>Options as data attributes, all starting with data-dijkstra-animation-:
| Attribute | Default | What it does |
|---|---|---|
-scenario |
city-map |
city-map, shortcut, tie-break or unreachable: the graph to run |
-edges |
the preset's | Your own graph: a-b:w for an edge that works both ways, a>b:w for a one-way edge, 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, at ∞ |
-source |
the preset's (0) | Where the run starts |
-target |
the preset's (the last vertex) | Where the path ends |
-seed |
7 |
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, source, target, edges) |
Elements added after the page loads: DijkstraAnimation.init(container); it returns a function
that undoes it. For full control, DijkstraAnimation.createDijkstraAnimation(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, setSource, setTarget, shuffle, player
(play, pause, seek…), trace() and destroy.
Copy DijkstraAnimation.tsx, dijkstra-animation.css and the core folder into your project:
import { DijkstraAnimation } from "./DijkstraAnimation";
<DijkstraAnimation />
<DijkstraAnimation scenario="tie-break" autoplay={false} />
<DijkstraAnimation edges="0-1:4; 0-2:1; 2-1:2; 1-3:1" source={0} target={3} loop inputs={false} onFrame={(frame) => console.log(frame.caption)} />The props are the options above (scenario, edges, vertices, source, target, 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 .dijkstra-animation (or a class of your own):
| Property | What it changes |
|---|---|
--dijk-tone-0 … --dijk-tone-4 |
The accent colours (cyan, violet, pink, amber, mint) |
--dijk-heat-0, --dijk-heat-1, --dijk-heat-2 |
The colours a settled node, its edge and its table cell take as the distance grows: the source's colour, the middle, the farthest vertex |
--dijk-frontier, --dijk-relax, --dijk-keep, --dijk-stale, --dijk-path |
Reached but not final, a relaxed edge, an edge that is no better, a stale entry or a skipped vertex, and the shortest path |
--dijk-lit |
The vertex being finished and the edge being tried |
--dijk-orb |
The ink every node is mixed with |
--dijk-bg, --dijk-surface, --dijk-surface-strong, --dijk-blur |
The background and the frosted glass |
--dijk-edge, --dijk-shadow, --dijk-code-bg |
Borders, shadows, the code panel |
--dijk-text, --dijk-muted, --dijk-on-accent, --dijk-error, --dijk-ring |
Text, secondary text, text on the coloured chips and buttons, the error message, focus ring |
--dijk-font, --dijk-mono |
The fonts for words and for code and numbers |
--dijk-radius |
Corners of the player |
--dijk-duration, --dijk-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 Dijkstra animation: a weighted graph where the frontier glows, edges relax and settled nodes lock in, a live priority queue with its stale entries, a dist / prev table, the shortest-path tree and the path to a chosen target, four seeded graphs 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
- AnimationPathfinding
Pathfinding Maze
An editable neon maze where BFS, Dijkstra, A* and greedy search race to the goal, step by step
Free


