Professional Documents
Culture Documents
Lec - 15a Disjoint - Sets
Lec - 15a Disjoint - Sets
1
What Are Disjoint Sets?
Two sets A and B are disjoint if they have NO elements in
common. (A B = 0)
Disjoint Sets
We will determine if two objects are in the same disjoint set by defining a function which
finds the representative object of one of the disjoint sets
If the representative objects are the same, the objects are in the same disjoint set
An Application: Determining the Connected
Components of an Undirected Graph
CONNECTED-COMPONENTS(G) //computes connected components of a graph
1 for each vertex v in the set V[G]
2 do MAKE-SET(v)
3 for each edge (u,v) in the set E[G]
4 do FIND-SET(u) = FIND-SET(v)
5 then UNION(u,v)
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
6
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
7
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
8
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
(a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j}
9
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
(a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j}
(h, i) {a, c} {b, d} {e, g} {f} {h, i} {j}
10
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
(a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j}
(h, i) {a, c} {b, d} {e, g} {f} {h, i} {j}
(a, b) {a, b, c, d} {e, g} {f} {h, i} {j}
11
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
(a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j}
(h, i) {a, c} {b, d} {e, g} {f} {h, i} {j}
(a, b) {a, b, c, d} {e, g} {f} {h, i} {j}
(e, f) {a, b, c, d} {e, f, g} {h, i} {j}
12
An Application of Disjoint-Set Data Structures
Determining the connected components of an undirected graph G=(V,E)
a b e f h j
c d g i
Initial {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}
(b, d) {a} {b, d} {c} {e} {f} {g} {h} {i} {j}
(e, g) {a} {b, d} {c} {e, g} {f} {h} {i} {j}
(a, c) {a, c} {b, d} {e, g} {f} {h} {i} {j}
(h, i) {a, c} {b, d} {e, g} {f} {h, i} {j}
(a, b) {a, b, c, d} {e, g} {f} {h, i} {j}
(e, f) {a, b, c, d} {e, f, g} {h, i} {j}
(b, c) {a, b, c, d} {e, f, g} {h, i} {j}
13
Disjoint sets
14
Implementation
let each disjoint set be represented by a general tree
◦ The root of the tree is the representative object
To take the union of two such sets, we will simply attach one tree to the root of the other
Implementation
Normally, a node points to its children:
We are only interested in the root; therefore, our interest is in storing the parent
Disjoint sets
16
Implementation
For simplicity, we will assume we are creating disjoint sets the n integers
0, 1, 2, ..., n – 1
Implementation
This function is also easy to define:
void set_union( size_t i, size_t j ) {
i = find( i );
j = find( j ); Tset_union(n) = 2Tfind(n) + Q(1)
= O(h)
if ( i != j ) {
// slightly sub-optimal...
parent[j] = i;
}
}
The keyword union is reserved in C++
Disjoint sets
18
Example
Consider the following disjoint set on the ten decimal digits:
Disjoint sets
19
Example
If we take the union of the sets containing 1 and 3
set_union(1, 3);
we perform a find on both entries and update the second
Disjoint sets
20
Example
Now, find(1) and find(3) will both return the integer 1
Disjoint sets
21
Example
Next, take the union of the sets containing 3 and 5,
set_union(3, 5);
we perform a find on both entries and update the second
Disjoint sets
22
Example
Now, if we take the union of the sets containing 5 and 7 set_union(5, 7);
we update the value stored in find(7) with the value find(5):
Disjoint sets
23
Example
Taking the union of the sets containing 6 and 8
set_union(6, 8);
we update the value stored in find(8) with the value find(6):
Disjoint sets
24
Example
Taking the union of the sets containing 8 and 9
set_union(8, 9);
we update the value stored in find(8) with the value find(9):
Disjoint sets
25
Example
Taking the union of the sets containing 4 and 8
set_union(4, 8);
we update the value stored in find(8) with the value find(4):
Disjoint sets
26
Example
Finally, if we take the union of the sets containing 5 and 6 set_union(5, 6);
we update the entry of find(6) with the value of find(5):
Disjoint sets
27
Optimizations
whenever find is called, update the object to point to the root
◦ To ensure that you do not randomly remove the same wall twice, we can have an
array of unchecked walls
Disjoint sets
34