union-find
Advanced tools
Comparing version 1.0.0 to 1.0.1
19
index.js
@@ -15,7 +15,11 @@ "use strict"; "use restrict"; | ||
UnionFind.prototype.length = function() { | ||
return this.roots.length; | ||
} | ||
var proto = UnionFind.prototype | ||
UnionFind.prototype.makeSet = function() { | ||
Object.defineProperty(proto, "length", { | ||
"get": function() { | ||
return this.roots.length | ||
} | ||
}) | ||
proto.makeSet = function() { | ||
var n = this.roots.length; | ||
@@ -27,3 +31,3 @@ this.roots.push(n); | ||
UnionFind.prototype.find = function(x) { | ||
proto.find = function(x) { | ||
var roots = this.roots; | ||
@@ -38,3 +42,3 @@ while(roots[x] !== x) { | ||
UnionFind.prototype.link = function(x, y) { | ||
proto.link = function(x, y) { | ||
var xr = this.find(x) | ||
@@ -57,3 +61,2 @@ , yr = this.find(y); | ||
} | ||
} | ||
} |
{ | ||
"name": "union-find", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A union-find data structure for maintaining disjoint sets.", | ||
@@ -20,6 +20,12 @@ "main": "index.js", | ||
], | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
}, | ||
"author": "Mikola Lysenko", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"gitHead": "8fbd75feecd9d7154f4c2ff21754f483ad07ccab" | ||
"gitHead": "8fbd75feecd9d7154f4c2ff21754f483ad07ccab", | ||
"devDependencies": { | ||
"tape": "^2.12.3" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
`union-find` | ||
union-find | ||
========== | ||
@@ -8,2 +8,3 @@ | ||
Union find data structures solve the incremental connectivity problem. (That is maintaining a spanning forest under incremental insertions of edges.) To handle fully dynamic connectivity, you can use a [dynamic forest](https://www.npmjs.org/package/dynamic-forest) data structure. | ||
@@ -14,26 +15,72 @@ Usage | ||
//Import data structure | ||
var UnionFind = require('union-find'); | ||
//Link all the nodes together | ||
var forest = new UnionFind(VERTEX_COUNT); | ||
for(var i=0; i<edges.length; ++i) { | ||
forest.link(edges[i][0], edges[i][1]); | ||
} | ||
//Label components | ||
var labels = new Array(VERTEX_COUNT); | ||
for(var i=0; i<VERTEX_COUNT; ++i) { | ||
labels[i] = forest.find(i); | ||
} | ||
```javascript | ||
//Import data structure | ||
var UnionFind = require('union-find') | ||
var VERTEX_COUNT = 8 | ||
var edges = [ | ||
[0,1], | ||
[1,2], | ||
[2,3], | ||
[5,6], | ||
[7,1] | ||
] | ||
//Link all the nodes together | ||
var forest = new UnionFind(VERTEX_COUNT) | ||
for(var i=0; i<edges.length; ++i) { | ||
forest.link(edges[i][0], edges[i][1]) | ||
} | ||
//Label components | ||
var labels = new Array(VERTEX_COUNT) | ||
for(var i=0; i<VERTEX_COUNT; ++i) { | ||
labels[i] = forest.find(i) | ||
} | ||
``` | ||
Installation | ||
============ | ||
npm install union-find | ||
``` | ||
npm install union-find | ||
``` | ||
# API | ||
```javascript | ||
var UnionFind = require('union-find') | ||
``` | ||
## Constructor | ||
### `var forest = new UnionFind(numVertices)` | ||
Creates a new union-find data structure. | ||
* `numVertices` is the number of vertices in the graph | ||
**Returns** A new union-find data structure | ||
## Methods | ||
### `forest.length` | ||
Returns the number of vertices in the forest | ||
### `forest.makeSet()` | ||
Creates a new vertex | ||
**Returns** An integer id for the new vertex | ||
### `forest.find(v)` | ||
Returns an identifier representing the connected component of any given vertex | ||
**Returns** An integer id representing the connected component of `v` | ||
### `forest.link(s, t)` | ||
Links a pair of connected components together | ||
* `s` and `t` are both vertices | ||
Acknowledgements | ||
================ | ||
(c) 2013 Mikola Lysenko. MIT License | ||
Credits | ||
======= | ||
(c) 2013-2014 Mikola Lysenko. MIT License |
Sorry, the diff of this file is not supported yet
6011
7
96
84
1