-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickUnion.h
32 lines (27 loc) · 842 Bytes
/
QuickUnion.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
#ifndef QUICK_UNION_H
#define QUICK_UNION_H
#include <iostream>
#include <random>
#include <vector>
#include "UnionFind.h"
// QU: unweighted quick-union, (2) UW: union-by-size (a.k.a. union-by-weight), (3) UR: union-by-rank. The choices for path compression are: (1) NC: no compression, (2) FC: full path compression, (3) PS: path splitting, (4) PH: path halving.
class QuickUnion : public UnionFind {
public:
// Creates the partition {{0}, {1}, ..., {n-1}}
QuickUnion(int n, PathCompressionType pathCompressionType) {
for (int i = 0; i < n; ++i) {
P.push_back(i);
};
n_blocks = n;
this->pathCompressionType = pathCompressionType;
};
void merge(int i, int j) override {
int ri = find(i);
int rj = find(j);
if (ri != rj) {
P[ri] = rj;
--n_blocks;
}
}
};
#endif // QUICK_UNION_H