Skip to content

Commit 1020f44

Browse files
committed
Update weight quickunion
1 parent 3f0494e commit 1020f44

File tree

1 file changed

+72
-69
lines changed

1 file changed

+72
-69
lines changed

src/sets/weightquickunion.js

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,77 @@
1-
/**
2-
* Checks whether there is a path between two nodes
3-
* Complexity of the initialization O(n).
4-
*
5-
* @constructor
6-
* @param {number} n The nodes count
7-
*/
8-
function QuickUnion(n) {
9-
this._ids = [];
10-
this._size = [];
11-
for (var i = 0; i < n; i += 1) {
12-
this._ids[i] = i;
13-
this._size[i] = 1;
1+
(function (exports) {
2+
/**
3+
* Checks whether there is a path between two nodes
4+
* Complexity of the initialization O(n).
5+
*
6+
* @constructor
7+
* @param {number} n The nodes count
8+
*/
9+
function QuickUnion(n) {
10+
this._ids = [];
11+
this._size = [];
12+
for (var i = 0; i < n; i += 1) {
13+
this._ids[i] = i;
14+
this._size[i] = 1;
15+
}
1416
}
15-
}
1617

17-
/**
18-
* Finds the root of given node.
19-
* The complexity is around O(logn)
20-
*
21-
* @param {number} i The given node
22-
* @return {number} The root of the node
23-
*/
24-
QuickUnion.prototype._root = function (i) {
25-
while (i !== this._ids[i]) {
26-
// this._ids[i] = this._ids[this._ids[i]]; //enables the path compression
27-
i = this._ids[i];
28-
}
29-
return i;
30-
};
18+
/**
19+
* Finds the root of given node.
20+
* The complexity is around O(logn)
21+
*
22+
* @param {number} i The given node
23+
* @return {number} The root of the node
24+
*/
25+
QuickUnion.prototype._root = function (i) {
26+
while (i !== this._ids[i]) {
27+
// this._ids[i] = this._ids[this._ids[i]]; //enables the path compression
28+
i = this._ids[i];
29+
}
30+
return i;
31+
};
3132

32-
/**
33-
* Checks whether two nodes are connected.
34-
* Complexity O(logn)
35-
*
36-
* @param {number} p The first node
37-
* @param {number} q The second node
38-
* @return {boolean} True/false depending on whether the nodes are connected
39-
*/
40-
QuickUnion.prototype.connected = function (p, q) {
41-
return this._root(p) === this._root(q);
42-
};
33+
/**
34+
* Checks whether two nodes are connected.
35+
* Complexity O(logn)
36+
*
37+
* @param {number} p The first node
38+
* @param {number} q The second node
39+
* @return {boolean} True/false depending on whether the nodes are connected
40+
*/
41+
QuickUnion.prototype.connected = function (p, q) {
42+
return this._root(p) === this._root(q);
43+
};
4344

44-
/**
45-
* Unions two nodes.
46-
* Complexity O(logn)
47-
*
48-
* @param {number} p The first node
49-
* @param {number} q The second node
50-
*/
51-
QuickUnion.prototype.union = function (p, q) {
52-
var pf = this._root(p);
53-
var qf = this._root(q);
54-
if (pf == qf) return; // already linked
55-
var psz = this._size[qf];
56-
var qsz = this._size[pf];
57-
if (psz < qsz) {
58-
this._ids[pf] = qf;
59-
this._size[qf] += psz;
60-
} else {
61-
this._ids[qf] = pf;
62-
this._size[pf] += qsz;
63-
}
64-
};
45+
/**
46+
* Unions two nodes.
47+
* Complexity O(logn)
48+
*
49+
* @param {number} p The first node
50+
* @param {number} q The second node
51+
*/
52+
QuickUnion.prototype.union = function (p, q) {
53+
var pf = this._root(p);
54+
var qf = this._root(q);
55+
if (pf == qf) return; // already linked
56+
var psz = this._size[qf];
57+
var qsz = this._size[pf];
58+
if (psz < qsz) {
59+
this._ids[pf] = qf;
60+
this._size[qf] += psz;
61+
} else {
62+
this._ids[qf] = pf;
63+
this._size[pf] += qsz;
64+
}
65+
};
6566

66-
//var union = new QuickUnion(10);
67-
//union.union(0, 1);
68-
//union.union(2, 1);
69-
//union.union(3, 4);
70-
//union.union(8, 9);
71-
//union.union(4, 8);
72-
//
73-
//console.log(union.connected(0, 9)); //expected false
74-
//console.log(union.connected(3, 9)); //expected true
67+
//var union = new QuickUnion(10);
68+
//union.union(0, 1);
69+
//union.union(2, 1);
70+
//union.union(3, 4);
71+
//union.union(8, 9);
72+
//union.union(4, 8);
73+
//
74+
//console.log(union.connected(0, 9)); //expected false
75+
//console.log(union.connected(3, 9)); //expected true
76+
exports.QuickUnion = QuickUnion;
77+
}(typeof exports === 'undefined' ? window : exports));

0 commit comments

Comments
 (0)