bacon-theory
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name" : "bacon-theory", | ||
"description" : "network / graph theory library for node and the browser", | ||
"keyword" : [ | ||
"graph", | ||
"graph theory", | ||
"network graph", | ||
"small-world", | ||
"node graph" | ||
], | ||
"version" : "0.1.0", | ||
"author" : { | ||
"name" : "Mike Monteith" | ||
}, | ||
"main" : "bacon.js", | ||
"devDependencies" : { | ||
"mocha" : "*" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/mike220889/bacon" | ||
} | ||
"name": "bacon-theory", | ||
"description": "network / graph theory library for node and the browser", | ||
"keyword": [ | ||
"graph", | ||
"graph theory", | ||
"network graph", | ||
"small-world", | ||
"node graph" | ||
], | ||
"version": "0.2.0", | ||
"author": { | ||
"name": "Mike Monteith" | ||
}, | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.0.1", | ||
"grunt": "~0.4.5", | ||
"grunt-browserify": "~3.2.1", | ||
"grunt-contrib-watch": "~0.6.1", | ||
"grunt-contrib-jshint": "~0.10.0", | ||
"grunt-jsdoc": "^0.5.7", | ||
"grunt-mocha-test": "~0.12.4" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/mike220889/bacon" | ||
}, | ||
"dependencies": { | ||
"extend": "~2.0.0", | ||
"is-integer": "~1.0.3" | ||
} | ||
} |
@@ -6,2 +6,4 @@ Bacon Theory | ||
What is graph theory? [Wikipedia](http://en.wikipedia.org/wiki/Graph_theory) | ||
```js | ||
@@ -24,2 +26,66 @@ var bt = require('bacon-theory'); | ||
$npm install -g bacon-theory | ||
``` | ||
$ npm install -g bacon-theory | ||
``` | ||
# API | ||
```js | ||
var bt = require('bacon-theory'); | ||
``` | ||
For full documentation, see the source or build the documentation ```grunt jsdoc``` | ||
## Graph | ||
```js | ||
var graph = new bt.Graph(); | ||
``` | ||
## Node | ||
```js | ||
var graph = bt.Graph(); | ||
var node = bt.Node(); | ||
graph.addNode(node); | ||
graph.nNodes(); // = 1 | ||
``` | ||
## Edge | ||
```js | ||
var graph = bt.Graph(); //create a graph | ||
graph.addNode(); //add a node | ||
graph.addNode(); //add another node | ||
graph.getNode(0).addEdge(graph.getNode(1)); //connect node #0 and node #1 with an edge | ||
``` | ||
## autocreate | ||
Pass in an array of arrays which represents a collection of nodes. | ||
Integers as array values specify edges connecting to the n-th node (zero-indexed) | ||
```options.directed``` specifies if the edges are directional (true) or bi-directed (false, default) | ||
```js | ||
//creates a graph where the 0th node is connected to node #1, 1st node connected to node #2 and #3 etc... | ||
var options = { | ||
directed: false | ||
}; | ||
var graph = bt.autocreate([ | ||
[1], | ||
[2,3], | ||
[0], | ||
[0] | ||
], options); | ||
``` | ||
# TODO | ||
* Provide helpers for calculating common graph properties | ||
* Allow non-integer edge weights | ||
* Export graph to JSON format | ||
* Add in-browser tests | ||
* More documentation and examples |
var assert = require('assert'); | ||
var bacon = require('../bacon.js'); | ||
var bacon = require('../src/index.js'); | ||
@@ -65,2 +65,19 @@ describe('autocreate functions', function(){ | ||
}); | ||
describe('graph with repeat edges', function(){ | ||
var graph = bacon.autocreate([ | ||
[1,2,3], | ||
[0,0,3], | ||
[0], | ||
[0] | ||
]); | ||
it('has 4 nodes', function(){ | ||
assert.strictEqual(4, graph.nNodes()); | ||
}); | ||
it('has correct number of edges', function(){ | ||
assert.strictEqual(7, graph.getNode(0).nEdges()); | ||
assert.strictEqual(4, graph.getNode(1).nEdges()); | ||
assert.strictEqual(2, graph.getNode(2).nEdges()); | ||
assert.strictEqual(3, graph.getNode(3).nEdges()); | ||
}); | ||
}); | ||
}); | ||
@@ -89,2 +106,49 @@ describe('creates multiple graphs', function(){ | ||
}); | ||
describe('directed graph option', function(){ | ||
var data = [ | ||
[1,3], | ||
[], | ||
[3,0], | ||
[0] | ||
]; | ||
it('directed = true', function(){ | ||
var graph = bacon.autocreate(data, { | ||
directed: true, | ||
}); | ||
assert(graph instanceof bacon.Graph); | ||
assert(graph.getNode(0).isNeighbour(graph.getNode(1)), "0 -> 1"); | ||
assert(!graph.getNode(1).isNeighbour(graph.getNode(0)), "1 !-> 0") | ||
assert(graph.getNode(2).isNeighbour(graph.getNode(0)), "2 -> 0") | ||
assert(!graph.getNode(0).isNeighbour(graph.getNode(2)), "0 !-> 2") | ||
assert(!graph.getNode(1).isNeighbour(graph.getNode(2)), "1 !-> 2") | ||
assert(graph.getNode(3).isNeighbour(graph.getNode(0)), "3 -> 0"); | ||
assert(graph.getNode(0).isNeighbour(graph.getNode(3)), "0 -> 3"); | ||
assert.strictEqual(4, graph.getNode(0).nEdges()); | ||
assert.strictEqual(1, graph.getNode(1).nEdges()); | ||
assert.strictEqual(2, graph.getNode(2).nEdges()); | ||
assert.strictEqual(3, graph.getNode(3).nEdges()); | ||
}); | ||
it('directed = false', function(){ | ||
var graph = bacon.autocreate(data, { | ||
directed: false, | ||
}); | ||
assert(graph instanceof bacon.Graph); | ||
assert(graph.getNode(0).isNeighbour(graph.getNode(1)), "0 -> 1"); | ||
assert(graph.getNode(1).isNeighbour(graph.getNode(0)), "1 -> 0") | ||
assert(graph.getNode(2).isNeighbour(graph.getNode(0)), "2 -> 0") | ||
assert(graph.getNode(0).isNeighbour(graph.getNode(2)), "0 -> 2") | ||
assert(!graph.getNode(1).isNeighbour(graph.getNode(2)), "1 !-> 2") | ||
assert(graph.getNode(3).isNeighbour(graph.getNode(0)), "3 -> 0"); | ||
assert(graph.getNode(0).isNeighbour(graph.getNode(3)), "0 -> 3"); | ||
assert.strictEqual(4, graph.getNode(0).nEdges()); | ||
assert.strictEqual(1, graph.getNode(1).nEdges()); | ||
assert.strictEqual(2, graph.getNode(2).nEdges()); | ||
assert.strictEqual(3, graph.getNode(3).nEdges()); | ||
}); | ||
}); | ||
}); |
var assert = require('assert'); | ||
var bacon = require('../bacon.js'); | ||
var bacon = require('../src/index.js'); | ||
@@ -34,9 +34,9 @@ describe('Edge', function(){ | ||
}); | ||
it('throws an error when nodes are already connected', function(){ | ||
it('allows new edge, even if nodes are already connected', function(){ | ||
var node = bacon.Node(); | ||
var node2 = bacon.Node(); | ||
var edge = bacon.Edge(node, node2); | ||
assert.throws(function(){ | ||
assert.doesNotThrow(function(){ | ||
var repeat_edge = bacon.Edge(node, node2); | ||
}, /Nodes are already connected/); | ||
}); | ||
}); | ||
@@ -43,0 +43,0 @@ }); |
var assert = require('assert'); | ||
var bacon = require('../bacon.js'); | ||
var bacon = require('../src/index.js'); | ||
@@ -204,2 +204,100 @@ describe('Graph', function(){ | ||
}); | ||
it('order of node1 and node2 shouldn\'t affect the returned edge', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2); | ||
var edge1 = node1.getEdge(node2); | ||
var edge2 = node2.getEdge(node1); | ||
assert.deepEqual(node1, node2); | ||
}); | ||
it('empty or false third argument defines edge weight = 1 in both directions', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
var node3 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2).addNode(node3); | ||
graph.addEdge(node1, node2); | ||
graph.addEdge(node2, node3, false); | ||
var edge12 = node1.getEdge(node2); | ||
var edge23 = node2.getEdge(node3); | ||
assert.deepEqual(edge12.weights, [1, 1]); | ||
assert.deepEqual(edge23.weights, [1, 1]); | ||
}); | ||
it('third argument = true, defines directed edge weight = 1 from node1 to node2', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2, true); | ||
var edge = node1.getEdge(node2); | ||
var halfEdge1 = edge.getHalfEdge(node2); | ||
var halfEdge2 = edge.getHalfEdge(node1); | ||
assert.equal(halfEdge1.weight, 1); | ||
assert.equal(halfEdge2, null); | ||
}); | ||
it('third argument is numeric, defines weighted edge', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2, 9); | ||
var edge = node1.getEdge(node2); | ||
var halfEdge1 = edge.getHalfEdge(node2); | ||
var halfEdge2 = edge.getHalfEdge(node1); | ||
assert.equal(halfEdge1.weight, 9); | ||
assert.equal(halfEdge2.weight, 9); | ||
}); | ||
it('third argument is numeric, fourth argument = false, defines weighted edge', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2, 42, false); | ||
var edge = node1.getEdge(node2); | ||
var halfEdge1 = edge.getHalfEdge(node2); | ||
var halfEdge2 = edge.getHalfEdge(node1); | ||
assert.equal(halfEdge1.weight, 42); | ||
assert.equal(halfEdge2.weight, 42); | ||
}); | ||
it('third argument is numeric, fourth argument = true, defines directional weighted edge', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2, 42, true); | ||
var edge = node1.getEdge(node2); | ||
var halfEdge1 = edge.getHalfEdge(node2); | ||
var halfEdge2 = edge.getHalfEdge(node1); | ||
assert.equal(halfEdge1.weight, 42); | ||
assert.equal(halfEdge2, null); | ||
}); | ||
it('third and fourth arguments are numeric, defines edge weights in both directions', function(){ | ||
var graph = new bacon.Graph(); | ||
var node1 = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
graph.addNode(node1).addNode(node2); | ||
graph.addEdge(node1, node2, 9, 66); | ||
var edge = node1.getEdge(node2); | ||
var halfEdge1 = edge.getHalfEdge(node2); | ||
var halfEdge2 = edge.getHalfEdge(node1); | ||
assert.equal(halfEdge1.weight, 9); | ||
assert.equal(halfEdge2.weight, 66); | ||
}); | ||
}); | ||
@@ -206,0 +304,0 @@ describe('eachNode()', function(){ |
var assert = require('assert'); | ||
var bacon = require('../bacon.js'); | ||
var bacon = require('../src/index.js'); | ||
@@ -55,9 +55,9 @@ describe('Node', function(){ | ||
}); | ||
it('throws error if edge already exists', function(){ | ||
it('allows new edge, even if nodes are already connected', function(){ | ||
var node = new bacon.Node(); | ||
var node2 = new bacon.Node(); | ||
assert.throws(function(){ | ||
assert.doesNotThrow(function(){ | ||
node.addEdge(node2); | ||
node.addEdge(node2); | ||
}, /Nodes are already connected/); | ||
}); | ||
}); | ||
@@ -64,0 +64,0 @@ }); |
44590
18
1331
90
2
7
+ Addedextend@~2.0.0
+ Addedis-integer@~1.0.3
+ Addedextend@2.0.2(transitive)
+ Addedis-finite@1.1.0(transitive)
+ Addedis-integer@1.0.7(transitive)