Experiments on Union-Find Algorithms for the Disjoint

Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Experiments on Union-Find Algorithms for the
Disjoint-Set Data Structure
Md. Mostofa Ali Patwary*
Jean Blair**
Fredrik Manne*
*Department of Informatics, University of Bergen, Norway
**Department of EE and CS,United States Military Academy, USA
May 20-22, 2010
SEA 2010, Italy.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Overview
I
I
Extensive experimental study comparing 55 different
variations of Union-Find algorithm.
The study includes:
I
I
I
I
All the classical algorithms.
Several recently suggested enhancements.
Different combinations and optimizations of these.
Main Result: A somewhat forgotten simple algorithm
developed by Martin Rem in 1976 is the fastest algorithm.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Related Experimental Studies
Reference
Application
Computing
[Liu, 1990]
Sparse matrix
Factorization
2
[Gilbert et al., 1994]
Sparse matrix
Factorization
6
[Wassenberg et al., 2008]
Image processing
Labeling
8
[Wu et al., 2009]
Image processing
Labeling
3
[Hynes, 1998]
Graphs
Connected components
18
[Osipov et al., 2009]
Graphs
Minimum spanning tree
2
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
# of Algorithms
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Outline
Introduction
Applications and Definitions
Main Operations (Union-Find)
Variations of Classical Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Implementation Enhancements
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
The Fastest Algorithms
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Disjoint-Set Data Structure: Definitions
I
U ⇒ set of n elements and Si ⇒ a subset of U.
I
S1 and S2 are disjoint if S1 ∩ S2 = ∅.
I
Maintains a dynamic collection S1 , S2 , . . . Sk of disjoint sets
which together cover U.
I
Each set is identified by a representative x.
I
A set of algorithms that operate on this data structure is
often referred to as a Union-Find algorithm.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Main Operations
I
Each set is represented by a rooted
tree, pointer towards root.
I
The element in the root node is
the representative of the set.
I
I
Parent pointer p(x) denotes the
parent of node x.
Two main operations.
I
I
a
d
b
c
e
Figure: Si = {a, b, c, d, e}.
Find(x).
Union(x, y ).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Find(x)
a
b
I
I
To which set does a given element
x belong ⇒ Find(x).
c
Returns the root (representative)
of the set that contain x.
d
e
Figure: Find(d).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Union(x, y )
I
I
a
Create a new set from the union of
two existing sets containing x and
y ⇒ Union(x, y ).
Change the parent pointer of one
root to the other one.
f
d
b
c
g
e
Figure: Union(c, g ).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Union(x, y )
I
I
a
Create a new set from the union of
two existing sets containing x and
y ⇒ Union(x, y ).
Change the parent pointer of one
root to the other one.
f
d
b
c
g
e
Figure: Union(c, g ).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Applications and Definitions
Main Operations (Union-Find)
Use of Union-Find for Computing Connected Components: G = (V , E )
1: S ← ∅
2: for each x ∈ V do
3:
Makeset(x)
4: for each (x, y ) ∈ E do
5:
if Find(x) 6= Find(y ) then
6:
Union(x, y )
7:
S ← S ∪ {(x, y )}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
I
Note that if the edges are
processed by increasing
weight then this algorithm is
Kruskal’s algorithm.
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Union Techniques
I
Naive-Link (nl)
I
Link-by-Size (ls)
I
Link-by-Rank (lr)
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Union Techniques : Link-by-Rank (lr)
I
Each set maintains a
rank value, intially 0.
I
Lowest ranked root
⇒ higher ranked root.
I
Equal ranked roots ⇒
root of the combined
tree is increased by 1.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
2 a
f 1
d
b
c
g
e
Figure: Union.
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Union Techniques : Link-by-Rank (lr)
I
Each set maintains a
rank value, intially 0.
I
Lowest ranked root
⇒ higher ranked root.
I
Equal ranked roots ⇒
root of the combined
tree is increased by 1.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
2 a
f 1
d
b
c
g
e
Figure: Union.
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Union Techniques : Link-by-Rank (lr)
I
Each set maintains a
rank value, intially 0.
I
Lowest ranked root
⇒ higher ranked root.
I
Equal ranked roots ⇒
root of the combined
tree is increased by 1.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
2 a
f 2
d
b
c
e
g
h
Figure: Union.
Experiments on Union-Find Algorithms
i
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Union Techniques : Link-by-Rank (lr)
I
Each set maintains a
rank value, intially 0.
I
Lowest ranked root
⇒ higher ranked root.
I
Equal ranked roots ⇒
root of the combined
tree is increased by 1.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
3 2
a
f 2
d
b
c
e
g
h
Figure: Union.
Experiments on Union-Find Algorithms
i
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques
I
Naive-Find (nf)
I
Path-Compression (pc)
I
Path-Splitting (ps)
I
Path-Halving (ph)
I
Type-0-Reversal (r0)
I
Type-1-Reversal (r1)
I
Collapsing (co)
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques
a
I
Reduce the height of the tree
during the Find operation.
I
Subsequent Find operations
require less time.
I
Find-path of a node x is the path
of parent pointers from x upto the
root of the tree.
b
c
d
e
Figure: Find-path(d).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Path-Compression (pc)
a
I
I
Set the parent
pointers of all nodes
in the find path to
the root.
Need to traverse the
find-path twice.
b
c
d
e
Figure: Find(e) with pc
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Path-Compression (pc)
a
I
I
Set the parent
pointers of all nodes
in the find path to
the root.
Need to traverse the
find-path twice.
a
b
b
c
c
d
e
d
e
Figure: Find(e) with pc
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Path-Halving (ph)
a
I
I
Set the parent
pointers of every
other nodes in the
find-path to its
grandparent.
Traverse the find-path
once.
b
c
d
e
Figure: Find(e) with ph
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Path-Halving (ph)
a
I
I
Set the parent
pointers of every
other nodes in the
find-path to its
grandparent.
Traverse the find-path
once.
a
b
b
c
c
d
e
d
e
Figure: Find(e) with ph
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Collapsing (co)
a
I
Every node points
directly to the root.
I
Find operation takes
constant time.
I
In a Union
operation, all nodes
of one tree point to
the root of other tree.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
b
c
e
d
f
Figure: co
Experiments on Union-Find Algorithms
g
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Compression Techniques : Collapsing (co)
a
I
Every node points
directly to the root.
I
Find operation takes
constant time.
I
In a Union
operation, all nodes
of one tree point to
the root of other tree.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
b
c
e
d
a
b
c
f
g
e
d
f
Figure: co
Experiments on Union-Find Algorithms
g
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Worst Case Complexity
I
For any combination of m Makeset, Union and Find
operations on n elements.
Union
Compression
Complexity
nl
nf
O(mn)
nl
pc, ph, ps
O(m log(1+m/n) n)
nl
co
O(m + n2 )
nl, lr, ls
r0, r1
O(n + m log n)
lr, ls
co
O(m + n log n)
lr, ls
pc, ph, ps
O(m · α(m, n))
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Interleaved (Int) Algorithm
I
During a Union operation, the two Find are performed as a
single interleaved operation.
I
The first Int algorithm is Rem’s algorithm [Dijkstra, 1976].
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm [Dijkstra, 1976]
I
Each node has a unique identifier ⇒ index of the node.
I
Lowered numbered node points to higher numbered node or
to itself (if it is a root).
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Example - Edge (x, y )
ry
rx
x u
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
v y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
p(u) < p(v)
x u
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
v y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
u
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
v y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
u
p(u) > p(v)
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
v y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
u
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
v
y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
p(u) < p(v)
v
u
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
v
u
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Different Sets - Compare ID of Parents
ry
rx
v
u
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
y
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Same Set - Edge (x, y )
r
y
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Same Set - Edge (x, y )
r
u
v
y
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Same Set - Edge (x, y )
r
u
v
y
x
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Compression
Splicing (sp)
I
Set parent pointer to a higher
valued node ⇒ compressing the
tree.
I
Intuition: Higher valued node
should be closer to the root.
I
The running time of remsp is
O(m log(2+m/n) n).
20
18
17
15
14
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
13
u
12
v
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Compression
Splicing (sp)
I
Set parent pointer to a higher
valued node ⇒ compressing the
tree.
I
Intuition: Higher valued node
should be closer to the root.
I
The running time of remsp is
O(m log(2+m/n) n).
20
18
17
15
14
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
p(u) < p(v)
13
u
12
v
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Compression
Splicing (sp)
I
Set parent pointer to a higher
valued node ⇒ compressing the
tree.
I
Intuition: Higher valued node
should be closer to the root.
I
The running time of remsp is
O(m log(2+m/n) n).
20
18
17
15
14
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
13
u
12
v
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Compression
Splicing (sp)
I
Set parent pointer to a higher
valued node ⇒ compressing the
tree.
I
Intuition: Higher valued node
should be closer to the root.
I
The running time of remsp is
O(m log(2+m/n) n).
20
18
17
15
14
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
13
p(u) < p(v)
u
12
v
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
The rem Algorithm: Compression
Splicing (sp)
I
Set parent pointer to a higher
valued node ⇒ compressing the
tree.
I
Intuition: Higher valued node
should be closer to the root.
I
The running time of remsp is
O(m log(2+m/n) n).
20
18
17
15
u
14
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
13
12
v
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
A Variation of rem: “tvl”
Union Techniques
Compression Techniques
Interleaved Algorithms
[Tarjan and van Leeuwen, 1984]
I
Uses ranks rather than identifier.
I
This algorithms is slightly more complicated than rem.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Test Sets and Experimental Setup
I
I
Dell computer, Intel Core 2 CPU (2.40 GHz), Fedora 10,
C++ and GCC (-O3).
Three test sets.
1. rw: 9 real world graphs.
I
I
I
Linear programming, Medical science.
Structural engineering, Civil engineering.
Automotive industry.
2. sw: 5 synthetic small world graphs .
3. er: 6 synthetic Erdös-Rényi random graphs.
I
For each graph: 5 runs with 5 different random orderings of
edges.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Structural Properties of the Input Graphs
Avg Deg
# Edges
Processed
236
3,422
842
76
34
80
80
344
6,140
99
221
72
48
34
60
72
81
282
692,208
803,719
5,526,149
7,442,413
9,160,083
11,393,426
35,339,811
10,816,880
28,262,657
17,233
9,467
34,465
43,931
68,930
6,241
8,624
10,470
14,216
16,462
276
321
331
308
340
6,897,769
12,039,043
16,539,557
26,985,391
34,014,275
24
1
8
31
1
1
25
61
30
31
70
76
9
33
12
11
38
41
453,803
603,141
2,904,660
5,645,880
3,476,740
7,347,376
|V |
|E |
97,578
63,838
503,712
952,203
1,508,065
914,898
986,703
943,695
321,696
4,827,996
7,042,510
18,156,315
22,785,136
25,582,130
27,276,762
35,339,811
38,354,076
45,429,789
1
1
1
1
1
1
2
1
1
sw1
sw2
sw3
sw4
sw5
50,000
75,000
100,000
175,000
200,000
6,897,769
12,039,043
16,539,557
26,985,391
34,014,275
er1
er2
er3
er4
er5
er6
100,000
100,000
500,000
1,000,000
500,000
1,000,000
453,803
1,650,872
2,904,660
5,645,880
9,468,353
20,287,048
Graph
rw1
rw2
rw3
rw4
rw5
rw6
rw7
rw8
rw9
(m t1)
(crankseg 2)
(inline 1)
(ldoor)
(af shell10)
(boneS10)
(bone010)
(audikw 1)
(spal 004)
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Comp
Max Deg
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: 29 variations of classical algorithms. Each cell is an algorithm.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
X dominates Y
I
An algorithm X dominates another algorithm Y if X performs
at least as well as Y .
I
Since lrpc and lrph are generally accepted as best, we
begin by examining these.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê lrph2
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: 29 variations of classical algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê lrph2
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: lrpc dominates 14 algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê lrph2
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: lrph dominates 3 additional, including lrpc - Total 17.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê lrph2
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: remsp dominates 10 of remaining, including lrph - Total 27.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques
Compression Techniques
Interleaved Algorithms
Relative performance of the classical algorithms
CL
Union
Figure
Compression Technique
nf
pc
ph
ps
co
r0
r1
sp
nl
lrpc1
lrph2
remsp3
remsp3
lrpc1
lrpc1
lrpc1
lr
lrpc1
Ê lrph2
Ë remsp3
remsp3
remsp3
lrpc1
lrpc1
ls
lrpc1
lrpc1
remsp3
remsp3
remsp3
lrpc1
lrpc1
rem
lrpc1
remsp3
undom.
Ì undom.
tvl
lrpc1
lrpc1
remsp3
lrph2
Table: Only 2 algorithms are undominated.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Implementation Enhancements
I
I
Ways to make the classical algorithms faster.
Enhancements:
1. Immediate parent check [Osipov et al., 2009].
2. Better interleaved algorithms [Manne and Patwary, 2009].
3. Memory efficient algorithms, Reduce memory usage.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 1: Immediate Parent Check (ipc) [Osipov et al., 2009]
I
ipc applies to any classical algorithm (rem already
implements ipc).
I
{ipc} X {lr, ls} X {pc, ph, ps}
I
{ipc} X {tvl} X {pc, ps, sp}
I
9 more variations.
I
remsp dominates all 9 variations.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 2: Better Interleaved Algorithms (Int) [Manne and Patwary, 2009]
I
Better interleaved algorithms than tvl ⇒ etvl, zz.
I
{etvl} X {pc, ps, sp}
I
{zz} X {pc, ps}
I
5 more variations.
I
remsp dominates all 5 variations.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 3: Memory Efficient (ms) Algorithms
I
Reduce memory used by each algorithm.
I
{ms} X {nl} X {pc, ps}.
I
{ms} X {lr, ls} X {pc, ps, co}.
I
{ms} X {ipc} X {lr, ls} X {pc, ps}.
I
12 more variations.
I
Note that rem does not use size or rank, so it is
automatically an ms algorithm.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 3: ms relative performance
ms-Union
Method
pc
Compression Technique
ps
co
nl
remsp3
remsp3
lr
remsp3 ↑
undom.↑
undom.↑
ls
remsp3 ↑
undom. ↑
undom. ↑
ipc-lr
undom.↑
undom.
ipc-ls
remsp3
remsp3
rem
remsp3 ≡
undom. ≡
sp
Ì undom. ≡
Table: 12 more variations. Shaded row has already been considered.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 3: ms relative performance
ms-Union
Method
pc
Compression Technique
ps
co
nl
remsp3
remsp3
lr
remsp3 ↑
undom.↑
undom.↑
ls
remsp3 ↑
undom. ↑
undom. ↑
ipc-lr
undom.↑
undom.
ipc-ls
remsp3
remsp3
rem
remsp3 ≡
undom. ≡
sp
Ì undom. ≡
Table: remsp dominates 6 algorithms - Total 47 of 55.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
1. Immediate Parent Check [Osipov et al., 2009]
2. Better Interleaved Algorithms [Manne and Patwary, 2009]
3. Memory Efficient Algorithms
Enhancement 3: ms relative performance
ms-Union
Method
pc
Figure
Compression Technique
ps
co
nl
remsp3
remsp3
lr
remsp3 ↑
undom.↑
undom.↑
ls
remsp3 ↑
undom. ↑
undom. ↑
ipc-lr
undom.↑
undom.
ipc-ls
remsp3
remsp3
rem
remsp3 ≡
undom. ≡
sp
Ì undom. ≡
Table: 6 algorithms are undominated - Total 8 undominated of 55.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
The Fastest Algorithms
I
Different metric than the dominates technique.
I
Fictious algorithm (Global-Min) ⇒ run-time equal to the
best of any algorithm for each graph.
For each algorithm
I
1. Compute average relative time for each graph.
2. Compute average relative time for each type of graph (rw, sw
and er).
3. Compute average of the three types ⇒ final average.
I
Rank the order of the algorithms based on final averages.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Rank order of the fastest algorithms
Algorithm
remsp
remps
ms-ipc-lrpc
ms-lsps
ms-ipc-lspc
ms-lrps
ms-ipc-lrps
ms-lsco
ms-lrco
ms-ipc-lsps
All graphs
Figure
Rank based on graphs of type
Real-World
Small-World
1
2
3
4
5
6
7
8
9
10
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
1
5
6
2
8
4
3
9
10
7
Erdős-Rényi
1
2
3
10
4
13
11
6
5
15
Experiments on Union-Find Algorithms
1
4
7
2
8
3
5
9
10
6
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
The Fastest Algorithms: Observations
1. remsp
2. remps
3. ms-ipc-lrpc
I
All the top 10 algorithms use ms
enhancement.
4. ms-lsps
I
8 out of top 10 are one pass algorithms.
6. ms-lrps
I
Out of the top 5 algorithms, 2 uses pc.
7. ms-ipc-lrps
I
lrpc, lrph are not in top 10.
8. ms-lsco
5. ms-ipc-lspc
9. ms-lrco
10. ms-ipc-lsps
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Related Experimental Studies: Improvement
Reference
# of
Algorithms
Recommended
Algorithm
remsp
improves by
[Liu, 1990]
2
nlpc
56%
[Gilbert et al., 1994]
6
nlph
45%
[Wassenberg et al., 2008]
8
lrco
24%
[Wu et al., 2009]
3
lipc
48%
[Hynes, 1998]
18
lico, lsco
28%, 24%
[Osipov et al., 2009]
2
ipc-lrpc
29%
-
-
lrpc
52%
-
-
lrph
28%
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Future Works
I
Extend to other application areas.
I
Consider arbitrary sequences of intermixed Makeset,
Union, and Find operations.
I
More formal profiling including cache misses, pointer jumps,
number of comparisons etc.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Thank you.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Best Enhanced Classical Algorithm
110
I
I
Percent of MS−IPC−LRPC time
MS−IPC−LRPC
Best enhanced
classical algorithm is
ms-ipc-lrpc.
remsp improved over
ms-ipc-lrpc : 12%
(-3%-18%)
RemPS
105
RemSP
100
95
90
85
80
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: Improvement over ms-ipc-lrpc.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Naive-Link (nl)
a
I
I
Arbitrarily choose one
root to point to the
other one.
Height of the tree
could be O(n).
f
d
b
c
g
e
Figure: Union.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Naive-Link (nl)
a
I
I
Arbitrarily choose one
root to point to the
other one.
Height of the tree
could be O(n).
f
d
b
c
g
e
Figure: Union.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Naive-Link (nl)
a
I
I
Arbitrarily choose one
root to point to the
other one.
Height of the tree
could be O(n).
f
d
b
c
g
e
Figure: Union.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Link-by-Size (ls)
I
I
5 a
Root of tree with
fewer nodes points to
the other root.
To implement ls
efficiently, size of
each tree is
maintained in the
root.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
f 4
d
b
c
e
g
h
Figure: Union.
Experiments on Union-Find Algorithms
i
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Link-by-Size (ls)
I
I
9 5
a
Root of tree with
fewer nodes points to
the other root.
To implement ls
efficiently, size of
each tree is
maintained in the
root.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
f 4
d
b
c
e
g
h
Figure: Union.
Experiments on Union-Find Algorithms
i
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Link-by-Size (ls)
I
I
5 a
Root of tree with
fewer nodes points to
the other root.
To implement ls
efficiently, size of
each tree is
maintained in the
root.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
f 6
d
b
c
k
h
g
e
Figure: Union.
Experiments on Union-Find Algorithms
i
j
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Union Techniques : Link-by-Size (ls)
I
I
To implement ls
efficiently, size of
each tree is
maintained in the
root.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
11
f 6
k
5 a
Root of tree with
fewer nodes points to
the other root.
d
b
c
h
g
e
Figure: Union.
Experiments on Union-Find Algorithms
i
j
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
How much improvement
I
remsp substantially outperforms lrpc even though
theoritically inferior.
I
lrpc ⇒ real world, algorithm courses, libraries.
I
remsp improved over lrpc: 52% (38%-66%)
I
remsp improved over lrph: 28% (15%-45%)
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Relative Performance of the Classical Algorithms
1100
1000
Percent of LRPC time
900
LRR1
NLR0
LRNF
LRR0
TvLNL
LSNF
LSR0
RemNF
LSPC
NLR1
LSR1
TvLPC
LRPC
800
700
600
500
400
300
200
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: lrpc dominates 14 algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Relative Performance of the Classical Algorithms
Back
280
260
LRPC
TvLSP
NLPC
LRPH
Percent of LRPH time
240
220
200
180
160
140
120
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: lrph dominates 3 additional algorithms, including lrpc
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Relative Performance of the Classical Algorithms
300
Percent of RemSP time
280
260
NLPH
RemPC
LSPS
NLPS
LRPH
LRCO
TvLPS
LSPH
LSCO
LRPS
RemSP
240
220
200
180
160
140
120
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: remsp dominates 10 remaining algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Relative Performance of the Classical Algorithms
120
RemPS
118
RemSP
Percent of CL−Min time
116
114
112
110
108
106
104
102
100
98
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: Only 2 algorithms are undominated.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 1: ipc Relative Performance
Percent of IPC−LRPS time
200
IPC−TvLPC
IPC−LRPC
IPC−LRPH
IPC−LRPS
Back
180
160
140
120
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: ipc-lrps dominates 3 algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 1: ipc Relative Performance
Back
260
Percent of RemSP time
240
IPC−LSPH
IPC−LSPS
IPC−TvLPS
IPC−LSPC
IPC−LRPS
IPC−TvLSP
RemSP
220
200
180
160
140
120
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: remsp dominates 6 remaining algorithms, including ipc-lrps
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 2: Int Relative Performance
INT
Percent of RemSP time
250
ZZPC
ZZPS
eTvLPS
eTvLPC
eTvLSP
RemSP
200
150
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: remsp dominates all the 5 variations.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 3: ms Relative Performance
300
MS−NLPC
MS−LSPC
MS−IPC−LSPS
MS−NLPS
MS−LRPC
MS−IPC−LSPC
MS
Percent of RemSP time
RemSP
250
200
150
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
Graphs
Figure: remsp dominates 6 algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
The Fastest Algorithms
Back
160
160
MS−IPC−LRPC
MS−IPC−LSPC
RemPS
RemSP
140
130
120
150
Percent of Global−Min time
Percent of Global−Min time
150
MS−LSPS
MS−IPC−LSPS
MS−LSCO
MS−LRCO
MS−IPC−LRPS
MS−LRPS
140
130
120
110
110
100
100
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
sw1 sw2 sw3 sw4 sw5
er1 er2 er3 er4 er5 er6
rw1 rw2 rw3 rw4 rw5 rw6 rw7 rw8 rw9
Graphs
sw1 sw2 sw3 sw4 sw5
Graphs
Figure: Top 10 algorithms.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
er1 er2 er3 er4 er5 er6
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
B
C
E
D
A
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
A
B
C
D
E
B
C
E
D
A
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
I
A
B
C
D
(C, D)
E
B
C
E
D
A
S = { emptyset}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
I
A
B
C
D
(C, D) ⇒ Find(C ), Find(D)
E
B
C
E
D
A
S = { emptyset}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Back
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
I
D
A
B
C
E
B
C
E
D
(C, D) ⇒ Union(C , D)
A
S = {(C, D)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
E
D
I
(C, D)
I
(B, C)
A
S = {(C, D)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
E
D
I
(C, D)
I
(B, C) ⇒ Find(B), Find(C )
A
S = {(C, D)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Back
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
E
D
I
(C, D)
I
(B, C) ⇒ Union(B, C )
A
S = {(C, D), (B, C)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
E
D
I
(C, D)
I
(B, C)
I
(B, D)
A
S = {(C, D), (B, C)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Back
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
E
D
I
(C, D)
I
(B, C)
I
(B, D) ⇒ Find(B), Find(D)
A
S = {(C, D), (B, C)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Back
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components: Example:
D
A
B
C
E
B
C
A
E
I
(C, D)
I
(B, C)
I
(B, D)
I
(A, B)
I
(A, E)
I
(B, E)
I
(D, E)
Back
D
S = {(C, D), (B, C), (A, B), (A, E)}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Bibliography I
Dijkstra, E. W. (1976).
A Discipline of Programming.
Prentice-Hall.
Galil, Z. and Italiano, G. F. (1991).
Data structures and algorithms for disjoint set union problems.
ACM Comput. Surv., 23(3):319–344.
Gilbert, J. R., Ng, E. G., and Peyton, B. W. (1994).
An efficient algorithm to compute row and column counts for
sparse Cholesky factorization.
SIAM J. Matrix Anal. Appl., 15(4):1075–1091.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Bibliography II
Hynes, R. (1998).
A new class of set union algorithms.
Master’s thesis, Department of Computer Science, University
of Toronto, Canada.
Liu, J. W. H. (1990).
The role of elimination trees in sparse factorization.
SIAM J. Matrix Anal. Appl., 11:134–172.
Manne, F. and Patwary, M. M. A. (2009).
A scalable parallel union-find algorithm for distributed memory
computers.
To appear in the proceedings of PPAM 2009, LNCS.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Bibliography III
Osipov, V., Sanders, P., and Singler, J. (2009).
The filter-Kruskal minimum spanning tree algorithm.
In ALENEX, pages 52–61.
Tarjan, R. E. and van Leeuwen, J. (1984).
Worst-case analysis of set union algorithms.
J. ACM, 31(2):245–281.
Wassenberg, J., Bulatov, D., Middelmann, W., and Sanders,
P. (2008).
Determination of maximally stable extremal regions in large
images.
In From Proceeding of Signal Processing, Pattern Recognition,
and Applications (SPPRA). Acta Press.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Bibliography IV
Wu, K., Otoo, E., and Suzuki, K. (2009).
Optimizing two-pass connected-component labeling
algorithms.
Pattern Anal. Appl., 12(2):117–135.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Use of Union-Find for Computing Connected Components:
Quick-Union(x, y )[Galil and Italiano, 1991]
1: S ← ∅
2: for each x ∈ V do
3:
Makeset(x)
4: for each (x, y ) ∈ E do
5:
if Find(x) 6= Find(y ) then
6:
Union(x, y )
7:
S ← S ∪ {(x, y )}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
1:
2:
3:
4:
5:
6:
7:
8:
rx ← x, ry ← y
while rx 6= p(rx ) do
rx ← p(rx )
while ry 6= p(ry ) do
ry ← p(ry )
if rx 6= ry then
p(rx ) ← ry
S ← S ∪ {(x, y )}
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Quick-Union [Galil and Italiano, 1991]
1: S ← ∅
2: for each x ∈ V do
3:
Makeset(x)
4: for each (x, y ) ∈ E do
5:
if Find(x) 6= Find(y ) then
6:
Union(x, y )
7:
S ← S ∪ {(x, y )}
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
I
Store the results of the two
Find operations.
I
Use them as input to the
Union operation.
I
Speed up the algorithm →
Quick-Union.
I
Stop when |S| = (|V | − 1),
indicates no more edges will
be added to S.
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : Naive-Find (nf)
I
Find operation with
no compression.
I
Find is time
consuming.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : Path-Splitting (ps)
a
I
I
Set the parent
pointers of all nodes
in the find-path to its
grandparent.
Traverse the find-path
once.
b
c
d
e
Figure: Find(e) with ps
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : Path-Splitting (ps)
a
I
I
Set the parent
pointers of all nodes
in the find-path to its
grandparent.
Traverse the find-path
once.
a
b
b
c
c
d
e
d
e
Figure: Find(e) with ps
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : r0
a
I
I
All nodes except the
last k nodes on the
find-path point to the
first node.
Need to traverse the
find-path once.
b
c
d
e
Figure: Find(e) with r0
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : r0
a
I
I
All nodes except the
last k nodes on the
find-path point to the
first node.
Need to traverse the
find-path once.
a
b
b
c
c
d
e
d
e
Figure: Find(e) with r0
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Compression Techniques : r1
a
I
I
All nodes except the
last k nodes on the
find-path point to the
first node.
Need to traverse the
find-path once.
a
b
b
c
c
d
e
d
e
Figure: Find(e) with r1
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Algorithm Count
I
The running time of remsp is O(m log(2+m/n) n).
I
The running time of tvlsp is O(m · α(m, n)).
I
{rem, tvl} X {nl, pc, ps, sp} but not with {ph, r0, r1}.
I
8 more algorithms.
I
Total algorithm: 21 + 8 = 29 algorithms SO FAR.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Test Sets and Experimental Setup. . .
I
I
For each graph: 5 runs with 5 different random orderings.
Compute the average of these runtimes.
I
I
rw: 25 total runs.
sw and er: 125 total runs since each graph has 5 different
instances.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 3: Memory Efficient (ms) Algorithms
I
I
Reduce memory used by each algorithm.
Each node has:
I
I
I
I
a parent pointer
either a size or rank for some algorithms
a sibling pointer for co algorithm.
Thus have one to three fields in each record.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms
Introduction
Variations of Classical Algorithms
Implementation Enhancements
The Fastest Algorithms
Enhancement 3: Memory Efficient (ms) Algorithms
I
Save one field by coding the size or rank of the root into the
parent pointer.
I
{ms} X {nl} X {pc, ps}.
I
{ms} X {lr, ls} X {pc, ps, co}.
I
{ms} X {ipc} X {lr, ls} X {pc, ps}.
I
12 more variations.
I
Total algorithm: 43 + 12 = 55 variations.
I
Note that rem does not use size or rank, so it is
automatically an ms algorithm.
Md. Mostofa Ali Patwary, Jean Blair, and Fredrik Manne
Experiments on Union-Find Algorithms