|
| 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 | + |
1 | 25 | (function (exports) { |
2 | 26 | 'use strict'; |
3 | 27 |
|
4 | 28 | /** |
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). |
7 | 31 | * |
| 32 | + * @public |
8 | 33 | * @constructor |
9 | | - * @param {number} n The nodes count |
| 34 | + * @param {Numner} size Count of the nodes. |
10 | 35 | */ |
11 | | - function QuickUnion(n) { |
| 36 | + exports.QuickUnion = function (n) { |
12 | 37 | this._ids = []; |
13 | 38 | this._size = []; |
14 | 39 | for (var i = 0; i < n; i += 1) { |
15 | 40 | this._ids[i] = i; |
16 | 41 | this._size[i] = 1; |
17 | 42 | } |
18 | | - } |
| 43 | + }; |
19 | 44 |
|
20 | 45 | /** |
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. |
26 | 51 | */ |
27 | | - QuickUnion.prototype._root = function (i) { |
| 52 | + exports.QuickUnion.prototype._root = function (i) { |
28 | 53 | while (i !== this._ids[i]) { |
29 | 54 | // this._ids[i] = this._ids[this._ids[i]]; //enables the path compression |
30 | 55 | i = this._ids[i]; |
|
33 | 58 | }; |
34 | 59 |
|
35 | 60 | /** |
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). |
38 | 63 | * |
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. |
42 | 67 | */ |
43 | | - QuickUnion.prototype.connected = function (p, q) { |
| 68 | + exports.QuickUnion.prototype.connected = function (p, q) { |
44 | 69 | return this._root(p) === this._root(q); |
45 | 70 | }; |
46 | 71 |
|
47 | 72 | /** |
48 | | - * Unions two nodes. |
49 | | - * Complexity O(logn) |
| 73 | + * Connects two nodes - p and q.<br><br> |
| 74 | + * Time complexity: O(log N). |
50 | 75 | * |
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. |
53 | 80 | */ |
54 | | - QuickUnion.prototype.union = function (p, q) { |
| 81 | + exports.QuickUnion.prototype.union = function (p, q) { |
55 | 82 | var pf = this._root(p); |
56 | 83 | var qf = this._root(q); |
57 | 84 | if (pf === qf) { |
|
68 | 95 | } |
69 | 96 | }; |
70 | 97 |
|
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