-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnionWeight.h
45 lines (37 loc) · 1.1 KB
/
UnionWeight.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
#ifndef UNION_WEIGHT_H
#define UNION_WEIGHT_H
#include <iostream>
#include <random>
#include <vector>
#include "UnionFind.h"
using namespace std;
// 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 UnionWeight : public UnionFind {
public:
// Creates the partition {{0}, {1}, ..., {n-1}}
UnionWeight(int n, PathCompressionType pathCompressionType) {
P = vector<int>(n, -1);
n_blocks = n;
this->pathCompressionType = pathCompressionType;
};
// Performs the union of the classes with representatives ri and rj
void merge(int i, int j) {
int ri = find(i);
int rj = find(j);
if (ri == rj) return;
if (ri != rj) {
if (P[ri] >= P[rj]) {
// ri is the smallest/shortest
P[rj] += P[ri];
P[ri] = rj;
}
else {
// rj is the smallest/shortest
P[ri] += P[rj];
P[rj] = ri;
}
--n_blocks;
}
}
};
#endif // UNION_WEIGHT_H