Parent Pointers in Disjoint Sets

A disjoint-set structure represents each group through parent pointers leading to a representative root.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two unions
  2. Avoid relying on a chosen root
  3. Walk paths to roots before and after compression
  4. Join roots and keep cycles out of the forest

Trace two unions

Start with separate A, B, C. Join A and B by making one root point to the other, then join that group with C. All three finds should reach the same representative. Disjoint-set union explains how weighting and path compression reduce path lengths.

Avoid relying on a chosen root

A later merge may change which root represents earlier elements. If a display needs a stable group label, store that as separate data under a suitable merge rule. Stable collection identifiers explain the separate requirement.[1]

Walk paths to roots before and after compression

Suppose B points to A, C points to B, and A points to itself. Finding C follows two pointers and returns A. Compression can then point C directly to A without changing which members belong to which components. See working with tree structure.

Before: C → B → A → A
find(C) yields A
After full compression: C → A, B → A
Membership equivalence has not changed

Join roots and keep cycles out of the forest

To merge two components, compare their representative roots and link one root beneath the other if they differ. Connecting arbitrary interior nodes can invalidate representation assumptions. Test repeated union of two nodes already sharing a root and a later lookup through a compressed chain.

References

  1. Jeff Erickson, Algorithms ↩