Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
101 lines (95 loc) · 2.43 KB

File metadata and controls

101 lines (95 loc) · 2.43 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Keeps track of a set of elements partitioned into a
* number of disjoint (nonoverlapping) subsets.
* Allows to check whether the path between two nodes exists.
* <br>
* The algorithm is inspired by Robert Sedgewick's Java implementation.
* For further reading http://algs4.cs.princeton.edu/home/.
*
* @example
*
* var QuickUnion = require('path-to-algorithms/' +
* 'src/sets/weightquickunion').QuickUnion;
*
* var qunion = new QuickUnion(10);
* qunion.union(0, 1);
* qunion.union(2, 1);
* qunion.union(3, 4);
* qunion.union(8, 9);
* qunion.union(4, 8);
*
* console.log(qunion.connected(0, 9)); // false
* console.log(qunion.connected(3, 9)); // true
*
* @public
* @module sets/weightquickunion
*/
(function (exports) {
'use strict';
/**
* Initialization.<br><br>
* Time complexity: O(N).
*
* @public
* @constructor
* @param {Numner} size Count of the nodes.
*/
exports.QuickUnion = function (n) {
this._ids = [];
this._size = [];
for (var i = 0; i < n; i += 1) {
this._ids[i] = i;
this._size[i] = 1;
}
};
/**
* Finds the root of given node.<br><br>
* Time complexity: O(log N).
* @private
* @param {Number} i The given node.
* @return {Number} Root of the given node.
*/
exports.QuickUnion.prototype._root = function (i) {
while (i !== this._ids[i]) {
// this._ids[i] = this._ids[this._ids[i]]; //enables the path compression
i = this._ids[i];
}
return i;
};
/**
* Checks whether two nodes are connected.<br><br>
* Time complexity: O(log N).
*
* @param {Number} p The first node.
* @param {Number} q The second node.
* @return {Boolean} True/false depending on whether the nodes are connected.
*/
exports.QuickUnion.prototype.connected = function (p, q) {
return this._root(p) === this._root(q);
};
/**
* Connects two nodes - p and q.<br><br>
* Time complexity: O(log N).
*
* @public
* @method
* @param {Number} p The first node.
* @param {Number} q The second node.
*/
exports.QuickUnion.prototype.union = function (p, q) {
var pf = this._root(p);
var qf = this._root(q);
if (pf === qf) {
return; // already linked
}
var psz = this._size[qf];
var qsz = this._size[pf];
if (psz < qsz) {
this._ids[pf] = qf;
this._size[qf] += psz;
} else {
this._ids[qf] = pf;
this._size[pf] += qsz;
}
};
})(typeof window === 'undefined' ? module.exports : window);
Morty Proxy This is a proxified and sanitized view of the page, visit original site.