Upload
asher-craig
View
217
Download
0
Tags:
Embed Size (px)
Citation preview
DESIGN & ANALYSIS OF ALGORITHM06 – DISJOINT SETS
Informatics Department
Parahyangan Catholic University
DISJOINT SETS
Some applications involve grouping n distinct elements into a collection of disjoint sets
Two important operations: Find which set a given element belongs to Uniting two sets
DISJOINT SETS DATA STRUCTURE
Maintains a collection S = {S1, S2, … , Sk} of disjoint dynamic sets.
Each set is identified by a representative, which is some member of the set.
Operations : MAKE-SET(x) UNION(x, y) FIND-SET(x)
EXAMPLE : GRAPH’S CONNECTED COMPONENT
CONNECTED-COMPONENTS(G)
for each vertex v VMAKE-SET(v)
for each edge (u,v) Eif FIND-SET(u) ≠ FIND-SET(v)
UNION(u,v)
SAME-COMPONENT(u,v)
if FIND-SET(u) == FIND-SET(v)
return TRUE
else
return FALSE
Uses the disjoint-set operations to compute connected the components of a graph
Answers queries about whether two vertices are in the same connected component
REPRESENTATION #1 : LINKED LIST
Each list represents one set
The first element of the list is the set’s representative
Each node points to the set’s representative (first element of the list)
REPRESENTATION #1 : LINKED LIST
MAKE-SET and FIND-SET are easy, requiring O(1) time
UNION requires O(n) time for updating the pointer to set’s representative
a
head tail
b c
d
head tail
e
WEIGHTED UNION
Maintains the size of each list UNION operation adds the shorter list to the
longer list
The number of pointers updated is minimized Still takes O(n) time if the number of
elements of both lists are O(n)
How do weighted-union improves the
running time ?
REPRESENTATION #2 : ROOTED TREES
Each set represented as a tree
Each node points only to its parent
The root of each tree is the set’s representative, and is its own parent
REPRESENTATION #2 : ROOTED TREES
b c
e f g
h
d
a
j k
l
i
MAKE-SET creates a tree consisting of one node FIND-SET simply follows the parent pointer until it finds the
root UNION makes the root of one tree to points to the other
tree’s root
WHAT IS THE TIME COMPLEXITY ?
MAKE-SET is clearly O(1)
FIND-SET traces back to the root
UNION needs to find the roots of the two trees, thus performing 2x FIND-SET
→ O(n)
→ also O(n)
Why ?
CAN WE DO BETTER ?
A
B
+ =
A
B
A
BOR
UNION BY RANK
Similar to WEIGHTED-UNION, we add the shorter tree as a child of the taller tree
Keep track of rank of each tree, that is the upper bound of the tree’s height
How?
PATH COMPRESSION
For each node, we only need to know which node is the root
During FIND-SET, change the parent pointer of each node in find path to directly point to the root
Leave rank unchanged
a
b
c
d
a b c
d
=
How?
WHAT IS THE TIME COMPLEXITY ?
How do Union By Rank and Path Compression improves the time complexity ?
IMPLEMENTATION
Using two arrays: Array of parents Array of ranks
If the elements are not integer, use another array to map each element into an integer.
EXAMPLE
Apple
Melon Shoes
BananaGrapeT-Shirt
idx element
1 Apple
2 Melon
3 Shoes
4 Banana
5 Grape
6 T-Shirt
1
2 3
456
2
1
5
4
3
6
idx 1 2 3 4 5 6
parent 1 1 3 2 1 3
rank 2 1 1 0 0 0
APPLICATION : KRUSKAL’S MST
A Spanning Tree of a graph G is an acyclic subset T E that connects all of G’s vertices
The weight of T is the sum of all its edges’ weight
Such T with minimum possible weight is called the Minimum Spanning Tree (MST) of G
Kruskal’s MST algorithm is a greedy algorithm for finding the MST of graph G
KRUSKAL’S MST ALGORITHM
MST-KRUSKAL(G, w)
T = empty graph
for each v VMAKE-SET(v)
sort the edges of E into non-decreasing order by weight w
for each edge (u,v) E, taken in non-decreasing order by weight
if FIND-SET(u) ≠ FIND-SET(v)
T = T {(u,v)}UNION(u,v)
return T
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10
EXAMPLE
a
b c
i
h g f
d
e
8
8
11
4
7
4
2
7 6
1 2
14
9
10