graph-data-structure
Advanced tools
Comparing version 0.2.0 to 0.3.0
14
index.js
@@ -75,4 +75,12 @@ // A graph data structure with depth-first search and topological sort. | ||
if(!sourceNodes){ | ||
sourceNodes = nodes(); | ||
} | ||
if(typeof includeSourceNodes !== "boolean"){ | ||
includeSourceNodes = true; | ||
} | ||
var visited = {}; | ||
var nodes = []; | ||
var nodeList = []; | ||
@@ -83,3 +91,3 @@ function DFSVisit(node){ | ||
adjacent(node).forEach(DFSVisit); | ||
nodes.push(node); | ||
nodeList.push(node); | ||
} | ||
@@ -96,3 +104,3 @@ } | ||
return nodes; | ||
return nodeList; | ||
} | ||
@@ -99,0 +107,0 @@ |
{ | ||
"name": "graph-data-structure", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A graph data structure with topological sort.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# graph-data-structure [![Build Status](https://travis-ci.org/curran/graph-data-structure.svg?branch=master)](https://travis-ci.org/curran/graph-data-structure) | ||
# graph-data-structure | ||
@@ -7,2 +7,4 @@ A graph data structure with topological sort algorithm. | ||
[![Build Status](https://travis-ci.org/curran/graph-data-structure.svg?branch=master)](https://travis-ci.org/curran/graph-data-structure) | ||
# Usage | ||
@@ -9,0 +11,0 @@ |
79
test.js
@@ -84,7 +84,37 @@ | ||
// This example is from Cormen et al. "Introduction to Algorithms" page 550 | ||
it("Should compute topological sort.", function (){ | ||
var graph = Graph(); | ||
// Shoes depend on socks. | ||
// Socks need to be put on before shoes. | ||
graph.addEdge("socks", "shoes"); | ||
graph.addEdge("pants", "shoes"); | ||
graph.addEdge("underpants", "pants"); | ||
graph.addEdge("pants", "belt"); | ||
graph.addEdge("shirt", "belt"); | ||
graph.addEdge("shirt", "tie"); | ||
graph.addEdge("tie", "jacket"); | ||
graph.addEdge("belt", "jacket"); | ||
var sorted = graph.topologicalSort(); | ||
assert(comesBefore(sorted, "pants", "shoes")); | ||
assert(comesBefore(sorted, "underpants", "pants")); | ||
assert(comesBefore(sorted, "underpants", "shoes")); | ||
assert(comesBefore(sorted, "shirt", "jacket")); | ||
assert(comesBefore(sorted, "shirt", "belt")); | ||
assert(comesBefore(sorted, "belt", "jacket")); | ||
assert.equal(sorted.length, 8); | ||
}); | ||
it("Should compute topological sort, excluding source nodes.", function (){ | ||
var graph = Graph(); | ||
graph.addEdge("a", "b"); | ||
graph.addEdge("b", "c"); | ||
var sorted = graph.topologicalSort(["a"]); | ||
var sorted = graph.topologicalSort(["a"], false); | ||
assert.equal(sorted.length, 2); | ||
@@ -105,3 +135,3 @@ assert.equal(sorted[0], "b"); | ||
var sorted = graph.topologicalSort(["a"]); | ||
var sorted = graph.topologicalSort(["a"], false); | ||
assert.equal(sorted.length, 4); | ||
@@ -119,47 +149,2 @@ assert(contains(sorted, "b")); | ||
}); | ||
// This example is from Cormen et al. "Introduction to Algorithms" page 550 | ||
it("Should compute topological sort relatable case.", function (){ | ||
var graph = Graph(); | ||
// Shoes depend on socks. | ||
// Socks need to be put on before shoes. | ||
graph.addEdge("socks", "shoes"); | ||
// Shoes depend on pants. | ||
// Pants need to be put on before shoes. | ||
graph.addEdge("pants", "shoes"); | ||
// Pants depend on underpants. | ||
graph.addEdge("underpants", "pants"); | ||
// Belt depends on pants. | ||
graph.addEdge("pants", "belt"); | ||
// Belt depends on shirt. | ||
graph.addEdge("shirt", "belt"); | ||
// Tie depends on shirt. | ||
graph.addEdge("shirt", "tie"); | ||
// Jacket depends on tie. | ||
graph.addEdge("tie", "jacket"); | ||
// Jacket depends on belt. | ||
graph.addEdge("belt", "jacket"); | ||
var sorted = graph.topologicalSort(graph.nodes(), true); | ||
assert.equal(sorted.length, 8); | ||
assert(comesBefore(sorted, "pants", "shoes")); | ||
assert(comesBefore(sorted, "underpants", "pants")); | ||
assert(comesBefore(sorted, "underpants", "shoes")); | ||
assert(comesBefore(sorted, "shirt", "jacket")); | ||
assert(comesBefore(sorted, "shirt", "belt")); | ||
assert(comesBefore(sorted, "belt", "jacket")); | ||
}); | ||
}); | ||
@@ -166,0 +151,0 @@ |
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
48
11515
251