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

graph

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graph - npm Package Compare versions

Comparing version 0.0.3 to 0.1.0

test.html

6

lib/graph.js

@@ -6,5 +6,5 @@ (function ()

var Graph = function (graph) {
this._graph = {} // {u: {v: edge, ...}, ...}
this._degree = {} // {u: degree, ...}
this._indegree = {} // {u: degree, ...}
this._graph = {}; // {u: {v: edge, ...}, ...}
this._degree = {}; // {u: degree, ...}
this._indegree = {}; // {u: degree, ...}
this._vertices = []; // [u, v, ...]

@@ -11,0 +11,0 @@ this._vertex = {}; // {u: u, ...}

{
"name": "graph",
"description": "library for manipulating directed and undirected graphs",
"version": "0.0.3",
"version": "0.1.0",
"homepage": "http://github.johntantalo.com/graphjs/",

@@ -16,5 +16,7 @@ "repository": {

"devDependencies": {
"steel": "0.0.1"
"nodeunit": "latest"
},
"scripts": {"test": "test/all.js"},
"scripts": {
"test": "./node_modules/.bin/nodeunit test"
},
"engines": {

@@ -21,0 +23,0 @@ "node": "*",

@@ -9,4 +9,2 @@ # GraphJS

GraphJS uses [QUnit](http://docs.jquery.com/Qunit) and [steel](https://github.com/tantalor/steel) for testing.
## Usage

@@ -94,83 +92,10 @@

GraphJS is packaged with tests for several different environments.
GraphJS is packaged with `nodeunit` tests.
### Browser (QUnit)
The easiest way to run the tests is with `npm test`.
_see [QUnit](http://docs.jquery.com/Qunit)_
$ npm test
...
OK: 173 assertions (23ms)
Load the `graphjs/test/all.html` page in your favorite browser.
Tests completed in 88 milliseconds.
0 tests of 160 failed.
### Narwhal
_see [Narwhal](http://narwhaljs.org/)_
You can run the separate tests with `narwhal`.
$ narwhal -m test test/core.js
+ Running test/core.js
Passes: 24, Fails: 0, Errors: 0
Or all tests.
$ narwhal test/all.js
+ Running
Passes: 24, Fails: 0, Errors: 0
+ Running
Passes: 14, Fails: 0, Errors: 0
### Node
_see [Node](http://nodejs.org/)_
You can run the individual tests with `node`.
$ node test/core.js
+ Running
Passes: 24, Fails: 0, Errors: 0
Or all tests.
$ node test/all.js
+ Running
Passes: 24, Fails: 0, Errors: 0
+ Running
Passes: 14, Fails: 0, Errors: 0
### JavaScriptCore
_see [JavaScriptCore](http://webkit.org/projects/javascript/)_
You can run the separate tests with `jsc`.
$ jsc test/core.js
+ Running
Passes: 24, Fails: 0, Errors: 0
Or all tests.
$ jsc test/all.js
+ Running
Passes: 24, Fails: 0, Errors: 0
+ Running
Passes: 14, Fails: 0, Errors: 0
### RingoJS
_see [RingoJS](http://ringojs.org/)_
You can run the separate tests with `ringo`.
$ ringo test/core.js
+ Running
Passes: 24, Fails: 0, Errors: 0
Or all the tests.
$ ringo test/all.js
+ Running
Passes: 24, Fails: 0, Errors: 0
+ Running
Passes: 14, Fails: 0, Errors: 0
You can also test your browser by loading the `test.html` page.
if (typeof(require) !== 'undefined') {
// commonjs
var QUnit = require('steel');
var Graph = require("../lib/graph").Graph;
} else if (typeof(load) !== 'undefined') {
// jsc
var QUnit = load('steel.js');
var Graph = load("lib/graph.js").Graph;
}
with (QUnit)
this.core_suite =
{
test('Graph exists', function ()
'Graph exists': function (test)
{
ok(Graph,
test.ok(Graph,
"I can find the Graph class.");
});
test.done();
},
test('Null get', function ()
'Null get': function (test)
{
var g = new Graph();
ok(g.get(1, 2) === undefined,
test.ok(g.get(1, 2) === undefined,
"Get for unknown edge returns undef.");
ok(g.has(1, 2) === false,
test.ok(g.has(1, 2) === false,
"Has for unknown edge returns false.");
});
test.done();
},
test('Bad delete', function ()
'Bad delete': function (test)
{
var g = new Graph();
g.del(1, 2);
ok(g.degree(1) === 0,
test.ok(g.degree(1) === 0,
"Degree of one vertex is 0.");
ok(g.degree(2) === 0,
test.ok(g.degree(2) === 0,
"Degree of other vertex is 0.");
ok(g.size() === 0,
test.ok(g.size() === 0,
"Size is 0.");
});
test.done();
},
test('Simple get', function ()
'Simple get': function (test)
{
var g = new Graph();
ok(g.set(1, 2, 3) === 3,
test.ok(g.set(1, 2, 3) === 3,
"Set returns edge weight.");
ok(g.get(1, 2) === 3,
test.ok(g.get(1, 2) === 3,
"Get returns edge weight.");
});
test.done();
},
test('Set and get', function ()
'Set and get': function (test)
{
var g = new Graph();
g.set(1, 2, 3);
ok(g.get(1, 2) === 3,
test.ok(g.get(1, 2) === 3,
"Get with original order returns weight.");
ok(g.get(2, 1) === 3,
test.ok(g.get(2, 1) === 3,
"Get with reveresed order returns weight.");
ok(g.order() === 2,
test.ok(g.order() === 2,
"Number of vertices is 2.");
ok(g.degree(1) === 1,
test.ok(g.degree(1) === 1,
"Degree of one vertex is 1.");
ok(g.degree(2) === 1,
test.ok(g.degree(2) === 1,
"Degree of other vertex is 1.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
});
test.done();
},
test('Set and delete', function ()
'Set and delete': function (test)
{
var g = new Graph();
ok(g.set(1, 2),
test.ok(g.set(1, 2),
"Added edge.")
ok(g.del(1, 2) === false,
test.ok(g.del(1, 2) === false,
"Deleted edge.")
ok(!g.has(1, 2),
test.ok(!g.has(1, 2),
"Deleted edge doesn't exist.");
ok(!g.has(2, 1),
test.ok(!g.has(2, 1),
"Reverse of edge also doesn't exist.");
ok(g.order() === 2,
test.ok(g.order() === 2,
"Number of vertices is 2.");
ok(g.degree(1) === 0,
test.ok(g.degree(1) === 0,
"Degree of one vertex is 0.");
ok(g.degree(2) === 0,
test.ok(g.degree(2) === 0,
"Degree of other vertex is 0.");
ok(g.size() === 0,
test.ok(g.size() === 0,
"Size is 0.");
});
test.done();
},
test('Set and reverse set', function ()
'Set and reverse set': function (test)
{

@@ -93,112 +97,119 @@ var g = new Graph();

g.set(2, 1, 4);
ok(g.get(1, 2) === 4,
test.ok(g.get(1, 2) === 4,
"Get with original order returns new weight.");
ok(g.get(2, 1) === 4,
test.ok(g.get(2, 1) === 4,
"Get with reversed order returns new weight.");
ok(g.order() === 2,
test.ok(g.order() === 2,
"Number of vertices is 2.");
ok(g.degree(1) === 1,
test.ok(g.degree(1) === 1,
"Degree of one vertex is 1.");
ok(g.degree(2) === 1,
test.ok(g.degree(2) === 1,
"Degree of other vertex is 1.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
});
test.done();
},
test('Set and reverse delete', function ()
'Set and reverse delete': function (test)
{
var g = new Graph();
ok(g.set(1, 2),
test.ok(g.set(1, 2),
"Added edge.")
ok(g.del(2, 1) === false,
test.ok(g.del(2, 1) === false,
"Deleted edge.");
ok(!g.has(1, 2),
test.ok(!g.has(1, 2),
"Deleted edge doesn't exist.");
ok(!g.has(2, 1),
test.ok(!g.has(2, 1),
"Reverse of edge also doesn't exist.");
ok(g.order() === 2,
test.ok(g.order() === 2,
"Number of vertices is 2.");
ok(g.degree(1) === 0,
test.ok(g.degree(1) === 0,
"Degree of one vertex is 0.");
ok(g.degree(2) === 0,
test.ok(g.degree(2) === 0,
"Degree of other vertex is 0.");
ok(g.size() === 0,
test.ok(g.size() === 0,
"Size is 0.");
});
test.done();
},
test('Self edge', function ()
'Self edge': function (test)
{
var g = new Graph();
ok(g.set(1, 1, 2) == 2,
test.ok(g.set(1, 1, 2) == 2,
"Set self edge returns weight.");
ok(g.get(1, 1) === 2,
test.ok(g.get(1, 1) === 2,
"Get self edge returns weight.");
ok(g.order() === 1,
test.ok(g.order() === 1,
"Number of vertices is 1.");
ok(g.degree(1) === 1,
test.ok(g.degree(1) === 1,
"Degree of vertex is 1.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
});
test.done();
},
test('Simple constructor', function ()
'Simple constructor': function (test)
{
var g = new Graph({pirate: ['ninja', 'robot']});
ok(g.get('pirate', 'ninja') && g.get('pirate', 'robot'),
test.ok(g.get('pirate', 'ninja') && g.get('pirate', 'robot'),
"All edges exist.");
ok(g.order() === 3,
test.ok(g.order() === 3,
"Number of vertices is 3.");
ok(g.degree('pirate') === 2,
test.ok(g.degree('pirate') === 2,
"Degree of 'pirate' vertex is 2.");
ok(g.degree('ninja') === 1,
test.ok(g.degree('ninja') === 1,
"Degree of 'ninja' vertex is 1.");
ok(g.degree('robot') === 1,
test.ok(g.degree('robot') === 1,
"Degree of 'robot' vertex is 1.");
ok(g.size() === 2,
test.ok(g.size() === 2,
"Size is 2.");
});
test.done();
},
test('Constructor with weights', function ()
'Constructor with weights': function (test)
{
var g = new Graph({pirate: {ninja: 'robot'}});
ok(g.get('pirate', 'ninja') === 'robot',
test.ok(g.get('pirate', 'ninja') === 'robot',
"Get in original order has weight 'robot'.");
ok(g.get('ninja', 'pirate') === 'robot',
test.ok(g.get('ninja', 'pirate') === 'robot',
"Get in reversed order has weight 'robot'.");
ok(g.order() === 2,
test.ok(g.order() === 2,
"Number of vertices is 2.");
ok(g.degree('pirate') === 1,
test.ok(g.degree('pirate') === 1,
"Degree of 'pirate' vertex is 1.");
ok(g.degree('ninja') === 1,
test.ok(g.degree('ninja') === 1,
"Degree of 'ninja' vertex is 1.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
});
test.done();
},
test('Multiget', function ()
'Multiget': function (test)
{
var g = new Graph();
ok(g.set(1, 2) && g.set(2, 3) && g.set(3, 1),
test.ok(g.set(1, 2) && g.set(2, 3) && g.set(3, 1),
"Set all edges.");
ok(g.get(2, 1) && g.get(3, 2) && g.get(1, 3),
test.ok(g.get(2, 1) && g.get(3, 2) && g.get(1, 3),
"All edges exist.");
ok(g.degree(1) == 2 && g.degree(2) == 2 && g.degree(3) == 2,
test.ok(g.degree(1) == 2 && g.degree(2) == 2 && g.degree(3) == 2,
"Degree of all vertices is 2.");
ok(g.order() === 3,
test.ok(g.order() === 3,
"Number of vertices is 3.");
ok(g.size() === 3,
test.ok(g.size() === 3,
"Size is 3.");
});
test.done();
},
test('Adjacency', function ()
'Adjacency': function (test)
{
var g = new Graph();
g.set(1, 2);
ok(1 in g.adj(2),
test.ok(1 in g.adj(2),
"Vertex 1 is adjacent to vertex 2.");
ok(2 in g.adj(1),
test.ok(2 in g.adj(1),
"Vertex 2 is adjacent to vertex 1.");
});
test.done();
},
test('Depth-first search', function ()
'Depth-first search': function (test)
{

@@ -220,12 +231,13 @@ var g = new Graph({

visit(1);
ok(visited[1], "Visited vertex 1.");
ok(visited[2], "Visited vertex 2.");
ok(visited[3], "Visited vertex 3.");
ok(visited[4], "Visited vertex 4.");
ok(visited[5], "Visited vertex 5.");
ok(visited[6], "Visited vertex 6.");
ok(visited[7], "Visited vertex 7.");
});
test.ok(visited[1], "Visited vertex 1.");
test.ok(visited[2], "Visited vertex 2.");
test.ok(visited[3], "Visited vertex 3.");
test.ok(visited[4], "Visited vertex 4.");
test.ok(visited[5], "Visited vertex 5.");
test.ok(visited[6], "Visited vertex 6.");
test.ok(visited[7], "Visited vertex 7.");
test.done();
},
test('Breadth-first search', function ()
'Breadth-first search': function (test)
{

@@ -248,12 +260,13 @@ var g = new Graph({

}
ok(visited[1], "Visited vertex 1.");
ok(visited[2], "Visited vertex 2.");
ok(visited[3], "Visited vertex 3.");
ok(visited[4], "Visited vertex 4.");
ok(visited[5], "Visited vertex 5.");
ok(visited[6], "Visited vertex 6.");
ok(visited[7], "Visited vertex 7.");
});
test.ok(visited[1], "Visited vertex 1.");
test.ok(visited[2], "Visited vertex 2.");
test.ok(visited[3], "Visited vertex 3.");
test.ok(visited[4], "Visited vertex 4.");
test.ok(visited[5], "Visited vertex 5.");
test.ok(visited[6], "Visited vertex 6.");
test.ok(visited[7], "Visited vertex 7.");
test.done();
},
test('Simple copy', function ()
'Simple copy': function (test)
{

@@ -265,21 +278,22 @@ var g = new Graph({

var h = g.copy();
ok(g.get(2, 3),
test.ok(g.get(2, 3),
"Original graph has edge.")
ok(g.del(2, 3) === false,
test.ok(g.del(2, 3) === false,
"Deleted edge in original graph.");
ok(!g.has(2, 3),
test.ok(!g.has(2, 3),
"Original graph does not have deleted edge.")
ok(h.get(2, 3),
test.ok(h.get(2, 3),
"Copied graph has deleted edge.");
ok(g.size() == 2,
test.ok(g.size() == 2,
"Original graph has size 2.");
ok(h.size() == 3,
test.ok(h.size() == 3,
"Copied graph has size 3.");
ok(g.degree(2) == 1,
test.ok(g.degree(2) == 1,
"Degree of vertex 1 in original is 1.")
ok(h.degree(2) == 2,
test.ok(h.degree(2) == 2,
"Degree of vertex 1 in copy is 2.")
});
test.done();
},
test('Copy with weights', function ()
'Copy with weights': function (test)
{

@@ -290,13 +304,14 @@ var g = new Graph({

var h = g.copy();
ok(g.get(1, 2) === 3,
test.ok(g.get(1, 2) === 3,
"Original graph has edge with weight.")
ok(g.del(1, 2) === false,
test.ok(g.del(1, 2) === false,
"Deleted edge in original graph.");
ok(!g.has(1, 2),
test.ok(!g.has(1, 2),
"Original graph does not have deleted edge.")
ok(h.get(1, 2) == 3,
test.ok(h.get(1, 2) == 3,
"Copied graph has deleted edge with weight.");
});
test.done();
},
test('Repeated vertices', function ()
'Repeated vertices': function (test)
{

@@ -308,7 +323,8 @@ var g = new Graph();

ok(g.order() === 2,
test.ok(g.order() === 2,
"Graph has 2 vertices.");
});
test.done();
},
test('Add falsey weight', function ()
'Add falsey weight': function (test)
{

@@ -318,7 +334,8 @@ var g = new Graph();

ok(g.size() === 1,
test.ok(g.size() === 1,
"Graph has 1 edge.");
});
test.done();
},
test('Remove falsey weight', function ()
'Remove falsey weight': function (test)
{

@@ -329,7 +346,8 @@ var g = new Graph();

ok(g.size() === 0,
test.ok(g.size() === 0,
"Graph has 0 edges.");
});
test.done();
},
test('Add directed edges', function ()
'Add directed edges': function (test)
{

@@ -339,28 +357,29 @@ var g = new Graph();

ok(g.order() === 2,
test.ok(g.order() === 2,
"Order is 2.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
ok(g.has(1, 2),
test.ok(g.has(1, 2),
"1 ~ 2");
ok(!g.has(2, 1),
test.ok(!g.has(2, 1),
"2 !~ 1");
ok(g.degree(1) === 1,
test.ok(g.degree(1) === 1,
"Out degrees of 1 is 1.");
ok(g.degree(2) === 0,
test.ok(g.degree(2) === 0,
"Out degrees of 2 is 0.");
ok(g.indegree(1) === 0,
test.ok(g.indegree(1) === 0,
"Out degrees of 1 is 0.");
ok(g.indegree(2) === 1,
test.ok(g.indegree(2) === 1,
"Out degrees of 2 is 1.");
});
test.done();
},
test('Remove directed edges', function ()
'Remove directed edges': function (test)
{

@@ -371,28 +390,29 @@ var g = new Graph();

ok(g.order() === 2,
test.ok(g.order() === 2,
"Order is 2.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
ok(g.has(1, 2),
test.ok(g.has(1, 2),
"1 ~ 2");
ok(!g.has(2, 1),
test.ok(!g.has(2, 1),
"2 !~ 1");
ok(g.degree(1) === 1,
test.ok(g.degree(1) === 1,
"Out degrees of 1 is 1.");
ok(g.degree(2) === 0,
test.ok(g.degree(2) === 0,
"Out degrees of 2 is 0.");
ok(g.indegree(1) === 0,
test.ok(g.indegree(1) === 0,
"Out degrees of 1 is 0.");
ok(g.indegree(2) === 1,
test.ok(g.indegree(2) === 1,
"Out degrees of 2 is 1.");
});
test.done();
},
test('Double directed edges', function ()
'Double directed edges': function (test)
{

@@ -403,13 +423,14 @@ var g = new Graph();

ok(g.order() === 2,
test.ok(g.order() === 2,
"Order is 2.");
ok(g.size() === 1,
test.ok(g.size() === 1,
"Size is 1.");
ok(g.has(1, 2) && g.has(2, 1),
test.ok(g.has(1, 2) && g.has(2, 1),
"1 ~ 2 and 2 ~ 1");
});
test.done();
},
test('Drop vertex', function ()
'Drop vertex': function (test)
{

@@ -422,17 +443,16 @@ // var g = Graph.k(4, ['a', 'b', 'c', 'd']);

});
ok(!g.drop('z'),
test.ok(!g.drop('z'),
"Can't drop a vertex that isn't in the graph.")
ok(g.size() === 6 && g.order() === 4,
test.ok(g.size() === 6 && g.order() === 4,
"Graph is K(4).");
ok(g.drop('a'),
test.ok(g.drop('a'),
"Dropped a vertex.");
ok(g.size() === 3 && g.order() === 3,
test.ok(g.size() === 3 && g.order() === 3,
"K(4) is now K(3).")
ok(g.del('b'),
test.ok(g.del('b'),
"Dropped another vertex.");
ok(g.size() === 1 && g.order() === 2,
"K(3) is now K(2).")
});
}
if (QUnit.run) QUnit.run(typeof exports !== 'undefined' ? exports : undefined);
test.ok(g.size() === 1 && g.order() === 2,
"K(3) is now K(2).");
test.done();
}
};
if (typeof(require) !== 'undefined') {
// commonjs
var QUnit = require('steel');
var Graph = require("../lib/extras").Graph;
} else if (typeof(load) !== 'undefined') {
// jsc
var QUnit = load('steel.js');
var Graph = load("lib/extras.js").Graph;
}
with (QUnit)
this.extra_suite =
{
test('Cartesian product', function ()
'Cartesian product': function (test)
{

@@ -26,26 +24,27 @@ var g = new Graph();

ok(gh.order() === 6,
test.ok(gh.order() === 6,
"Order is 6.");
ok(gh.size() === 12,
test.ok(gh.size() === 12,
"Size is 12.");
ok([1, 3] in gh.adj([1, 4]),
test.ok([1, 3] in gh.adj([1, 4]),
"(1,3) adjacent to (1,4).");
ok([1, 4] in gh.adj([1, 5]),
test.ok([1, 4] in gh.adj([1, 5]),
"(1,4) adjacent to (1,5).");
ok([1, 5] in gh.adj([1, 3]),
test.ok([1, 5] in gh.adj([1, 3]),
"(1,5) adjacent to (1,3).");
ok([2, 3] in gh.adj([2, 4]),
test.ok([2, 3] in gh.adj([2, 4]),
"(2,3) adjacent to (2,4).");
ok([2, 4] in gh.adj([2, 5]),
test.ok([2, 4] in gh.adj([2, 5]),
"(2,4) adjacent to (2,5).");
ok([2, 5] in gh.adj([2, 3]),
test.ok([2, 5] in gh.adj([2, 3]),
"(2,5) adjacent to (2,3).");
ok(gh.grep(function (v) {return v in gh.adj(v)}).length === gh.order(),
test.ok(gh.grep(function (v) {return v in gh.adj(v)}).length === gh.order(),
"Every vertex adjacent to self.")
});
test.done();
},
test('Cartesian self product', function ()
'Cartesian self product': function (test)
{

@@ -60,41 +59,44 @@ var g = new Graph({

ok(h.size() === 18,
test.ok(h.size() === 18,
"Size is 18.");
ok(h.order() === 9,
test.ok(h.order() === 9,
"Order is 9.");
ok(['a', 'b'] in h.adj(['b', 'b']),
test.ok(['a', 'b'] in h.adj(['b', 'b']),
"(a,b) is adjacent to (b,b)");
ok(['c', 'b'] in h.adj(['b', 'b']),
test.ok(['c', 'b'] in h.adj(['b', 'b']),
"(c,b) is adjacent to (b,b)");
ok(['b', 'a'] in h.adj(['b', 'b']),
test.ok(['b', 'a'] in h.adj(['b', 'b']),
"(b,a) is adjacent to (b,b)");
ok(['b', 'c'] in h.adj(['b', 'b']),
test.ok(['b', 'c'] in h.adj(['b', 'b']),
"(b,c) is adjacent to (b,b)");
ok(h.grep(function (v) {return h.degree(v) === 4;}).length === h.order(),
test.ok(h.grep(function (v) {return h.degree(v) === 4;}).length === h.order(),
"Degree of all vertices is 4.");
});
test.done();
},
test('Perterson graph', function ()
'Perterson graph': function (test)
{
var g = Graph.peterson();
ok(g.order() === 10,
test.ok(g.order() === 10,
"Peterson graph has 10 vertices.");
ok(g.size() === 15,
test.ok(g.size() === 15,
"Peterson graph has 15 edges.");
})
test.done();
},
test('Bipartite double cover', function ()
'Bipartite double cover': function (test)
{
var g = Graph.peterson().bipartite_double_cover();
ok(g.order() === 20,
test.ok(g.order() === 20,
"Desargues graph has 20 vertices.")
ok(g.size() === 30,
test.ok(g.size() === 30,
"Desargues graph has 30 vertices.")
});
test.done();
},
test('Complete graphs', function ()
'Complete graphs': function (test)
{

@@ -105,10 +107,11 @@ for (var n = 2; n < 5; n++)

var size = n*(n-1)/2;
ok(g.order() === n,
test.ok(g.order() === n,
"K("+n+") has "+n+" vertices.");
ok(g.size() === size,
test.ok(g.size() === size,
"K("+n+") has "+size+" edges.");
}
});
test.done();
},
test('Cycles', function ()
'Cycles': function (test)
{

@@ -118,12 +121,13 @@ for (var n = 3; n < 6; n++)

var g = Graph.c(n);
ok(g.order() === n,
test.ok(g.order() === n,
"C("+n+") has "+n+" vertices.");
ok(g.size() === n,
test.ok(g.size() === n,
"C("+n+") has "+n+" edges.");
ok(g.grep(function (v) {return (v+1)%n in g.adj(v);}).length === n,
test.ok(g.grep(function (v) {return (v+1)%n in g.adj(v);}).length === n,
"C("+n+") neighbors are adjacent.");
}
});
test.done();
},
test('Union', function ()
'Union': function (test)
{

@@ -134,85 +138,92 @@ var g = new Graph({a: ['b', 'c']});

ok(gh.get('a', 'b') && gh.get('a', 'c') && gh.get('b', 'c'),
test.ok(gh.get('a', 'b') && gh.get('a', 'c') && gh.get('b', 'c'),
"All edges exist.");
ok(gh.order() === 3,
test.ok(gh.order() === 3,
"Order is 3.");
ok(gh.size() === 3,
test.ok(gh.size() === 3,
"Size is 3.");
});
test.done();
},
test('Bipartite testing', function ()
'Bipartite testing': function (test)
{
ok(Graph.k(2).is_bipartite(),
test.ok(Graph.k(2).is_bipartite(),
"K(2) is bipartite.");
ok(!Graph.k(3).is_bipartite(),
test.ok(!Graph.k(3).is_bipartite(),
"K(3) is not bipartite.");
var k2k3 = Graph.k(2, ['a', 'b']).union(Graph.k(3, ['c', 'd', 'e']));
ok(!k2k3.is_bipartite(),
test.ok(!k2k3.is_bipartite(),
"K(2) union K(3) is not bipartite.");
ok(Graph.peterson().bipartite_double_cover().is_bipartite(),
test.ok(Graph.peterson().bipartite_double_cover().is_bipartite(),
"Desargues graph is bipartite.");
});
test.done();
},
test('Completeness testing', function ()
'Completeness testing': function (test)
{
ok(!new Graph({a: ['a'], b: ['b']}).is_complete(),
test.ok(!new Graph({a: ['a'], b: ['b']}).is_complete(),
"Graph with two loops is not complete.");
ok(new Graph().is_complete(),
test.ok(new Graph().is_complete(),
"Null graph is complete.");
ok(new Graph({a: ['b', 'c'], b: ['c']}).is_complete(),
test.ok(new Graph({a: ['b', 'c'], b: ['c']}).is_complete(),
"K(3) is complete.");
});
test.done();
},
test('Cycle testing', function ()
'Cycle testing': function (test)
{
ok(!new Graph({a: ['b'], b: ['c']}).is_cycle(),
test.ok(!new Graph({a: ['b'], b: ['c']}).is_cycle(),
"Path on a, b, c is not a cycle.");
ok(Graph.c(3).is_cycle(),
test.ok(Graph.c(3).is_cycle(),
"C(3) is a cycle.");
ok(!Graph.c(3, ['a', 'b', 'c']).union(Graph.c(4, ['d', 'e', 'f', 'g'])).is_cycle(),
test.ok(!Graph.c(3, ['a', 'b', 'c']).union(Graph.c(4, ['d', 'e', 'f', 'g'])).is_cycle(),
"C(3) union C(4) is not a cycle.")
});
test.done();
},
test('Subgraph of C(4)', function ()
'Subgraph of C(4)': function (test)
{
var g = Graph.c(4, ['a', 'b', 'c', 'd']).subgraph(['a', 'b', 'c']);
ok(!g.is_cycle(),
test.ok(!g.is_cycle(),
"Not a cycle.")
ok(g.order() === 3,
test.ok(g.order() === 3,
"Has 3 vertices.");
ok(g.size() === 2,
test.ok(g.size() === 2,
"Has 2 edges.");
});
test.done();
},
test('Subgraph of K(4)', function ()
'Subgraph of K(4)': function (test)
{
var g = Graph.k(4, ['a', 'b', 'c', 'd']).subgraph(['a', 'b', 'c']);
ok(g.order() === 3,
test.ok(g.order() === 3,
"Has 3 vertices.");
ok(g.is_complete(),
test.ok(g.is_complete(),
"Is K(3).");
});
test.done();
},
test('Subgraph testing', function ()
'Subgraph testing': function (test)
{
ok(Graph.c(4).is_subgraph(Graph.k(4)),
test.ok(Graph.c(4).is_subgraph(Graph.k(4)),
"C(4) is subgraph of K(4)");
ok(!new Graph({a: ['b', 'c']}).is_subgraph(new Graph({b: ['a', 'c']})),
test.ok(!new Graph({a: ['b', 'c']}).is_subgraph(new Graph({b: ['a', 'c']})),
"Subgraph negative.");
});
test.done();
},
test('Subgraph vertices', function ()
'Subgraph vertices': function (test)
{

@@ -226,3 +237,3 @@ var a = new Date("1/1/2001");

{
ok(v === a || v === b,
test.ok(v === a || v === b,
"Vertices of graph match.");

@@ -234,8 +245,7 @@ });

{
ok(v === a || v === b,
test.ok(v === a || v === b,
"Vertices of subgraph match.");
});
});
}
if (QUnit.run) QUnit.run(typeof exports !== 'undefined' ? exports : undefined);
test.done();
},
};

Sorry, the diff of this file is not supported yet

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