Skip to content

Commit caa4188

Browse files
committed
Merge pull request mgechev#34 from AndreyGeonya/doc-pack12
Doc pack12
2 parents a0fc935 + cafe357 commit caa4188

4 files changed

Lines changed: 146 additions & 98 deletions

File tree

‎doc-config.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"./src/combinatorics/",
1313
"./src/primes/",
1414
"./src/others/",
15-
"./src/searching/"
15+
"./src/searching/",
16+
"./src/sets/"
1617
],
1718
"includePattern": ".+\\.js(doc)?$",
1819
"excludePattern": "docs"

‎src/sets/quickfind.js‎

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
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 QuickFind = require('path-to-algorithms/src/sets/quickfind').QuickFind;
9+
*
10+
* var qfind = new QuickFind(10);
11+
* qfind.union(0, 1);
12+
* qfind.union(2, 1);
13+
* qfind.union(3, 4);
14+
* qfind.union(8, 9);
15+
* qfind.union(4, 8);
16+
*
17+
* console.log(qfind.connected(0, 9)); // false
18+
* console.log(qfind.connected(3, 9)); // true
19+
*
20+
* @public
21+
* @module sets/quickfind
22+
*/
123
(function (exports) {
224
'use strict';
325

426
/**
5-
* Checks whether there is a path between two nodes.
6-
* The initialization is O(n).
27+
* Initialization.<br><br>
28+
* Time complexity: O(N).
729
*
30+
* @public
831
* @constructor
9-
* @param {numner} size The count of the nodes
32+
* @param {Numner} size Count of the nodes.
1033
*/
11-
function QuickFind(size) {
34+
exports.QuickFind = function (size) {
1235
this._ids = [];
1336
for (var i = 0; i < size; i += 1) {
1437
this._ids[i] = i;
1538
}
16-
}
39+
};
1740

1841
/**
19-
* Connects two nodes - p and q.
20-
* Complexity O(n).
42+
* Connects two nodes - p and q.<br><br>
43+
* Time complexity: O(N).
2144
*
22-
* @param {number} p The first node
23-
* @param {number} q The second node
45+
* @public
46+
* @method
47+
* @param {Number} p The first node.
48+
* @param {Number} q The second node.
2449
*/
25-
QuickFind.prototype.union = function (p, q) {
50+
exports.QuickFind.prototype.union = function (p, q) {
2651
var size = this._ids.length;
2752
var pval = this._ids[p];
2853
var qval = this._ids[q];
@@ -34,26 +59,16 @@
3459
};
3560

3661
/**
37-
* Checks whether two nodes are connected.
38-
* Complexity O(1).
39-
*
40-
* @param {number} p The first node
41-
* @param {number} q The second node
62+
* Checks whether two nodes are connected.<br><br>
63+
* Time complexity: O(1).
4264
*
65+
* @public
66+
* @method
67+
* @param {Number} p The first node.
68+
* @param {Number} q The second node.
69+
* @return {Boolean}
4370
*/
44-
QuickFind.prototype.connected = function (p, q) {
71+
exports.QuickFind.prototype.connected = function (p, q) {
4572
return this._ids[p] === this._ids[q];
4673
};
47-
48-
exports.QuickFind = QuickFind;
49-
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));
74+
})(typeof window === 'undefined' ? module.exports : window);

‎src/sets/quickunion.js‎

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,83 @@
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+
125
(function (exports) {
226
'use strict';
327

428
/**
5-
* Checks whether path between two nodes exists.
6-
* The initialization has O(n) complexity.
29+
* Initialization.<br><br>
30+
* Time complexity: O(N).
731
*
32+
* @public
833
* @constructor
9-
* @param {number} n Nodes count
10-
*
34+
* @param {Numner} size Count of the nodes.
1135
*/
12-
function QuickUnion(n) {
36+
exports.QuickUnion = function (n) {
1337
this._ids = [];
1438
for (var i = 0; i < n; i += 1) {
1539
this._ids[i] = i;
1640
}
17-
}
41+
};
1842

1943
/**
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+
* Time complexity: O(N).
46+
* @private
47+
* @param {Number} i The given node.
48+
* @return {Number} Root of the given node.
2549
*/
26-
QuickUnion.prototype._root = function (i) {
50+
exports.QuickUnion.prototype._root = function (i) {
2751
while (i !== this._ids[i]) {
2852
i = this._ids[i];
2953
}
3054
return i;
3155
};
3256

3357
/**
34-
* Unions two nodes.
35-
* Complexity O(n).
58+
* Connects two nodes - p and q.<br><br>
59+
* Time complexity: O(N).
3660
*
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.
3965
*/
40-
QuickUnion.prototype.union = function (p, q) {
66+
exports.QuickUnion.prototype.union = function (p, q) {
4167
var pRoot = this._root(p);
4268
var qRoot = this._root(q);
4369
this._ids[pRoot] = qRoot;
4470
};
4571

4672
/**
47-
* Checks whether two nodes are connected.
48-
* Complexity O(n).
73+
* Checks whether two nodes are connected.<br><br>
74+
* Time complexity: O(N).
4975
*
50-
* @param {number} p The first node.
51-
* @param {number} q The second node.
52-
* @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.
5379
*/
54-
QuickUnion.prototype.connected = function (p, q) {
80+
exports.QuickUnion.prototype.connected = function (p, q) {
5581
return this._root(p) === this._root(q);
5682
};
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);

‎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/weightquickunion
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)