D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
nehiljain
Full window
Github gist
// source http://jsbin.com/ceqeso
<!DOCTYPE html> <html> <head> <meta name="description" content="[add your bin description]" /> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> var WeightedUFPathCompression = (function () { var _privateData = {}; var _privateId = 0; function WeightedUFPathCompression(numberOfElements) { var i; Object.defineProperty(this, '_id', {value: _privateId++}); _privateData[this._id] = { count: numberOfElements, id: [], sz: [] }; for (i = 0; i < numberOfElements; i++) { _privateData[this._id].id[i] = i; _privateData[this._id].sz[i] = 1; } } WeightedUFPathCompression.prototype.count = function () { return _privateData[this._id].count; }; WeightedUFPathCompression.prototype.find = function (p) { var root = p, newP; while (root != _privateData[this._id].id[root]) { root = _privateData[this._id].id[root]; } while (p != root) { newP = _privateData[this._id].id[p]; _privateData[this._id].id[p] = root; p = newP; } return root; }; WeightedUFPathCompression.prototype.isConnected = function (p, q) { return this.find(p) == this.find(q); }; WeightedUFPathCompression.prototype.union = function (p,q) { var rootp = this.find(p); var rootq = this.find(q); if (_privateData[this._id].sz[rootp] > _privateData[this._id].sz[rootq]) { _privateData[this._id].id[rootq] = rootp; _privateData[this._id].sz[rootp] += _privateData[this._id].sz[rootq]; } else { _privateData[this._id].id[rootp] = rootq; _privateData[this._id].sz[rootq] += _privateData[this._id].sz[rootp]; } _privateData[this._id].count -= 1; }; return WeightedUFPathCompression; })(); var obj1 = new WeightedUFPathCompression(10); console.log(obj1.count() + " components"); obj1.union(1,2); obj1.union(7,2); console.log(obj1.isConnected(7,1) + " connected"); console.log(obj1.find(7) + " find"); console.log(obj1.count() + " components"); </script> <script id="jsbin-source-javascript" type="text/javascript">var WeightedUFPathCompression = (function () { var _privateData = {}; var _privateId = 0; function WeightedUFPathCompression(numberOfElements) { var i; Object.defineProperty(this, '_id', {value: _privateId++}); _privateData[this._id] = { count: numberOfElements, id: [], sz: [] }; for (i = 0; i < numberOfElements; i++) { _privateData[this._id].id[i] = i; _privateData[this._id].sz[i] = 1; } } WeightedUFPathCompression.prototype.count = function () { return _privateData[this._id].count; }; WeightedUFPathCompression.prototype.find = function (p) { var root = p, newP; while (root != _privateData[this._id].id[root]) { root = _privateData[this._id].id[root]; } while (p != root) { newP = _privateData[this._id].id[p]; _privateData[this._id].id[p] = root; p = newP; } return root; }; WeightedUFPathCompression.prototype.isConnected = function (p, q) { return this.find(p) == this.find(q); }; WeightedUFPathCompression.prototype.union = function (p,q) { var rootp = this.find(p); var rootq = this.find(q); if (_privateData[this._id].sz[rootp] > _privateData[this._id].sz[rootq]) { _privateData[this._id].id[rootq] = rootp; _privateData[this._id].sz[rootp] += _privateData[this._id].sz[rootq]; } else { _privateData[this._id].id[rootp] = rootq; _privateData[this._id].sz[rootq] += _privateData[this._id].sz[rootp]; } _privateData[this._id].count -= 1; }; return WeightedUFPathCompression; })(); var obj1 = new WeightedUFPathCompression(10); console.log(obj1.count() + " components"); obj1.union(1,2); obj1.union(7,2); console.log(obj1.isConnected(7,1) + " connected"); console.log(obj1.find(7) + " find"); console.log(obj1.count() + " components"); </script></body> </html>