-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_union.h
More file actions
53 lines (42 loc) · 1.21 KB
/
Copy pathquick_union.h
File metadata and controls
53 lines (42 loc) · 1.21 KB
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
#ifndef DATA_STRUCTURES_QUICK_UNION_H_
#define DATA_STRUCTURES_QUICK_UNION_H_
#include <numeric>
#include <vector>
using namespace std;
// Data structure that allows to connect nodes and efficiently check if two
// nodes are connected.
class QuickUnion {
public:
// T~O(N), S~O(N)
explicit QuickUnion(const int num_nodes)
: parent_(num_nodes), depth_(num_nodes, 1) {
std::iota(parent_.begin(), parent_.end(), 0);
}
// T~O(log(N)), S~(1)
void connect(const int a, const int b) {
const int root_a = findRoot(a);
const int root_b = findRoot(b);
if (root_a == root_b) { return; }
int& depth_root_a = depth_[root_a];
int& depth_root_b = depth_[root_b];
if (depth_root_a > depth_root_b) {
parent_[root_b] = root_a;
} else {
parent_[root_a] = root_b;
depth_root_b = max(depth_root_b, 1 + depth_root_a);
}
}
// T~O(log(N)), S~(1)
bool isConnected(const int a, const int b) const {
return findRoot(a) == findRoot(b);
}
private:
// T~O(log(N)), S~(1)
int findRoot(int node) const {
while (parent_[node] != node) { node = parent_[node]; }
return node;
}
vector<int> parent_;
vector<int> depth_;
};
#endif // DATA_STRUCTURES_QUICK_UNION_H_