Professional Documents
Culture Documents
Union-Find
We have n items
Maintains a collection of disjoint sets
Each of the n items is in exactly one set
items = {1, 2, 3, 4, 5, 6}
collections = {1, 4}, {3, 5, 6}, {2}
collections = {1}, {2}, {3}, {4}, {5}, {6}
Supports two operations efficiently: find(x) and
union(x,y).
Union-Find
items = {1, 2, 3, 4, 5, 6}
collections = {1, 4}, {3, 5, 6}, {2}
find(x) returns a representative item from the set
that x is in
find(1) = 1
find(4) = 1
find(3) = 5
find(5) = 5
find(6) = 5
find(2) = 2
Union-Find
items = {1, 2, 3, 4, 5, 6}
collections = {1, 4}, {3, 5, 6}, {2}
union(x, y) merges the set containing x and the set
containing y together.
union(4, 2)
collections = {1, 2, 4}, {3, 5, 6}
union(3, 6)
collections = {1, 2, 4}, {3, 5, 6}
union(2, 6)
collections = {1, 2, 3, 4, 5, 6}
Union-Find implementation
struct union_find {
vector<int> parent;
union_find(int n) {
parent = vector<int>(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
// find and union
};
6
Union-Find implementation
// find and union
int find(int x) {
if (parent[x] == x) {
return x;
} else {
parent[x] = find(parent[x]);
return parent[x];
}
}
void unite(int x, int y) {
parent[find(x)] = find(y);
}
7
If youre in a hurry...
Union-Find applications
5
3
10
5
3
items = {1, 2, 3, 4, 5, 6, 7}
10
5
3
items = {1, 2, 3, 4, 5, 6, 7}
collections = {1, 4, 7}, {2}, {3, 5, 6}
10
5
3
items = {1, 2, 3, 4, 5, 6, 7}
collections = {1, 4, 7}, {2}, {3, 5, 6}
union(2, 5)
10
5
3
items = {1, 2, 3, 4, 5, 6, 7}
collections = {1, 4, 7}, {2, 3, 5, 6}
11
5
3
items = {1, 2, 3, 4, 5, 6, 7}
collections = {1, 4, 7}, {2, 3, 5, 6}
union(6, 2)
11
5
3
items = {1, 2, 3, 4, 5, 6, 7}
collections = {1, 4, 7}, {2, 3, 5, 6}
12
http://uva.onlinejudge.org/external/106/10608.html
13
Range queries
14
5 9 3
15
5 9 3
sum(0, 6)
15
5 9 3
sum(0, 6) = 33
15
5 9 3
sum(0, 6) = 33
sum(2, 5)
15
5 9 3
sum(0, 6) = 33
sum(2, 5) = 29
15
5 9 3
sum(0, 6) = 33
sum(2, 5) = 29
sum(2, 2)
15
5 9 3
sum(0, 6) = 33
sum(2, 5) = 29
sum(2, 2) = 7
15
5 9 3
sum(0, 6) = 33
sum(2, 5) = 29
sum(2, 2) = 7
15
5 9 3
=
1 0 7 8
5 9 3
1 0 7 8
5 9 3
16
0 7
17
0 7
17
0 7
1
17
0 7
1 8
17
0 7 8
1 8 16
17
0 7 8
1 8 16
5
21
17
0 7 8
1 8 16
5
21
9
30
17
0 7 8
1 8 16
5
21
9
30
3
33
17
0 7 8
1 8 16
5
21
9
30
3
33
17
0 7 8
1 8 16
5
21
9
30
3
33
17
1 0 7 8
5 9 3
18
1 0 7 8
5 9 3
sum(0, 6)
18
1 0 7 8
5 9 3
sum(0, 6) = 33
18
1 0 7 8
5 9 3
sum(0, 6) = 33
update(3, 2)
18
1 0 7 -2
5 9 3
sum(0, 6) = 33
update(3, 2)
18
1 0 7 -2
5 9 3
sum(0, 6) = 33
update(3, 2)
sum(0, 6)
18
1 0 7 -2
5 9 3
sum(0, 6) = 33
update(3, 2)
sum(0, 6) = 23
18
1 0 7 -2
5 9 3
sum(0, 6) = 33
update(3, 2)
sum(0, 6) = 23
18
Segment Tree
1 0 7 8
5 9 3
19
Segment Tree
1 0 7 8
5 9 3
19
Segment Tree
15
1
1
14
8
1 0 7 8
5 9 3
19
Segment Tree
16
17
15
1
1
14
8
1 0 7 8
5 9 3
19
Segment Tree
33
16
17
15
1
1
14
8
1 0 7 8
5 9 3
19
Segment Tree
33
16
17
15
1
1
14
8
1 0 7 8
5 9 3
19
20
16
17
15
1
1
14
8
1 0 7 8
5 9 3
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5) = 16 + 14 = 30
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5) = 16 + 14 = 30
We only need to consider a few vertices to get the
entire range
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5) = 16 + 14 = 30
We only need to consider a few vertices to get the
entire range
But how do we find them?
21
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
16
17
15
1
1
14
8
1 0 7 8
5 9 3
sum(0, 5)
22
23
16
17
15
1
1
14
8
1 0 7 8
5 9 3
24
16
17
15
1
1
14
8
1 0 7 8
5 9 3
update(3, 5)
24
16
17
15
1
1
14
8
1 0 7 8
5 9 3
update(3, 5)
24
16
17
15
1
1
14
5
1 0 7 5
5 9 3
update(3, 5)
24
16
17
15
1
1
14
5
1 0 7 5
5 9 3
update(3, 5)
24
16
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
16
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
13
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
13
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
13
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
13
17
1
1
12
0
14
5
1 0 7 5
5 9 3
update(3, 5)
24
25
Segment Tree
Now we can
build a Segment Tree
query a range
update a single value
26
Segment Tree
Now we can
build a Segment Tree
query a range
update a single value
26
Segment Tree
Now we can
build a Segment Tree in O(n)
query a range
update a single value
26
Segment Tree
Now we can
build a Segment Tree in O(n)
query a range in O(log n)
update a single value
26
Segment Tree
Now we can
build a Segment Tree in O(n)
query a range in O(log n)
update a single value in O(log n)
26
Segment Tree
Now we can
build a Segment Tree in O(n)
query a range in O(log n)
update a single value in O(log n)
26
Segment Tree
Now we can
build a Segment Tree in O(n)
query a range in O(log n)
update a single value in O(log n)
26
http://uva.onlinejudge.org/external/120/12086.html
27