Let R be an equivalence relation on the set of students in this room, where (s, t) ∈ R if s and t have the same favorite among {AC, FN, FB, TR, CC, CR, PMC, ___}.
Operations We’d Like to Facilitate
find(4)
find(4) == find(8)
if find(7) != find(2): union(find(7), find(2))
Disjoint Sets ADT
We will implement a data structure in support of “Disjoint Sets”:
Maintains a collection S = {s0, s1, … sk} of disjoint sets.
Each set has a representative member.
Supports functions:
make_set(k) — creates a new set containing only k
union(k1, k2) — merges the sets containing k1 and k2
find(k) — returns the representative of the set containing k
A First Data Structure for Disjoint Sets
Find: __________
Union: __________
A Better Data Structure: UpTrees
If array value is -1, then we’ve found a root; otherwise value is the index of the parent.
x and y are in the same uptree iff they are in the same set.