Union-Find & Artist Dedupe
In short: union-find (a disjoint-set) keeps a collection of groups
and answers "merge these two" and "are these the same group?" in effectively
constant time, without ever walking a graph. Each group has a root
(representative) that every member's parent pointer eventually leads to;
find follows pointers to the root, union hangs one root under the other.
Two add-ons make it fast: path compression (flatten pointers while you
walk) and union by rank (always hang the shorter tree under the taller).
Together: O(α(n)) amortized per operation — α is the inverse Ackermann
function, so slow-growing it's ≤ 4 for any n you could ever store, which is
why people call it "effectively constant".

Deduplicating Tunebox's artist profiles
Three labels uploaded the same artist three times: theweeknd, the-weeknd
and weeknd-official. Moderators report duplicates one pair at a time —
"theweeknd is weeknd-official", then later "the-weeknd is theweeknd" — and
Tunebox has to merge them transitively, then answer "same artist?" instantly
while reports keep arriving. At the start nobody has merged, so everyone is
their own parent — their own root:
find follows parent pointers until one points at itself:
parent = {"adele-music": "adele-hq", "adele-hq": "adele", "adele": "adele", "sza": "sza"}
def find(x):
while parent[x] != x:
x = parent[x]
return x
print(find("adele-music"), find("sza"))Two elements are in the same group exactly when find gives the same root.
The mistake: pointing the element you were handed at the other, instead
of pointing one root at the other — the second merge below silently
undoes the first:
All three profiles were reported as the same artist, so this should print True. It prints False. Fix union so merging never undoes an earlier merge.
❓ Cross-question — "Why not store the matches as a graph and run DFS?" For one question on a fixed graph, that's fine. But DFS costs O(V + E) per question, and matches keep arriving between questions. Union-find handles an interleaved stream of merges and questions at almost O(1) each.
A long chain of parents makes find slow, so path compression flattens
it while you walk: once you know the root, point every element you passed
through straight at it. Each arrow below is a parent pointer, and the
highlighted nodes are roots:
Unlucky unions build a chain, and one find with path compression flattens it for good.
Every element starts as its own parent, so there are five one-member groups. 5 nodes, 0 edges, undirected. Frontier: a, b, c, d, e.
parent = {"d": "c", "c": "b", "b": "a", "a": "a"}
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
find("d")
print(parent["c"])Union by rank avoids building tall trees in the first place: give each root a rank — an upper bound on its tree's height — and always hang the lower-ranked root under the higher one:
Both tricks together, as a class worth keeping, drawing its own parent pointers after every change — reorder the merges and run it again:
Your union-find: arrows are parent pointers, highlighted nodes are roots, and compression shortens every path it walks.