Comparing version 0.0.0 to 0.0.1
34
graph.js
@@ -0,5 +1,7 @@ | ||
// graph.js | ||
// https://github.com/DubFriend/graph.js | ||
(function () { | ||
'use strict'; | ||
var _ = _; | ||
var _; | ||
@@ -77,3 +79,3 @@ var mapListToGraph = function (list) { | ||
if(module && module.exports) { | ||
if(typeof exports !== 'undefined') { | ||
module.exports = GraphJS; | ||
@@ -84,2 +86,3 @@ _ = require('underscore'); | ||
this.GraphJS = GraphJS; | ||
_ = window._; | ||
} | ||
@@ -176,13 +179,22 @@ | ||
GraphJS.prototype.isForest = function () { | ||
var numberOfVertices = Object.keys(this.referenceDictionary).length; | ||
var isForest = true; | ||
var numberOfEdges = _.reduce( | ||
this.referenceDictionary, | ||
function (acc, node) { | ||
return acc + node.links.length; | ||
}, | ||
0 | ||
); | ||
_.each(this.referenceDictionary, function (node) { | ||
if(!node.discovered) { | ||
depthFirstSearch( | ||
node, | ||
function () { return false; }, | ||
function () { | ||
isForest = false; | ||
} | ||
); | ||
} | ||
}); | ||
return numberOfEdges === numberOfVertices - 1 && !this.hasCycles(); | ||
// cleanup | ||
_.each(this.referenceDictionary, function (node) { | ||
delete node.discovered; | ||
}); | ||
return isForest; | ||
}; | ||
@@ -189,0 +201,0 @@ |
{ | ||
"name": "mathgraph", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "graph utilities", | ||
@@ -5,0 +5,0 @@ "main": "graph.js", |
#GraphJS | ||
nodejs | ||
`npm install mathgraph` | ||
browser | ||
```html | ||
<script src="path/to/underscore.js"></script> | ||
<script src="path/to/graph.js"></script> | ||
``` | ||
Create a new directed graph from a list | ||
@@ -4,0 +13,0 @@ |
52
test.js
@@ -115,6 +115,49 @@ var GraphJS = require('./graph'); | ||
exports.isConnected = function (test) { | ||
test.strictEqual(this.graph.isConnected(), true, 'test true'); | ||
test.strictEqual(this.graph.isConnected(), true, 'test true: idempotence'); | ||
test.strictEqual(new GraphJS([ | ||
{ id: 1, data: 'a', link: 2 }, | ||
{ id: 2, data: 'b' }, | ||
{ id: 3, data: 'c' } | ||
]).isConnected(), false, 'test false: without cycles'); | ||
test.done(); | ||
}; | ||
exports.isForest = function (test) { | ||
test.strictEqual(this.graph.isForest(), false, 'test false: has cycles'); | ||
test.strictEqual(new GraphJS([ | ||
{ id: 1, data: 'a', link: [2, 3] }, | ||
{ id: 2, data: 'b', link: 4 }, | ||
{ id: 3, data: 'c' }, | ||
{ id: 4, data: 'd' } | ||
]).isForest(), true, 'test true: connected'); | ||
test.strictEqual(new GraphJS([ | ||
{ id: 1, data: 'a', link: [2, 3] }, | ||
{ id: 2, data: 'b', link: 4 }, | ||
{ id: 3, data: 'c' }, | ||
{ id: 4, data: 'd' }, | ||
{ id: 5, data: 'e' } | ||
]).isForest(), true, 'test true: disconnected'); | ||
test.strictEqual(new GraphJS([ | ||
{ id: 1, data: 'a', link: [2, 3] }, | ||
{ id: 2, data: 'b', link: 4 }, | ||
{ id: 3, data: 'c', link: 4 }, | ||
{ id: 4, data: 'd' } | ||
]).isForest(), false, 'test false: diamond shaped'); | ||
test.done(); | ||
}; | ||
exports.isTree = function (test) { | ||
test.strictEqual(this.graph.isTree(), false, 'test 1'); | ||
// testing idempotence. | ||
test.strictEqual(this.graph.isTree(), false, 'test 1.a'); | ||
test.strictEqual(new GraphJS([ | ||
@@ -126,2 +169,3 @@ { id: 1, data: 'a', link: [2, 3] }, | ||
]).isTree(), true, 'test 2'); | ||
test.strictEqual(new GraphJS([ | ||
@@ -132,3 +176,11 @@ { id: 1, data: 'a', link: [2, 3] }, | ||
]).isTree(), false, 'test 3'); | ||
test.strictEqual(new GraphJS([ | ||
{ id: 1, data: 'a', link: [2, 3] }, | ||
{ id: 2, data: 'b', link: 4 }, | ||
{ id: 3, data: 'c', link: 4 }, | ||
{ id: 4, data: 'd' } | ||
]).isForest(), false, 'test false: diamond shaped'); | ||
test.done(); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13297
340
65