Download Ch.8: Disjoint Sets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 8: The Disjoint Set Class
• Equivalence Classes
• Disjoint Set ADT
• Kruskal’s Algorithm
• Disjoint Set Implementation
CS 340
Page 132
The Disjoint Set Class
Background: Equivalence Relations
A relation R on a set S maps every pair of elements in S to
either TRUE or FALSE.
For example, the greater than relation, >, is TRUE for the
integer pair (5,3) (i.e., 5 > 3) but not for the pair (6,8) (since
6>8 is FALSE).
An equivalence relation R is a relation satisfying the
following three properties:
• (Reflexive)  R  for every  in set S.
• (Symmetric) For every pair  and  in set S, if  R , then
 R .
• (Transitive)
For every triple , ,  in set S, if  R  and 
For example…
R  ,• then
 R .equality is an equivalence relation for integers.
Modulo-10
• The greater than relation is not an equivalence relation for
integers (since it lacks the symmetric property).
• The does not equal relation is not an equivalence relation
for integers (since it lacks both the reflexive and transitive
properties).
• The electrical connectivity relation is an equivalence
relation for network system components (i.e., endstations,
servers, switches).
CS 340
Page 133
Equivalence Classes
An equivalence relation R splits the set S up into
disjoint (i.e., non-intersecting) subsets called
equivalence classes, such that:
• Every pair of elements in the same equivalence
class is R-equivalent.
• Every pair of elements from different equivalence
classes is not R-equivalent.
The Equivalence Classes for Electrical
Connectivity
The Equivalence Classes for Mod-3
Equality
15
3
21
6
-1
12
20 23
14
5
17
11
2
...
-4
8
-6
0 9
-3
18
...
1
16
13
22 4
19
-5
10
-2
7
...
CS 340
Page 134
The Disjoint Set ADT
Let’s define an abstract data type to implement
equivalence relations. The data will consist of the set S,
divided into equivalence classes. The desired operations
will be as follows:
A Union operation that merges two previously
separate equivalence classes.
D
I
J K
L M N O
P Q
CS 340
•
tree:
C
E F G H
A Find operation that determines which equivalence
class contains a particular set element.
To findFinding
the nearest
commonCommon
ancestor forAncestors
two nodes Uinand
V in a
Example:
Nearest
a Tree
A
B
•
• Originally, set up each node as its own equivalence class.
• Then traverse the tree in postorder…


Every time you return from traversing offspring Y of
parent X, perform Union(Find(X),Find(Y)) and mark X as
the “anchor” node for Find(X).
Continue until Find(U) and Find(V) are the same. At that
point, their common anchor is their nearest common
ancestor.
Page 135
Example: Kruskal’s Algorithm
A spanning tree for a graph is a tree formed
from edges of the graph that connects all of the
graph vertices.
A minimum spanning tree is one in which the
sum of the costs in the tree is minimized (where
every edge is assumed to have an associated
“cost”).
Kruskal’s Algorithm finds a minimum spanning
tree by repeatedly adding the previously
excluded edge with the least cost without
creating a loop. (Minimum spanning trees are
particularly useful in such applications as
network routing).
CS 340
Originally, each node is its own equivalence
class; a Union operation is performed between
distinct equivalence classes Find(X) and
Find(Y) whenever X and Y have a minimum
cost edge between them.
Page 136
Applying Kruskal’s Algorithm
6
B
5
C
7
3
A
4
8
D
7
E
6
4
8
H
3
F
9
G
7
ORIGINAL GRAPH
B
C
D
A
E
B
H
3
A
B
C
D
E
H
3
A
C
D
E
H
3
F
B
G
F
G
C
B
C
F
B
4
3
A
D
E
3
A
3
4
F
6
5
D
3
4
F
6
B
H
D
F
D
E
F
C
G
6
B
5
7
E
H
C
4
3
A
D
7
E
H
3
4
G
H
3
4
3
4
G
3
A
4
3
A
4
G
5
E
H
3
C
C
5
E
F
4
3
D
4
G
B
A
H
G
F
G
MINIMUM SPANNING TREE
CS 340
Page 137
Disjoint Set Union Operations
Seven elements, originally in distinct sets
1
2
Element
A
3
Element
B
4
Element C
5
Element
D
6
Element E
7
Element
F
Element
G
After Union(C,D) and Union(E,F)
1
2
Element
A
3
Element
B
5
Element C
7
Element E
4
Element
G
6
Element
D
Element
F
After Union(C,E)
1
2
Element
A
3
Element
B
7
Element C
4
Element
G
5
Element
D
Element E
6
Element
F
CS 340
Page 138
Disjoint Set Union Operation Choices
After Union(B,C) with random union
(Depth can become linear.)
1
2
Element
A
7
Element
B
Element
G
3
Element C
4
5
Element
D
Element E
6
Element
F
After Union(B,C) with union-by-size or union-by-height
(Depth remains logarithmic.)
1
3
Element
A
7
Element C
4
2
Element
B
Element
G
5
Element
D
Element E
6
Element
F
CS 340
Page 139
Disjoint Set Implementation
1
2
Element
A
7
Element
B
Element
G
When using random union, merely
use an array filled with the slot
numbers of the parent nodes (with
zero for nodes with no parent).
0 0 2 3 3 5 0
3
Element C
1
4
5
Element
D
Element
F
1
3
Element
A
7
Element C
Element
G
4
2
Element
B
5
Element
D
Element
E
6
Element
F
4
5
6
7
2
3
4
5
6
7
When using union-by-height, merely
use an array filled with the slot
numbers of the parent nodes, the
negation of the height of the tree for
nodes with offspring but no parent,
and zero for the nodes with no
offspring and
0 3 no
-2 parent.
3 3 5 0
1
CS 340
3
When using union-by-size, merely
use an array filled with the slot
numbers of the parent nodes, and
the negation of the size of the tree
for nodes -1
with
no 3parent.
3 -5
3 5 -1
Element
E
6
1
2
2
3
4
5
6
7
Page 140
Disjoint Set Application: Maze
Generation
Starting with a complete grid of walls, remove the two
representing the entrance and the exit, and consider
each cell in the grid as a disjoint set.
Randomly remove walls that separate two
disconnected cells, performing a union of the cells.
Continue until there is only one cell left.
CS 340
Page 141
Disjoint Set Application: Colorization
A monochrome image is analyzed one scanline at a
time, with white pixels grouped in equivalence classes
whenever it is determined that their respective regions
are separated by black border pixels.
Union operations occur whenever a non-border path
is discovered between two regions.
CS 340
Page 142
Related documents