Professional Documents
Culture Documents
1 UnionFind PDF
1 UnionFind PDF
Mathematical analysis.
2
1.5 U NION -F IND
‣ dynamic connectivity
‣ quick find
‣ quick union
Algorithms
‣ improvements
union(9, 4)
union(2, 1)
5 6 7 8 9
connected(0, 7) 𐄂
connected(8, 9) ✔
union(5, 0)
union(7, 2)
union(6, 1)
union(1, 0)
connected(0, 7) ✔
4
Connectivity example
A. Yes.
5
Modeling the objects
6
Modeling the connections
0 1 2 3
4 5 6 7
{ 0 } { 1 4 5 } { 2 3 6 7 }
3 connected components
7
Implementing the operations
0 1 2 3 0 1 2 3
union(2, 5)
4 5 6 7 4 5 6 7
{ 0 } { 1 4 5 } { 2 3 6 7 } { 0 } { 1 2 3 4 5 6 7 }
public class UF
9
Dynamic-connectivity client
10
1.5 U NION -F IND
‣ dynamic connectivity
‣ quick find
‣ quick union
Algorithms
‣ improvements
0 1 2 3 4
5 6 7 8 9
13
Quick-find [eager approach]
Data structure.
・Integer array id[] of length N.
・Interpretation: p and q are connected iff they have the same id.
0 1 2 3 4 5 6 7 8 9
id[] 0 1 1 8 8 0 0 1 8 8
0 1 2 3 4 5 6 7 8 9
after union of 6 and 1
id[] 1 1 1 8 8 1 1 1 8 8
0 1 2 3 4
5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
id[] 0 1 2 3 4 5 6 7 8 9
15
Quick-find demo
0 1 2 3 4
5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
id[] 1 1 1 8 8 1 1 1 8 8
Quick-find: Java implementation
public QuickFindUF(int N)
{
id = new int[N];
set id of each object to itself
for (int i = 0; i < N; i++) (N array accesses)
id[i] = i;
}
check whether p and q
public boolean connected(int p, int q) are in the same component
{ return id[p] == id[q]; } (2 array accesses)
quick-find N N 1
quadratic
18
Quadratic algorithms do not scale
・
linearithmic
With quadratic algorithm, takes 10x as long! 8T
linear
size 1K 2K 4K 8K
19
1.5 U NION -F IND
‣ dynamic connectivity
‣ quick find
‣ quick union
Algorithms
‣ improvements
Data structure.
・Integer array id[] of length N. keep going until it doesn’t change
・Root of i is id[id[id[...id[i]...]]]. 0 1 9 6 7 8
0 1 2 3 4 5 6 7 8 9
2 4 5
id[] 0 1 9 4 9 6 6 7 8 9
root of 3 is 9
22
Quick-union [lazy approach]
Data structure.
・Integer array id[] of length N.
・Interpretation: id[i] is parent of i.
・Root of i is id[id[id[...id[i]...]]]. 0 1 9 6 7 8
0 1 2 3 4 5 6 7 8 9
2 4 5 q
id[] 0 1 9 4 9 6 6 7 8 9
p 3
0 1 2 3 4 5 6 7 8 9 9
5 q
id[] 0 1 9 4 9 6 6 7 8 6
2 4
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
id[] 0 1 2 3 4 5 6 7 8 9
24
Quick-union demo
1 3 9
0 2 7 4
0 1 2 3 4 5 6 7 8 9
id[] 1 8 1 8 3 0 5 1 8 8
6
Quick-union: Java implementation
public QuickUnionUF(int N)
{
id = new int[N]; set id of each object to itself
for (int i = 0; i < N; i++) id[i] = i; (N array accesses)
}
quick-find N N 1
Quick-find defect.
・Union too expensive (N array accesses).
・Trees are flat, but too expensive to keep them flat.
Quick-union defect.
・Trees can get tall.
・Find too expensive (could be N array accesses).
27
1.5 U NION -F IND
‣ dynamic connectivity
‣ quick find
‣ quick union
Algorithms
‣ improvements
Weighted quick-union.
・Modify quick-union to avoid tall trees.
・Keep track of size of each tree (number of objects).
・Balance by linking root of smaller tree to root of larger tree.
reasonable alternatives:
quick-union q union by height or "rank"
p
smaller
p tree q
smaller larger
tree tree
might put the
larger larger tree lower
tree
weighted
p always chooses the p
better alternative
q q
larger smaller smaller larger
tree tree tree tree
Weighted quick-union
30
Weighted quick-union demo
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
id[] 0 1 2 3 4 5 6 7 8 9
31
Weighted quick-union demo
4 0 2 5
3 8 9 1 7
0 1 2 3 4 5 6 7 8 9
id[] 6 2 6 4 6 6 6 2 4 4
Quick-union and weighted quick-union example
quick-union
33
Weighted quick-union: Java implementation
34
Weighted quick-union analysis
Running time.
・Find: takes time proportional to depth of p and q.
・Union: takes constant time, given roots. lg = base-2 logarithm
N = 10
depth(x) = 3 ≤ lg N
35
Weighted quick-union analysis
Running time.
・Find: takes time proportional to depth of p and q.
・Union: takes constant time, given roots.
Proposition. Depth of any node x is at most lg N.
Pf. When does depth of x increase?
Increases by 1 when tree T1 containing x is merged into another tree T2.
・The size of the tree containing x at least doubles since | T | ≥
2 | T 1 |.
・Size of tree containing x can double at most lg N times. Why?
T2
T1
x
36
Weighted quick-union analysis
Running time.
・Find: takes time proportional to depth of p and q.
・Union: takes constant time, given roots.
Proposition. Depth of any node x is at most lg N.
quick-find N N 1
quick-union N N † N
weighted QU N lg N † lg N
Quick union with path compression. Just after computing the root of p,
set the id of each examined node to point to that root.
0 root
1 2
3 4 5
6 7
p
8 x 9
10 11 12
38
Improvement 2: path compression
Quick union with path compression. Just after computing the root of p,
set the id of each examined node to point to that root.
0 root
p
9 1 2
11 12 3 4 5
x 6 7
10
39
Improvement 2: path compression
Quick union with path compression. Just after computing the root of p,
set the id of each examined node to point to that root.
0 root
p
9 6 1 2
11 12 8 x 3 4 5
10 7
40
Improvement 2: path compression
Quick union with path compression. Just after computing the root of p,
set the id of each examined node to point to that root.
0 root
p
9 6 3 x 1 2
11 12 8 7 4 5
10
41
Improvement 2: path compression
Quick union with path compression. Just after computing the root of p,
set the id of each examined node to point to that root.
x
0 root
p
9 6 3 1 2
11 12 8 7 4 5
10
42
Path compression: Java implementation
Simpler one-pass variant: Make every other node in path point to its
grandparent (thereby halving path length).
i = id[i];
}
return i;
}
65536 4
265536 5
quick-find MN
quick-union MN
weighted QU N + M log N
・Percolation.
・Games (Go, Hex).
✓ Dynamic connectivity.
・Least common ancestor.
・Equivalence of finite state automata.
・Hoshen-Kopelman algorithm in physics.
・Hinley-Milner polymorphic type inference.
・Kruskal's minimum spanning tree algorithm.
・Compiling equivalence statements in Fortran.
・Morphological attribute openings and closings.
・Matlab's bwlabel() function in image processing.
48
Percolation
open
site
open site connected to top
N=8 no open site connected to top
49
Percolation
50
Likelihood of percolation
51
Percolation phase transition
percolation
probability
p*
0
0 0.593 1
N = 100
site vacancy probability p
52
Monte Carlo simulation
blocked site
N = 20
53
Dynamic connectivity solution to estimate percolation threshold
N=5
open site
blocked site
54
Dynamic connectivity solution to estimate percolation threshold
N=5 0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
open site
blocked site
55
Dynamic connectivity solution to estimate percolation threshold
N=5
open site
blocked site
56
Dynamic connectivity solution to estimate percolation threshold
bottom row
open site
blocked site
57
Dynamic connectivity solution to estimate percolation threshold
Clever trick. Introduce 2 virtual sites (and connections to top and bottom).
・Percolates iff virtual top site is connected to virtual bottom site.
efficient algorithm: only 1 call to connected()
bottom row
open site
virtual bottom site
blocked site
58
Dynamic connectivity solution to estimate percolation threshold
N=5
open site
blocked site
59
Dynamic connectivity solution to estimate percolation threshold
up to 4 calls to union()
N=5
open site
blocked site
60
Percolation threshold
percolation
probability
p*
0
0 0.593 1
N = 100
site vacancy probability p
Mathematical analysis.
63
Algorithms R OBERT S EDGEWICK | K EVIN W AYNE