forked from mgechev/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickunion.js
More file actions
64 lines (60 loc) · 1.54 KB
/
quickunion.js
File metadata and controls
64 lines (60 loc) · 1.54 KB
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
(function (exports) {
/**
* Checks whether path between two nodes exists.
* The initialization has O(n) complexity.
*
* @constructor
* @param {number} n Nodes count
*
*/
function QuickUnion(n) {
this._ids = [];
for (var i = 0; i < n; i += 1) {
this._ids[i] = i;
}
}
/**
* Finds the root of given node.
* Complexity O(n).
*
* @param {number} i The given node
* @return {number} The root of the given node
*/
QuickUnion.prototype._root = function (i) {
while (i !== this._ids[i]) i = this._ids[i];
return i;
};
/**
* Unions two nodes.
* Complexity O(n).
*
* @param {number} p The first node
* @param {number} q The second node
*/
QuickUnion.prototype.union = function (p, q) {
var pRoot = this._root(p),
qRoot = this._root(q);
this._ids[pRoot] = qRoot;
};
/**
* Checks whether two nodes are connected.
* Complexity O(n).
*
* @param {number} p The first node.
* @param {number} q The second node.
* @return {boolean} True/false depending on whether the nodes are connected.
*/
QuickUnion.prototype.connected = function (p, q) {
return this._root(p) === this._root(q);
};
//var union = new QuickUnion(10);
//union.union(0, 1);
//union.union(2, 1);
//union.union(3, 4);
//union.union(8, 9);
//union.union(4, 8);
//
//console.log(union.connected(0, 9)); //expected false
//console.log(union.connected(3, 9)); //expected true
exports.QuickUnion = QuickUnion;
}(typeof exports === 'undefined' ? window : exports));