Skip to content

Commit 52ee347

Browse files
author
AndreyGeonya
committed
weighted quickunion jsdoc
1 parent 007f1a9 commit 52ee347

3 files changed

Lines changed: 61 additions & 45 deletions

File tree

‎src/sets/quickfind.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* Initialization.<br><br>
28-
* Complexity: O(N).
28+
* Time complexity: O(N).
2929
*
3030
* @public
3131
* @constructor
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Connects two nodes - p and q.<br><br>
43-
* Complexity: O(N).
43+
* Time complexity: O(N).
4444
*
4545
* @public
4646
* @method
@@ -60,7 +60,7 @@
6060

6161
/**
6262
* Checks whether two nodes are connected.<br><br>
63-
* Complexity: O(1).
63+
* Time complexity: O(1).
6464
*
6565
* @public
6666
* @method
@@ -71,5 +71,4 @@
7171
exports.QuickFind.prototype.connected = function (p, q) {
7272
return this._ids[p] === this._ids[q];
7373
};
74-
7574
})(typeof window === 'undefined' ? module.exports : window);

‎src/sets/quickunion.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
/**
2929
* Initialization.<br><br>
30-
* Complexity: O(N).
30+
* Time complexity: O(N).
3131
*
3232
* @public
3333
* @constructor
@@ -42,7 +42,7 @@
4242

4343
/**
4444
* Finds the root of given node.<br><br>
45-
* Complexity: O(N).
45+
* Time complexity: O(N).
4646
* @private
4747
* @param {Number} i The given node.
4848
* @return {Number} Root of the given node.
@@ -56,7 +56,7 @@
5656

5757
/**
5858
* Connects two nodes - p and q.<br><br>
59-
* Complexity: O(N).
59+
* Time complexity: O(N).
6060
*
6161
* @public
6262
* @method
@@ -70,12 +70,12 @@
7070
};
7171

7272
/**
73-
* Checks whether two nodes are connected.
74-
* Complexity O(n).
73+
* Checks whether two nodes are connected.<br><br>
74+
* Time complexity: O(N).
7575
*
76-
* @param {number} p The first node.
77-
* @param {number} q The second node.
78-
* @return {boolean} True/false depending on whether the nodes are connected.
76+
* @param {Number} p The first node.
77+
* @param {Number} q The second node.
78+
* @return {Boolean} True/false depending on whether the nodes are connected.
7979
*/
8080
exports.QuickUnion.prototype.connected = function (p, q) {
8181
return this._root(p) === this._root(q);

‎src/sets/weightquickunion.js‎

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
1+
/**
2+
* Keeps track of a set of elements partitioned into a
3+
* number of disjoint (nonoverlapping) subsets.
4+
* Allows to check whether the path between two nodes exists.
5+
*
6+
* @example
7+
*
8+
* var QuickUnion = require('path-to-algorithms/' +
9+
* 'src/sets/weightquickunion').QuickUnion;
10+
*
11+
* var qunion = new QuickUnion(10);
12+
* qunion.union(0, 1);
13+
* qunion.union(2, 1);
14+
* qunion.union(3, 4);
15+
* qunion.union(8, 9);
16+
* qunion.union(4, 8);
17+
*
18+
* console.log(qunion.connected(0, 9)); // false
19+
* console.log(qunion.connected(3, 9)); // true
20+
*
21+
* @public
22+
* @module sets/quickunion
23+
*/
24+
125
(function (exports) {
226
'use strict';
327

428
/**
5-
* Checks whether there is a path between two nodes
6-
* Complexity of the initialization O(n).
29+
* Initialization.<br><br>
30+
* Time complexity: O(N).
731
*
32+
* @public
833
* @constructor
9-
* @param {number} n The nodes count
34+
* @param {Numner} size Count of the nodes.
1035
*/
11-
function QuickUnion(n) {
36+
exports.QuickUnion = function (n) {
1237
this._ids = [];
1338
this._size = [];
1439
for (var i = 0; i < n; i += 1) {
1540
this._ids[i] = i;
1641
this._size[i] = 1;
1742
}
18-
}
43+
};
1944

2045
/**
21-
* Finds the root of given node.
22-
* The complexity is around O(logn)
23-
*
24-
* @param {number} i The given node
25-
* @return {number} The root of the node
46+
* Finds the root of given node.<br><br>
47+
* Time complexity: O(log N).
48+
* @private
49+
* @param {Number} i The given node.
50+
* @return {Number} Root of the given node.
2651
*/
27-
QuickUnion.prototype._root = function (i) {
52+
exports.QuickUnion.prototype._root = function (i) {
2853
while (i !== this._ids[i]) {
2954
// this._ids[i] = this._ids[this._ids[i]]; //enables the path compression
3055
i = this._ids[i];
@@ -33,25 +58,27 @@
3358
};
3459

3560
/**
36-
* Checks whether two nodes are connected.
37-
* Complexity O(logn)
61+
* Checks whether two nodes are connected.<br><br>
62+
* Time complexity: O(log N).
3863
*
39-
* @param {number} p The first node
40-
* @param {number} q The second node
41-
* @return {boolean} True/false depending on whether the nodes are connected
64+
* @param {Number} p The first node.
65+
* @param {Number} q The second node.
66+
* @return {Boolean} True/false depending on whether the nodes are connected.
4267
*/
43-
QuickUnion.prototype.connected = function (p, q) {
68+
exports.QuickUnion.prototype.connected = function (p, q) {
4469
return this._root(p) === this._root(q);
4570
};
4671

4772
/**
48-
* Unions two nodes.
49-
* Complexity O(logn)
73+
* Connects two nodes - p and q.<br><br>
74+
* Time complexity: O(log N).
5075
*
51-
* @param {number} p The first node
52-
* @param {number} q The second node
76+
* @public
77+
* @method
78+
* @param {Number} p The first node.
79+
* @param {Number} q The second node.
5380
*/
54-
QuickUnion.prototype.union = function (p, q) {
81+
exports.QuickUnion.prototype.union = function (p, q) {
5582
var pf = this._root(p);
5683
var qf = this._root(q);
5784
if (pf === qf) {
@@ -68,14 +95,4 @@
6895
}
6996
};
7097

71-
//var union = new QuickUnion(10);
72-
//union.union(0, 1);
73-
//union.union(2, 1);
74-
//union.union(3, 4);
75-
//union.union(8, 9);
76-
//union.union(4, 8);
77-
//
78-
//console.log(union.connected(0, 9)); //expected false
79-
//console.log(union.connected(3, 9)); //expected true
80-
exports.QuickUnion = QuickUnion;
81-
}(typeof exports === 'undefined' ? window : exports));
98+
})(typeof window === 'undefined' ? module.exports : window);

0 commit comments

Comments
 (0)