Tech Store

Showing suggestions.

Opening the store's index…
↑↓ to move↵ to openesc to close
Tech Store search
Animationv1.0.0~18 KB gzipped

Union-Find Animation

A glowing forest where groups merge and paths flatten: Union-Find with union by size and path compression, ready for your lessons, slides and posts

FreeFree
Format

.zip · HTML / CSS / JS · v1.0.0

Free — no account, no email. Always the newest version.

Live preview

Version
Preview width
Zoom

About

Union-Find is easiest to understand when you watch the forest change. This animation runs the real find, union and connected from the free algorithm page, step by step: every element is a glowing orb in a tree, union swings the smaller tree's root over to hang under the bigger one's, and find lights the path up to the root and, with path compression, flies every node it passed straight up to hang under it. parent[] and size[] sit under the forest and change in step, the line of code that is running lights up beside it, and a pair of bars sets the pointer hops against the same list without path compression.

  • Union by size and path compression, exactly as the listing writes them, with a switch to turn compression off
  • The listing's variables (root, node, big, small) glide over the orbs as chips, so every line of code has something to point at
  • Three seeded scenarios, friend circles, network links and the worst case for union by size, or your own operations typed in
  • Counters for finds, merges and pointer hops, and what the same list costs with and without compression
  • 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 find, union and connected exactly as the free algorithm page writes them: union by size, and a second pass of path compression
  • A forest of glowing orbs: union swings the smaller tree's root over to hang under the bigger one's, and compression flies every node on the path straight up to the root
  • find lights the path to the root one hop at a time, with the listing's own variables (root, node, big, small) gliding along as chips
  • parent[] and size[] under the forest, in step with every frame; the stale sizes of non-roots stay dimmed, just as the listing leaves them
  • Counters for finds, merges and pointer hops, and two bars that set the hops with path compression against the same list without it
  • Three seeded scenarios (friend circles, network links, the worst case for union by size), or type your own operations like union 0 1; find 1
  • Switch path compression on and off and watch the same list take more or fewer hops; the lines only compression runs dim when it is off
  • The code panel shows the method that is running, find, union or connected, with the call stack 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 18 KB gzipped (script and CSS together)

How to use it

Link the stylesheet and the script once, then add an empty element with data-union-find-animation wherever you want a player:

HTML
<link rel="stylesheet" href="union-find-animation.css" />
<script src="union-find-animation.js" defer></script>

<div class="union-find-animation" data-union-find-animation></div>

<div
  class="union-find-animation"
  data-union-find-animation
  data-union-find-animation-scenario="worst-case"
  data-union-find-animation-compression="false"
></div>

<div
  class="union-find-animation"
  data-union-find-animation
  data-union-find-animation-operations="union 0 1; union 2 3; union 1 3; find 3"
  data-union-find-animation-elements="6"
></div>

Options as data attributes, all starting with data-union-find-animation-:

Attribute Default What it does
-scenario friend-circles friend-circles, network-links or worst-case: the list of operations to run
-operations the scenario's Your own list: union a b, find a and connected a b, separated by ;, , or new lines (up to 24)
-elements the scenario's How many elements the forest starts with (2–16); made bigger when your list names more
-seed 7 Which list the scenario builds: the same seed always gives the same one
-compression true false runs find without the second pass of path compression
-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 (scenario, operations, elements, compression)

Elements added after the page loads: UnionFindAnimation.init(container); it returns a function that undoes it. For full control, UnionFindAnimation.createUnionFindAnimation(element, options) returns a handle with setScenario, setOperations(text) (it returns false and says why on the page when the list cannot be read), setElements, setCompression, shuffle, player (play, pause, seek…), trace() and destroy.

Ask about this product

Not sure it fits your project? Ask — the answer comes from this page.

Answers are written by AI from this page and can be wrong. The page itself is what counts.

Changelog

  1. 1.0.0First releaseLatest

    Plain HTML/CSS/JS and React builds of a step-by-step Union-Find (union by size, path compression) animation: a glowing forest where roots swing under roots and compressed nodes fly up, synced parent and size arrays, live code, hop counters with and without compression, three seeded scenarios or your own operations, player controls, light and dark.

  2. Fixes and new features land behind the same download, which always serves the latest version.