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
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
Breadth-first and depth-first search are the same code with one different container, and that one
change is easiest to understand when you watch it. This animation runs the real bfs and dfs
from the free Graph Traversal page, step by step, on a glowing node-link graph: BFS takes the
oldest vertex from a queue and spreads out in rings, DFS takes the newest from a stack and dives
along a trail before it backs up. The queue (with its head) or the stack (with its top) is drawn
live under the graph, the line of code that is running lights up beside it, and a strip of numbered
badges fills in with the order of the visit.
Press Compare and both searches run side by side on the same graph, one timeline, so you can point at the exact step where they part ways. The two moments the page spends the most words on are impossible to miss: BFS marking a vertex the instant it is queued, and DFS throwing away a stale entry that was pushed twice.
- Exactly the listing's logic: BFS marks on push, DFS pushes neighbours last to first, marks on pop and skips stale entries
- Compare mode with one shared timeline, or BFS and DFS on their own
- Six preset graphs, including the page's A–F graph, islands the search cannot cross and a cycle that makes DFS skip a stale entry, or type your own edges and pick the start vertex
- Counters for visited, pushes, stale skips and the biggest the queue or stack got
- 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 · 14
- Animates bfs and dfs exactly as the free algorithm page writes them: BFS with a queue and a head index that marks a vertex when it is pushed, DFS with an explicit stack that pushes neighbours last to first, marks on pop and skips stale entries
- A glowing node-link graph: every vertex takes a colour from its depth in the traversal tree, so BFS fills in as rings and DFS changes hue as it dives; tree edges light up from parent to child and the edges the tree does not need are dimmed
- A live queue with its head pointer, or a stack with its top: every entry shows who put it there, and a stale copy is struck through the moment its vertex is marked, then thrown away on a step of its own
- The two moments the page is about get their own colour on the listing: the BFS line visited[v] = true lights mint, the DFS line if (visited[u]) continue lights amber when it fires
- A glowing trail from the start to the vertex being processed, which flows towards it and shortens when DFS backs up
- A visit-order strip that fills with numbered badges, grouped by level for BFS and by branch for DFS
- Compare mode: BFS and DFS side by side on the same graph, on one timeline, so step 20 is the same step in both
- Six preset graphs (the page's A–F graph, a binary tree, a grid, islands, a cycle, a seeded random graph) or your own edges like 0-1; 0-2; 1-3, with 2–16 vertices and any start vertex
- Vertices the search cannot reach are dimmed on the last frame and the caption names them
- Counters for visited, pushes, stale skips and the most the queue or stack held at once
- The code panel shows the listing's own lines for the search that is running, with the lines of the current step 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 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-graph-traversal-animation wherever you want a player:
<link rel="stylesheet" href="graph-traversal-animation.css" />
<script src="graph-traversal-animation.js" defer></script>
<div class="graph-traversal-animation" data-graph-traversal-animation></div>
<div
class="graph-traversal-animation"
data-graph-traversal-animation
data-graph-traversal-animation-preset="tree"
data-graph-traversal-animation-mode="bfs"
></div>
<div
class="graph-traversal-animation"
data-graph-traversal-animation
data-graph-traversal-animation-edges="0-1; 0-2; 1-3; 2-3; 3-4; 2-5"
data-graph-traversal-animation-start="0"
></div>Options as data attributes, all starting with data-graph-traversal-animation-:
| Attribute | Default | What it does |
|---|---|---|
-preset |
page |
page, tree, grid, islands, cycle or random: the graph to search |
-edges |
the preset's | Your own graph: edges like 0-1 or 0 1, separated by ;, , or new lines (up to 40, vertices 0–15). The order sets each vertex's neighbour list, which is the order a search tries them in |
-vertices |
just enough | How many vertices your own graph has (2–16, never fewer than the edges name); the extra ones stand alone |
-start |
the preset's | The vertex both searches start at |
-mode |
compare |
bfs, dfs, or compare to run both side by side on one timeline |
-seed |
7 |
Which graph the random preset draws: the same seed always gives the same one |
-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 last frame |
-code |
true |
false hides the code panels |
-inputs |
true |
false hides the rows of inputs (preset, mode, edges, vertices, start) |
Elements added after the page loads: GraphTraversalAnimation.init(container); it returns a
function that undoes it. For full control, GraphTraversalAnimation.createGraphTraversalAnimation(element, options)
returns a handle with setPreset, setEdges(text) (it returns false and says why on the page
when the edges cannot be read), setVertices, setStart, setMode, shuffle, player (play,
pause, seek…), traces() (one per search on show, every frame included) and destroy.
Copy GraphTraversalAnimation.tsx, graph-traversal-animation.css and the core folder into
your project:
import { GraphTraversalAnimation } from "./GraphTraversalAnimation";
<GraphTraversalAnimation />
<GraphTraversalAnimation preset="tree" mode="bfs" autoplay={false} />
<GraphTraversalAnimation edges="0-1; 0-2; 1-3" start={0} loop inputs={false} onFrame={(frame, index, search) => console.log(search, frame.caption)} />The props are the options above (preset, edges, vertices, start, mode, seed, speed,
autoplay, loop, code, inputs), plus onFrame(frame, index, search), called with every
frame drawn, once for each search on show (search is "bfs" or "dfs"). Changing a prop starts
the searches 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 .graph-traversal-animation (or a class of your own):
| Property | What it changes |
|---|---|
--gt-tone-0 … --gt-tone-7 |
The eight depth colours (cyan, blue, violet, pink, orange, amber, lime, mint): a vertex, its tree edge and its chips take the colour of their depth, going round after eight |
--gt-bfs, --gt-dfs |
The colour of each search's name, queue or stack and highlighted code lines |
--gt-mark, --gt-stale |
The two moments the lesson is about: marking a vertex when it is queued (BFS), and a stale entry being skipped (DFS) |
--gt-lit |
The path from the start to the vertex being processed, and the vertex itself |
--gt-orb |
The ink every vertex is mixed with |
--gt-bg, --gt-surface, --gt-surface-strong, --gt-blur |
The background and the frosted glass |
--gt-edge, --gt-shadow, --gt-code-bg |
Borders, shadows, the code panel |
--gt-text, --gt-muted, --gt-on-accent, --gt-error, --gt-ring |
Text, secondary text, text on the coloured chips and buttons, the error message, focus ring |
--gt-font, --gt-mono |
The fonts for words and for code and numbers |
--gt-radius |
Corners of the player |
--gt-duration, --gt-ease |
How fast and how softly lines draw in and chips 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 BFS and DFS animation: a glowing node-link graph, a live queue with its head or a stack with its stale entries struck through, a numbered visit-order strip, side-by-side compare mode, six preset 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
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
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
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



