Professional Documents
Culture Documents
Unit 7: Disjoint Sets: Course Contents
Unit 7: Disjoint Sets: Course Contents
․Course contents:
⎯ Data structures for disjoint sets
․Reading:
⎯ Chapter 21
⎯ Union(x, y): Sx ∪ Sy
Connected-Components(G) Same-Component(u,v)
1. for each vertex v ∈ V[G] /* check if u, v are in the same
2. Make-Set(v); set */
3. for each edge (u, v) ∈ E[G] 1. if Find-Set(u) = Find-Set(v)
4. if Find-Set(u) ≠ Find-Set(v) 2. return TRUE;
5. Union(u, v); 3. return FALSE;
Make-Set(x)
1. p[x] ← x;
2. rank[x] ← 0. Link(x,y)
1. if rank[x] > rank[y]
Union(x,y) 2. p[y] ← x;
1. Link(Find-Set(x), Find-Set(y)). 3. else p[x] ← y;
4. if rank[x] = rank[y]
Find-Set(x) 5. rank[y] ← rank[y]+1.
1. if x ≠ p[x]
2. p[x] ← Find-Set(p[x]);
3. return p[x].