SLIDE 1
Data Structures for Disjoint Set Union-Find Data Structure Disjoint - - PowerPoint PPT Presentation
Data Structures for Disjoint Set Union-Find Data Structure Disjoint - - PowerPoint PPT Presentation
Data Structures for Disjoint Set Union-Find Data Structure Disjoint Set Data Structure Disjoint Set Data Structure Storing a family of sets S = S 1 , S 2 , . . . , S k with i j S i S j = . Each set S i is
SLIDE 2
SLIDE 3
Disjoint Set Data Structure
Disjoint Set Data Structure
◮ Storing a family of sets S =
- S1,S2, . . . ,Sk
- with i j → Si ∩ Sj = ∅.
◮ Each set Si is identified by a representative si ∈ Si. ◮ Three operations: Make-Set, Union, Find-Set
Make-Set(x)
◮ Creates a new set {x}. (Clearly, x is representative of the set.) ◮ x cannot be in any other set already.
Union(x,y)
◮ Merges the sets containing x and y into one set.
Find-Set(x)
◮ Finds the representative of the set containing x.
3 / 17
SLIDE 4
Implementation
Idea
◮ Represent each set Si as rooted tree (i. e., S is a forest). ◮ Root of tree is representative
Example: S =
- {b,c,e,h}, {d, f ,д}
- c
h e b f d g
4 / 17
SLIDE 5
Implementation — Find-Set
Find-Set
◮ Follow pointers to root.
Example: Find(b) = c
c h e b f d g
5 / 17
SLIDE 6
Implementation — Find-Set
1 Procedure Find-Set(x) 2
While par(x) x
3
Let x := par(x).
4
Return x
6 / 17
SLIDE 7
Implementation — Union
Union(x,y)
◮ Find the representatives rx and ry of x and y (i. e., find roots of trees). ◮ Make rx parent of ry
Example: Union(b,д)
c h e b f d g
7 / 17
SLIDE 8
Implementation — Union
1 Procedure Union(x,y) 2
Set par(Find-Set(x)) := Find-Set(y)
8 / 17
SLIDE 9
Implementation
Questions
◮ What is the worst-case runtime for these operations? ◮ Can we improve the runtime?
9 / 17
SLIDE 10
Implementation
Questions
◮ What is the worst-case runtime for these operations? ◮ Can we improve the runtime?
Example
◮ Assume that we perform Union(1, 2), Union(1, 3), Union(1, 4), . . .,
Union(1,n).
◮ Then, the runtime is in O(n2).
9 / 17
SLIDE 11
Improving Find-Set
Observation
◮ If we use Find-Set multiple times on the same element, we have to
search for the root each time again. Idea
◮ Update the parent pointer when calling Find-Set such that it points on
the root.
1 Procedure Find-Set(x) 2
If par(x) x Then
3
Set par(x) := Find-Set(par(x))
4
Return par(x)
10 / 17
SLIDE 12
Improving Union
Idea: Union by Rank
◮ Keep track of height of a tree.
Number is denoted as rank of a vertex.
◮ Make root of smaller tree child of root of larger tree. 1 Procedure Make-Set(x) 2
par(x) := x
3
rank(x) := 0
Observation
◮ We only need to keep track of the rank of the root.
11 / 17
SLIDE 13
Improving Union
1 Procedure Union(x,y) 2
Let x := Find-Set(x).
3
Let y := Find-Set(y).
4
If rank(x) > rank(y) Then
5
Set par(y) := x.
6
Else
7
Set par(x) := y.
8
If rank(x) = rank(y) Then
9
Set rank(y) := rank(y) + 1.
12 / 17
SLIDE 14
Runtime
Assume our sets contain n elements in total. Runtime
◮ Worst case for single operation: O(logn)
(Why?)
◮ Worst case for m operations: O(m · α(n))
Thus, O(α(n)) amortised runtime per operation.
α-Function
◮ Inverse Ackermann function ◮ α(atoms in the universe) ≤ 4 ◮ Grows extremely slow. However, it is strictly speaking not constant.
13 / 17
SLIDE 15
Partition Refinement
SLIDE 16
Partition Refinement
Union-Find
◮ Start with a partition P = {S1,S2, . . . ,Sk} of a set S ◮ Step by step join two sets Si and Sj together. ◮ Union(i, j): P :=
- P \ {Si,Sj}
- ∪ {Si ∪ Sj}
Partition Refinement
◮ Start with a partition P = {S1,S2, . . . ,Sk} of a set S (often P = {S}) ◮ Step by step, based on a set X ⊆ S, split subsets Si into Si \ X
and Si ∩ X.
◮ Refine(X): P := { S \ X,S ∩ X | S ∈ P }
15 / 17
SLIDE 17
Implementation – Data Structure
Data Structure
◮ Set S is an array storing all its elements. ◮ Partition P is a (doubly-linked) list of subsets Si ◮ Subset Si is represented by two integers which describe the interval
(i. e., the first and last index) of Si in the array S
S s1 s2
1
s3
2
s4
3
s5
4
s6
5
s7
6
s8
7
P 0 | 2 3 | 5 6 | 7 S1 S2 S3
16 / 17
SLIDE 18
Implementation – Refinement
Refinement
◮ Flag all elements si ∈ X.
s1 s2 s3 s4 s5 s6 s7 s8 S
1 2 3 4 5 6 7
P 0 | 2 3 | 5 6 | 7 S1 S2 S3
17 / 17
SLIDE 19
Implementation – Refinement
Refinement
◮ Flag all elements si ∈ X. ◮ For each subset Si
◮ Reorder such that flagged elements are in front.
s1 s3 s2 s4 s5 s4 s7 s8 S
1 2 3 4 5 6 7
P 0 | 2 3 | 5 6 | 7 S1 S2 S3
17 / 17
SLIDE 20
Implementation – Refinement
Refinement
◮ Flag all elements si ∈ X. ◮ For each subset Si
◮ Reorder such that flagged elements are in front. ◮ Split Si into two sets containing only flagged or non-flagged elements.
s1 s3 s2 s4 s5 s4 s7 s8 S
1 2 3 4 5 6 7
P 0 | 1 2 | 2 3 | 4 5 | 5 6 | 7 S1 S2 S3 S4 S5
17 / 17