-
Notifications
You must be signed in to change notification settings - Fork 5
/
union_find.h
97 lines (79 loc) · 2.39 KB
/
union_find.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Created by codercat on 19-5-10.
//
#ifndef CPP_ALGORITHMS_UNION_FIND_H
#define CPP_ALGORITHMS_UNION_FIND_H
class UnionFind {
private:
// 元素个数
unsigned int elementNum = 0;
// 联通分量
unsigned int connectedComponent = 0;
int *parents;
// 记录每个节点所在树的深度
int *ranks;
public:
UnionFind(unsigned int elementNum) {
this->elementNum = elementNum;
// 初始情况下联通分量为元素个数
this->connectedComponent = elementNum;
this->parents = new int[elementNum];
this->ranks = new int[elementNum];
for ( int i = 0; i < elementNum; i ++ ) {
this->parents[i] = i;
this->ranks[i] = 1;
}
}
int find(int element) {
int parent = parents[element];
if (parent == element) {
return parent;
}
// pathCompression
parents[element] = parents[parent];
return this->find(parents[element]);
}
// int find(int element) {
// int parent = parents[element];
// if (parent == element) {
// return parent;
// }
// // pathCompression
// parents[element] = this->find(parents[element]);
// return parents[element];
// }
bool isConnected(int p ,int q) {
return this->find(p) == this->find(q);
}
unsigned int getElementNum() {
return this->elementNum;
}
unsigned int getConnectedComponent() {
return this->connectedComponent;
}
void unionElement(int p, int q) {
int pRoot = this->find(p);
int qRoot = this->find(q);
// 如果两个元素的根节点相同,则代表它们属于同一个集合,就不再需要合并
if ( pRoot == qRoot ) {
return;
}
int pRank = this->ranks[pRoot];
int qRank = this->ranks[qRoot];
// 把深度低的树的根节点指向深度高的树的根节点,保证树的平衡,避免退化成链表.
if ( pRank > qRank ) {
this->parents[qRoot] = pRoot;
} else if ( pRank < qRank ) {
this->parents[pRoot] = qRoot;
} else {
this->parents[pRoot] = qRoot;
this->ranks[qRoot] ++;
}
this->connectedComponent --;
}
~UnionFind() {
delete this->parents;
delete this->ranks;
}
};
#endif //CPP_ALGORITHMS_UNION_FIND_H