-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedQuickUnionUF.cpp
More file actions
45 lines (39 loc) · 1.1 KB
/
WeightedQuickUnionUF.cpp
File metadata and controls
45 lines (39 loc) · 1.1 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
//
// Created by Ben Chan on 7/31/21.
//
#include "WeightedQuickUnionUF.hpp"
int WeightedQuickUnionUF::find(int p) {
validate(p);
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
void WeightedQuickUnionUF::validate(int p) {
int n = (int) parent.size();
if (p < 0 || p >= n)
throw invalid_argument("index " + to_string(p) + " is not between 0 and " + to_string(n - 1));
}
void WeightedQuickUnionUF::weightedUnion(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make root of smaller rank point to root of larger rank
if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
else {
parent[rootQ] = rootP;
rank[rootP]++;
}
thisCount--;
}
WeightedQuickUnionUF::WeightedQuickUnionUF(int n) : thisCount(n) {
if (n < 0) throw invalid_argument("n is less than zero.");
parent.resize(n);
rank.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}