The Disjoint Set Class S
The Disjoint Set Class S
The Disjoint Set Class S
Overview
Equivalence Relations q The Dynamic Equivalence Problem
Simple algorithms for solving
Find Union
Example p
Examples: E l
Electrical Connectivity Same country: Two cities are related if they belong to the same country
What if relation is implicit? Equivalence class of an element a S is a subset of S which contains all the elements that are related to a
check if a and b belong to the same equivalence class class.
Disjoint sets
Dynamic : the sets change during the course of the algorithm l ith Observations: Ob ti
Values doesnt matter ,location matters find(a) == find(b) matters
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
Union operation
union : make parent link of root of one tree to the p f f root of the other tree
Union(4,5)
Union(6,7)
Union(4,6)
Union(3,4) -1 1 0 1 -1 1 -1 1 2 4 3 -5 5 4 4 5 4 6 6 7
-1 0 1
-1
-1 2
4 3
-3 4
4 5
4 6
6 7
Path Compression
After fi d( ) f find(14)
Path Compression
2.
3. 3
4.
Use the union/find data structure I iti ll each cell is in its own equivalence Initially, h ll i i it i l class
Initial State: All walls up, all cells in their own set.
Eventually, 24 walls are knocked down. All elements are in the same set.
Questions?