|
| 1 | +/** |
| 2 | + * Keeps track of a set of elements partitioned into a |
| 3 | + * number of disjoint (nonoverlapping) subsets. |
| 4 | + * |
| 5 | + * @example |
| 6 | + * |
| 7 | + * var QuickFind = require('path-to-algorithms/src/sets/quickfind').QuickFind; |
| 8 | + * |
| 9 | + * var find = new QuickFind(10); |
| 10 | + * find.union(0, 1); |
| 11 | + * find.union(2, 1); |
| 12 | + * find.union(3, 4); |
| 13 | + * find.union(8, 9); |
| 14 | + * find.union(4, 8); |
| 15 | + * |
| 16 | + * console.log(find.connected(0, 9)); // false |
| 17 | + * console.log(find.connected(3, 9)); // true |
| 18 | + * |
| 19 | + * @public |
| 20 | + * @module sets/quickfind |
| 21 | + */ |
1 | 22 | (function (exports) { |
2 | 23 | 'use strict'; |
3 | 24 |
|
4 | 25 | /** |
5 | | - * Checks whether there is a path between two nodes. |
6 | | - * The initialization is O(n). |
| 26 | + * Initialization.<br><br> |
| 27 | + * Complexity: O(N). |
7 | 28 | * |
8 | 29 | * @constructor |
9 | | - * @param {numner} size The count of the nodes |
| 30 | + * @param {Numner} size Count of the nodes. |
10 | 31 | */ |
11 | 32 | function QuickFind(size) { |
12 | 33 | this._ids = []; |
|
16 | 37 | } |
17 | 38 |
|
18 | 39 | /** |
19 | | - * Connects two nodes - p and q. |
20 | | - * Complexity O(n). |
| 40 | + * Connects two nodes - p and q.<br><br> |
| 41 | + * Complexity: O(N). |
21 | 42 | * |
22 | | - * @param {number} p The first node |
23 | | - * @param {number} q The second node |
| 43 | + * @param {Number} p The first node. |
| 44 | + * @param {Number} q The second node. |
24 | 45 | */ |
25 | 46 | QuickFind.prototype.union = function (p, q) { |
26 | 47 | var size = this._ids.length; |
|
34 | 55 | }; |
35 | 56 |
|
36 | 57 | /** |
37 | | - * Checks whether two nodes are connected. |
38 | | - * Complexity O(1). |
| 58 | + * Checks whether two nodes are connected.<br><br> |
| 59 | + * Complexity: O(1). |
39 | 60 | * |
40 | | - * @param {number} p The first node |
41 | | - * @param {number} q The second node |
| 61 | + * @param {number} p The first node. |
| 62 | + * @param {number} q The second node. |
42 | 63 | * |
43 | 64 | */ |
44 | 65 | QuickFind.prototype.connected = function (p, q) { |
|
47 | 68 |
|
48 | 69 | exports.QuickFind = QuickFind; |
49 | 70 |
|
50 | | - //var find = new QuickFind(10); |
51 | | - //find.union(0, 1); |
52 | | - //find.union(2, 1); |
53 | | - //find.union(3, 4); |
54 | | - //find.union(8, 9); |
55 | | - //find.union(4, 8); |
56 | | - // |
57 | | - //console.log(find.connected(0, 9)); //expected false |
58 | | - //console.log(find.connected(3, 9)); //expected true |
59 | | -}(typeof exports === 'undefined' ? window : exports)); |
| 71 | +})(typeof window === 'undefined' ? module.exports : window); |
0 commit comments