Professional Documents
Culture Documents
network
›connectivity
quick find
›
quick union
›
improvements
›
applications
›
1
Subtext of today’s lecture (and this course)
2
network
›connectivity
quick find
›
quick union
›
improvements
›
applications
›
3
Network connectivity
Basic abstractions
• set of objects
• union command: connect two objects
• find query: is there a path connecting one object to another?
4
Objects
• Computers in a network.
• Web pages on the Internet.
•• Transistors
Variable name in aliases.
a computer chip.
stay tuned
2 3 4
0 7 8
5
Union-find abstractions
• Objects.
0 1 2 3 4 5 6 7 8 grid points
9
6
Connected components
Connected component: set of mutually connected vertices
in out 1 6 5 9
3 4 3 4
4 9 4 9
8 0 8 0
2 3 2 3 2 3 4
5 6 5 6 3 = 10-7 components
2 9
5 9 5 9
7 3 7 3 7
7 union commands
0 8
7
Network connectivity: larger example
find(u, v) ?
8
Network connectivity: larger example
find(u, v) ?
63 components
true
9
Union-find abstractions
• Objects.
• Disjoint sets of objects.
• Find queries: are two objects in the same set?
• Union commands: replace sets containing two items by their union
Goal. Design efficient data structure for
union-find.
Data structure.
12
Quick-find [eager approach]
Data structure.
i 0 1 2 3 4 5 6 7 8 9 union of 3 and 6
id[i] 0 1 6 6 6 6 6 7 8 6 2, 3, 4, 5, 6, and 9 are connected
3-4 0 1 2 4 4 5 6 7 8 9
4-9 0 1 2 9 9 5 6 7 8 9
8-0 0 1 2 9 9 5 6 7 09
2-3 0 1 9 9 9 5 6 7 0 9
5-6 0 1 9 9 9 6 6 7 0 9
5-9 0 1 9 9 9 9 9 7 0 9
7-3 0 1 9 9 9 9 9 9 0 9
4-8 0 1 0 0 0 0 0 0 0 0
6-1 1 1 1 1 1 1 1 1 1 1
14
Quick-find: Java implementation
public QuickFind(int N)
{ id = new int[N];
set id of each
for (int i = 0; i < N; i++) object to itself
id[i] = i;
}
15
Quick-find is too slow
• Quick-find takes
Paradoxically, quadratic
morealgorithms
19 get worse with newer equipment.
than 10 operations.
•• New computer may be 10x as fast.
300+ years of computer time!
• But, has 10x as much memory so problem may be 10x bigger.
• With quadratic algorithm, takes 10x as long! 16
network connectivity
›
quick find
›
quick union
› improvements
›
applications
›
17
Quick-union [lazy
approach]
Data structure.
•Integer array id[] of size N.
keep going until it doesn’t change
• Rootid[i]
i 0
of i is
0
1 2
7
3
8
4
9
5 6 0 1 9 6 7 8
id[id[id[...id[i]...]]].
1 9 4 9 6 6 2 4 5
7 8 9
3
18
Quick-union [lazy approach]
Data structure.
i.
• Root of
i 0
i is
1 2 3 4 5 6 0 1 9 6 7 8
id[i] 0 7 8 .
id[id[id[...id[i]...]]] 9
1 9 4 9 6 6 2 4 5
7 8 9
Find. Check if p and q have the same root.
3
i 0 1 2 3 4 5 6 0 1 9 7 8
id[i] 0 7 8 9
1 9 4 9 6 9 2 4 6
7 8 9
only one value changes
p 3 5 q
19
Quick-union example
3-4 0 1 2 4 4 5 6 7 8 9
4-9 0 1 2 4 9 5 6 7 8 9
8-0 0 1 2 4 9 5 6 7 0 9
2-3 0 1 9 4 9 5 6 7 0 9
5-6 0 1 9 4 9 6 6 7 0 9
5-9 0 1 9 4 9 6 9 7 0 9
7-3 0 1 9 4 9 6 9 9 0 9
4-8 0 1 9 4 9 6 9 9 0 0
6-1 1 1 9 4 9 6 9 9 0 0
20
Quick-union: Java implementation
public
{ QuickUnion(int N)
id = new int[N];
for (int i = 0; i < N; i++) id[i] = i;
}
Quick-find defect.
Quick-find N 1
22
network connectivity
›
quick find
›
quick union
›
improvements
› applications
›
23
Improvement 1: Weighting
Weighted quick-union.
0 1 9 6 7 8
2 4 5
p
3
q
24
Weighted quick-union example
3-4 0 1 2 3 3 5 6 7 8 9
4-9 0 1 2 3 3 5 6 7 8 3
8-0 8 1 2 3 3 5 6 7 8 3
2-3 8 1 3 3 3 5 6 7 8 3
5-6 8 1 3 3 3 5 5 7 8 3
5-9 8 1 3 3 3 3 5 7 8 3
7-3 8 1 3 3 3 3 5 3 8 3
4-8 8 1 3 3 3 3 5 3 3 3
6-1 8 3 3 3 3 3 5 3 3 3
25
Weighted quick-union: Java implementation
Java implementation.
26
Weighted quick-union analysis
Analysis.
Quick-find N 1
Quick-union N*
N
Weighted QU lg N *
lg N
27
Improvement 2: Path compression
0 0
1 2 9 6 3 1 2
3 4 5 10 11 8 7 4 5
6 7
root(9)
8 9
10 11
28
Weighted quick-union with path compression
Path compression.
• Standard implementation: add second loop to root() to set
the id of each examined node to every
the root.
• Simpler one-pass variant:
make other node in path
29
Weighted quick-union with path compression
3-4 0 1 2 3 3 5 6 7 8 9
4-9 0 1 2 3 3 5 6 7 8 3
8-0 8 1 2 3 3 5 6 7 8 3
2-3 8 1 3 3 3 5 6 7 8 3
5-6 8 1 3 3 3 5 5 7 8 3
5-9 8 1 3 3 3 3 5 7 8 3
7-3 8 1 3 3 3 3 5 3 8 3
4-8 8 1 3 3 3 3 5 3 3 3
6-1 8 3 3 3 3 3 3 3 3 3
30
WQUPC performance
Linear algorithm?
265536 5
Amazing fact:
Quick-find MN
Quick-union MN
Weighted QU N + M log N
33
Union-find applications
f
Network connectivity.
• Percolation.
• Image processing.
• Least common ancestor.
• Equivalence of finite state automata.
• Hinley-Milner polymorphic type inference.
• Kruskal's minimum spanning tree algorithm.
• Games (Go, Hex)
• Compiling equivalence statements in Fortran.
34
Percolation
• N-by-N grid.
• Each square is vacant or occupied.
• Grid percolates if top and bottom are connected by vacant squares.
35
Percolation phase transition
• Vacancy percentage
2 3 4 5 estimates
6 7 8 p*9 10 11 12 0
14 15 16 16 16 16 20 21 22 23 24 0
14 14 28 29 30 31top32 33 34 35 36 0
14 39 40 1 42 43 32 45 46 1 1 49
50 1 52 1 54 55 56 57 58 1 1 1
vacant
1 1 1 1 1 1 1 1 1 1 1 1
not vacant
bottom
37
Percolation
38
Hex
Hex. [Piet Hein 1942, John Nash 1948, Parker Brothers 1962]
Reference: http://mathworld.wolfram.com/GameofHex.html
40