CPSC 221

Disjoint Sets

Today

  • The Disjoint Sets ADT
  • A first (naive) data structure
  • UpTrees
  • Smart unions: by height, by size
  • Path compression

A Disjoint Sets example

Let \(R\) be an equivalence relation on the students in this class, where \((s,t) \in R\)
if \(s\) and \(t\) have the same favorite among {AC, FN, FB, TR, CC, CR, PMC, ____}.

2 5 9 7 0 1 4 8 3 6

Operations we’d like to facilitate:

  1. Find(4)
  2. Find(4) == Find(8)
  3. 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 = \{s_0, s_1, \ldots, s_k\}\) of disjoint sets
  • Each set has a representative member
  • Supports functions:
void MakeSet(kType k);
void Union(kType k1, kType k2);
kType Find(kType k);

A first data structure for Disjoint Sets

0 1 4 2 7 3 5 6 0 0 1 0 2 2 3 3 4 0 5 3 6 3 7 2


Find:


Union:

A better data structure: UpTrees

  • If the array value is \(-1\), we’ve found a root; otherwise the value is the index of the parent.
  • \(x\) and \(y\) are in the same tree if and only if they are in the same set.
Sketch: 0 1 2 3 Memory: 0 -1 1 -1 2 -1 3 -1 0 1 2 3 0 1 2 3 0 1 2 3

Example: a partition into disjoint sets

2 5 9 7 0 1 4 8 3 6 9 2 5 7 0 1 8 4 6 3 0 4 1 8 2 5 3 6 4 -1 5 -1 6 -1 7 -1 8 4 9 5

UpTree implementation of Disjoint Sets ADT

int DS::Find(int i) {
    if (s[i] < 0) return i;
    else return Find(s[i]);
}

Running time depends on ________________.

Worst case?

What’s an ideal tree?

void DS::Union(int root1, int root2) {
    s[root2] = root1;
}




0 6 4 1

Smart unions

0 1 2 6 3 8 9 7 11 5 10 4 Union by height: Union by size: 0 6 1 6 2 6 3 8 4 5 10 6 7 7 8 7 9 7 10 4 11 5 0 6 1 6 2 6 3 8 4 5 10 6 7 7 8 7 9 7 10 4 11 5 Keeps overall height oftree as small as possible. Increases distance toroot for fewest nodes.

Both of these schemes for Union guarantee the height of the tree is ____________.

Smart unions

int DS::Find(int i) {
    if (s[i] < 0) return i;
    else return __________ Find(s[i]);
}
void DS::UnionBySize(int root1, int root2) {
    int newSize = s[root1] + s[root2];
    if (isBigger(root1, root2)) {
        s[root2] = root1;
        s[root1] = newSize;
    }
    else {
        s[root1] = root2;
        s[root2] = newSize;
    }
}

Path compression

1 3 5 6 4 2 8 7 9 11 10 1 9 8 7 3 2 6 4 5 11 10 before Find(5) after Find(5)


On the way back from the root,
every node on the path is
pointed straight at the root.

Nodes off the path (1, 8, 3, 6)
just come along with their parents.

The next Find on any of them
is one or two steps.

Analysis

Something we’ll need — Iterated log:

\[\log^* (n) = \begin{cases} 0 & \text{if } n \leq 1 \\ 1 + \log^*(\log n) & \text{if } n > 1 \end{cases}\]

Example: \(\log^* (2^{65536})\)

Relevant result: In an upTree implementation of Disjoint Sets using
smart Union and Find with path compression, any sequence of \(m\) Union
and Find operations results in worst case running time of \(O(\_\_\_\_\_\_\_\_\_\_)\),
where \(n\) is the number of items.

http://research.cs.vt.edu/AVresearch/UF/

Your turn

union(1, 7)
union(0, 6)
union(2, 3)
union(6, 3)
union(3, 7)
union(7, 4)

Union by size; a tie goes to the first argument.
union(a, b) calls Find on both first.

0 -1 1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1

What is the array afterwards:

  1. without path compression?
  2. with path compression?

Your turn: answer

Without path compression 7 1 3 2 4 6 0 5 0 -7 1 0 2 0 3 2 4 0 5 -1 6 0 7 1 With path compression 1 2 3 4 6 7 0 5 0 -7 1 0 2 0 3 0 4 0 5 -1 6 0 7 0