Professional Documents
Culture Documents
15 Union Find
15 Union Find
Algorithms
F O U R T H
E D I T I O N
quick union
improvements
applications
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Dynamic connectivity
Given a set of N objects.
union(9, 4)
union(2, 1)
connected(0, 7)
connected(8, 9)
union(5, 0)
union(7, 2)
union(6, 1)
union(1, 0)
connected(0, 7)
Connectivity example
Q. Is there a path connecting p and q ?
A. Yes.
5
Reflexive: p is connected to p.
Symmetric: if p is connected to q, then q is connected to p.
Transitive: if p is connected to q and q is connected to r,
then p is connected to r.
Connected components. Maximal set of objects that are mutually
connected.
0
{ 0 } { 1 4 5 } { 2 3 6 7 }
3 connected components
7
union(2, 5)
4
{ 0 } { 1 4 5 } { 2 3 6 7 }
3 connected components
{ 0 } { 1 2 3 4 5 6 7 }
2 connected components
8
Dynamic-connectivity client
% more tinyUF.txt
10
4 3
3 8
6 5
9 4
2 1
8 9
5 0
7 2
6 1
1 0
6 7
10
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
if and only if
13
id[6] = 0; id[1] = 1
6 and 1 are not connected
id[]
Quick-find demo
id[]
15
Quick-find demo
id[]
}
}
17
algorithm
initialize
union
find
quick-find
quadratic
18
a truism (roughly)
since 1950!
18
time
quadratic
64T
32T
16T
linearithmic
8T
size
linear
1K 2K
4K
8K
19
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
3
root of 3 is 9
22
root of 3 is 9
root of 5 is 6
id[]
9
5
2
3
23
Quick-union demo
id[]
9
24
Quick-union demo
id[]
}
26
algorithm
initialize
union
find
quick-find
quick-union
worst case
Quick-find defect.
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Improvement 1: weighting
Weighted quick-union.
q
p
larger
tree
smaller
tree
reasonable alternatives:
union by height or "rank"
q
smaller
tree
larger
tree
weighted
larger
tree
smaller
tree
smaller
tree
larger
tree
Weighted quick-union
30
id[]
31
id[]
quick-union
weighted
33
lg = base-2 logarithm
x
N = 10
depth(x) = 3 lg N
35
| T 1 |.
T2
T1
x
36
algorithm
initialize
union
connected
quick-find
quick-union
weighted QU
lg N
N
lg N
root
p
8
10
11
12
38
root
p
9
11
12
10
39
root
p
6
11
12
10
40
root
p
6
11
12
10
41
root
p
6
11
12
10
42
lg* N
16
65536
265536
Summary
Bottom line. Weighted quick union (with path compression) makes it
possible to solve problems that could not otherwise be addressed.
algorithm
worst-case time
quick-find
MN
quick-union
MN
weighted QU
N + M log N
QU + path compression
N + M log N
N + M lg* N
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
Union-find applications
Percolation.
Games (Go, Hex).
Dynamic connectivity.
48
Percolation
A model for many physical systems:
blocked
site
open
site
N=8
49
Percolation
A model for many physical systems:
model
system
vacant site
occupied site
percolates
electricity
material
conductor
insulated
conducts
fluid flow
material
empty
blocked
porous
social interaction
population
person
empty
communicates
50
Likelihood of percolation
Depends on site vacancy probability p.
p low (0.4)
does not percolate
p medium (0.6)
percolates?
p high (0.8)
percolates
51
percolation
probability
p*
0
0
N = 100
0.593
N = 20
53
N=5
open site
blocked site
54
N=5
1.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
open site
blocked site
55
N=5
open site
blocked site
56
top row
N=5
bottom row
open site
blocked site
57
top row
N=5
bottom row
open site
blocked site
N=5
open site
blocked site
59
N=5
open site
blocked site
60
Percolation threshold
Q. What is percolation threshold p* ?
A. About 0.592746 for large square lattices.
constant known only via simulation
percolation
probability
p*
0
0
N = 100
0.593
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
quick union
improvements
applications
63
Algorithms
Algorithms
F O U R T H
E D I T I O N
quick union
improvements
applications