|
| 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/quickunion').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 path between two nodes exists. |
6 | | - * The initialization has O(n) complexity. |
| 29 | + * Initialization.<br><br> |
| 30 | + * Complexity: O(N). |
7 | 31 | * |
| 32 | + * @public |
8 | 33 | * @constructor |
9 | | - * @param {number} n Nodes count |
10 | | - * |
| 34 | + * @param {Numner} size Count of the nodes. |
11 | 35 | */ |
12 | | - function QuickUnion(n) { |
| 36 | + exports.QuickUnion = function (n) { |
13 | 37 | this._ids = []; |
14 | 38 | for (var i = 0; i < n; i += 1) { |
15 | 39 | this._ids[i] = i; |
16 | 40 | } |
17 | | - } |
| 41 | + }; |
18 | 42 |
|
19 | 43 | /** |
20 | | - * Finds the root of given node. |
21 | | - * Complexity O(n). |
22 | | - * |
23 | | - * @param {number} i The given node |
24 | | - * @return {number} The root of the given node |
| 44 | + * Finds the root of given node.<br><br> |
| 45 | + * Complexity: O(N). |
| 46 | + * @private |
| 47 | + * @param {Number} i The given node. |
| 48 | + * @return {Number} Root of the given node. |
25 | 49 | */ |
26 | | - QuickUnion.prototype._root = function (i) { |
| 50 | + exports.QuickUnion.prototype._root = function (i) { |
27 | 51 | while (i !== this._ids[i]) { |
28 | 52 | i = this._ids[i]; |
29 | 53 | } |
30 | 54 | return i; |
31 | 55 | }; |
32 | 56 |
|
33 | 57 | /** |
34 | | - * Unions two nodes. |
35 | | - * Complexity O(n). |
| 58 | + * Connects two nodes - p and q.<br><br> |
| 59 | + * Complexity: O(N). |
36 | 60 | * |
37 | | - * @param {number} p The first node |
38 | | - * @param {number} q The second node |
| 61 | + * @public |
| 62 | + * @method |
| 63 | + * @param {Number} p The first node. |
| 64 | + * @param {Number} q The second node. |
39 | 65 | */ |
40 | | - QuickUnion.prototype.union = function (p, q) { |
| 66 | + exports.QuickUnion.prototype.union = function (p, q) { |
41 | 67 | var pRoot = this._root(p); |
42 | 68 | var qRoot = this._root(q); |
43 | 69 | this._ids[pRoot] = qRoot; |
|
51 | 77 | * @param {number} q The second node. |
52 | 78 | * @return {boolean} True/false depending on whether the nodes are connected. |
53 | 79 | */ |
54 | | - QuickUnion.prototype.connected = function (p, q) { |
| 80 | + exports.QuickUnion.prototype.connected = function (p, q) { |
55 | 81 | return this._root(p) === this._root(q); |
56 | 82 | }; |
57 | | - |
58 | | - //var union = new QuickUnion(10); |
59 | | - //union.union(0, 1); |
60 | | - //union.union(2, 1); |
61 | | - //union.union(3, 4); |
62 | | - //union.union(8, 9); |
63 | | - //union.union(4, 8); |
64 | | - // |
65 | | - //console.log(union.connected(0, 9)); //expected false |
66 | | - //console.log(union.connected(3, 9)); //expected true |
67 | | - exports.QuickUnion = QuickUnion; |
68 | | -}(typeof exports === 'undefined' ? window : exports)); |
| 83 | +})(typeof window === 'undefined' ? module.exports : window); |
0 commit comments