workshop private

← all creations

Collapse

viz · created 2026-09-04

Union–find as a live forest, one operation stream run twice — plain quick-union growing a chain beside union-by-rank with path compression, where every find lights the pointers it walked and then snaps them all flat, and the stored rank stays behind as a bound that is already wrong.

algorithmsinterview-prepcanvas

Two disjoint-set forests, the same stream of union and find operations, side by side. The left forest is plain quick-union — hang the root of one tree under the root of the other, no heuristics. The right is union by rank with path compression. Both answer every query correctly and end with exactly the same components; the only thing that differs is what the pointers cost, and a strip along the bottom draws the two running hop counts pulling apart.

The payoff is a thing complexity tables assert and never show: a find is not just a read. When the compressed forest walks four pointers to reach a root, all four nodes are repointed straight at it on the way back down — the path lights up, then the tree visibly collapses under it, and the next query through any of those nodes is one hop. The query paid for its successors. That is the whole of amortization, and it is a picture rather than a paragraph.

The default stream is adversarial on purpose: it always unions the same node into a fresh one, which under plain quick-union hangs the entire accumulated tree under the newcomer every single time. The left pane becomes one chain as tall as the node count and every subsequent step walks all of it; the right pane is a flat star of height 1 by the third operation. Switch to random pairs for the honest average case, where the gap is smaller but still lopsided, and where the second detail shows up.

That second detail is on the root badges, which read h·/r·: the tree’s actual height against the stored rank. Rank is only ever an upper bound — it goes up when two equal-rank roots merge and is never lowered, because compression flattens trees without telling anyone. So a tree that has been queried hard sits at height 1 while claiming rank 3, the pill turns amber, and the algorithm remains correct anyway: the bound is used to pick which root survives a merge, and a bound that is too large only ever makes that choice conservative. Wrong-looking but sound, which is the interesting half of union-find.

Hover any node to see its chain to the root drawn in both forests at once, with the two hop counts in the readout — the fastest way to feel what the difference actually buys.

Reuse

src/collapse.js is a framework-free ES module with no canvas and no timers:

Same trace-per-tick shape as rho, sift, window and astar-grid: the module produces frames, the demo draws them.

Gotchas