generated from hchiam/learning-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_using_array.js
64 lines (51 loc) · 1.5 KB
/
example_using_array.js
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
// using a parentOf[x] array
class UnionFind {
constructor(size) {
this.parentOf = new Array(size).fill(null).map((x, i) => i);
this.sizeOf = new Array(size).fill(null).map((x) => 1);
}
findSetOf(x) {
if (this.parentOf[x] !== x) {
this.parentOf[x] = this.findSetOf(this.parentOf[x]);
}
return this.parentOf[x]; // if it's its own parent, or if parent is found
}
union(x, y) {
let [smallParent, bigParent] = [this.findSetOf(x), this.findSetOf(y)];
if (smallParent === bigParent) return smallParent;
const namedWrongly = this.sizeOf[smallParent] > this.sizeOf[bigParent];
if (namedWrongly) {
[smallParent, bigParent] = [bigParent, smallParent];
}
// add smaller to bigger for better perf:
this.parentOf[smallParent] = bigParent;
this.sizeOf[bigParent] += this.sizeOf[smallParent];
return bigParent;
}
isInSameSet(x, y) {
return this.findSetOf(x) === this.findSetOf(y);
}
}
exampleUsage();
function exampleUsage() {
const data = [0, 1, 0, 1];
const uf = new UnionFind(data.length);
for (let i = 0; i < data.length; i++) {
uf.union(data[i], i);
}
console.log(uf);
data.forEach((x) => {
console.log(`${x} is under group`, uf.findSetOf(x));
});
console.log(uf.isInSameSet(0, 0));
console.log(uf.isInSameSet(0, 1));
/**
* UnionFind { parentOf: [ 2, 3, 2, 3 ], sizeOf: [ 1, 1, 2, 2 ] }
* 0 is under group 2
* 1 is under group 3
* 0 is under group 2
* 1 is under group 3
* true
* false
*/
}