~/ 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/aedd91ee-c795-4e77-b5de-56b9ea297f41/flashcard utf-8 LF