Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mathgraph

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mathgraph - npm Package Compare versions

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 @@

@@ -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();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc