-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
static-disjoint-set.js
195 lines (157 loc) · 3.64 KB
/
static-disjoint-set.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint no-constant-condition: 0 */
/**
* Mnemonist StaticDisjointSet
* ============================
*
* JavaScript implementation of a static disjoint set (union-find).
*
* Note that to remain performant, this implementation needs to know a size
* beforehand.
*/
var helpers = require('./utils/typed-arrays.js');
/**
* StaticDisjointSet.
*
* @constructor
*/
function StaticDisjointSet(size) {
// Optimizing the typed array types
var ParentsTypedArray = helpers.getPointerArray(size),
RanksTypedArray = helpers.getPointerArray(Math.log2(size));
// Properties
this.size = size;
this.dimension = size;
this.parents = new ParentsTypedArray(size);
this.ranks = new RanksTypedArray(size);
// Initializing parents
for (var i = 0; i < size; i++)
this.parents[i] = i;
}
/**
* Method used to find the root of the given item.
*
* @param {number} x - Target item.
* @return {number}
*/
StaticDisjointSet.prototype.find = function(x) {
var y = x;
var c, p;
while (true) {
c = this.parents[y];
if (y === c)
break;
y = c;
}
// Path compression
while (true) {
p = this.parents[x];
if (p === y)
break;
this.parents[x] = y;
x = p;
}
return y;
};
/**
* Method used to perform the union of two items.
*
* @param {number} x - First item.
* @param {number} y - Second item.
* @return {StaticDisjointSet}
*/
StaticDisjointSet.prototype.union = function(x, y) {
var xRoot = this.find(x),
yRoot = this.find(y);
// x and y are already in the same set
if (xRoot === yRoot)
return this;
this.dimension--;
// x and y are not in the same set, we merge them
var xRank = this.ranks[x],
yRank = this.ranks[y];
if (xRank < yRank) {
this.parents[xRoot] = yRoot;
}
else if (xRank > yRank) {
this.parents[yRoot] = xRoot;
}
else {
this.parents[yRoot] = xRoot;
this.ranks[xRoot]++;
}
return this;
};
/**
* Method returning whether two items are connected.
*
* @param {number} x - First item.
* @param {number} y - Second item.
* @return {boolean}
*/
StaticDisjointSet.prototype.connected = function(x, y) {
var xRoot = this.find(x);
return xRoot === this.find(y);
};
/**
* Method returning the set mapping.
*
* @return {TypedArray}
*/
StaticDisjointSet.prototype.mapping = function() {
var MappingClass = helpers.getPointerArray(this.dimension);
var ids = {},
mapping = new MappingClass(this.size),
c = 0;
var r;
for (var i = 0, l = this.parents.length; i < l; i++) {
r = this.find(i);
if (typeof ids[r] === 'undefined') {
mapping[i] = c;
ids[r] = c++;
}
else {
mapping[i] = ids[r];
}
}
return mapping;
};
/**
* Method used to compile the disjoint set into an array of arrays.
*
* @return {array}
*/
StaticDisjointSet.prototype.compile = function() {
var ids = {},
result = new Array(this.dimension),
c = 0;
var r;
for (var i = 0, l = this.parents.length; i < l; i++) {
r = this.find(i);
if (typeof ids[r] === 'undefined') {
result[c] = [i];
ids[r] = c++;
}
else {
result[ids[r]].push(i);
}
}
return result;
};
/**
* Convenience known methods.
*/
StaticDisjointSet.prototype.inspect = function() {
var array = this.compile();
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: StaticDisjointSet,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
StaticDisjointSet.prototype[Symbol.for('nodejs.util.inspect.custom')] = StaticDisjointSet.prototype.inspect;
/**
* Exporting.
*/
module.exports = StaticDisjointSet;