~/ learn/ comp-372/ cards/ Disjoint sets & union-find
1 of 5

FIND-SET with path compression (two-pass recursion)

FIND-SET with path compression (two-pass recursion)

Answer

FIND-SET(x): if x != x.p: x.p = FIND-SET(x.p) # compress: point straight at root return x.p

The recursion ascends to the root; as it unwinds, x.p = FIND-SET(x.p) repoints every node on the path directly to the root. Future finds on those nodes are O(1).

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/0f68ef17-ec20-42df-8554-d78c2a757f27/flashcard utf-8 LF