forked from dagrejs/graphlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var _ = require("lodash"); | ||
|
||
module.exports = postorder; | ||
|
||
function postorder(g, root) { | ||
var visited = {}, | ||
results = []; | ||
|
||
dfs(g, root, undefined, visited, results); | ||
|
||
return results; | ||
} | ||
|
||
function dfs(g, v, prev, visited, results) { | ||
if (_.has(visited, v)) { | ||
throw new Error("The input graph is not a tree: " + g); | ||
} | ||
visited[v] = true; | ||
g.successors(v).forEach(function(w) { | ||
if (g.isDirected() || w !== prev) { | ||
dfs(g, w, v, visited, results); | ||
} | ||
}); | ||
results.push(v); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var _ = require("lodash"), | ||
expect = require("../chai").expect, | ||
Graph = require("../..").Graph, | ||
Digraph = require("../..").Digraph, | ||
postorder = require("../..").alg.postorder; | ||
|
||
describe("alg.postorder", function() { | ||
it("returns the root for a singleton graph", function() { | ||
var g = new Graph(); | ||
g.setNode("a"); | ||
expect(postorder(g, "a")).to.eql(["a"]); | ||
}); | ||
|
||
it("works for an undirected tree", function() { | ||
var g = new Graph(); | ||
g.setEdge("a", "b"); | ||
g.setPath(["a", "c", "d"]); | ||
g.setEdge("c", "e"); | ||
|
||
var result = postorder(g, "a"); | ||
expect(_.sortBy(result)).to.eql(["a", "b", "c", "d", "e"]); | ||
expect(result.indexOf("b")).to.be.lt(result.indexOf("a")); | ||
expect(result.indexOf("c")).to.be.lt(result.indexOf("a")); | ||
expect(result.indexOf("d")).to.be.lt(result.indexOf("c")); | ||
expect(result.indexOf("e")).to.be.lt(result.indexOf("c")); | ||
}); | ||
|
||
it("fails for an undirected graph that is not a tree", function() { | ||
var g = new Graph(); | ||
g.setPath(["a", "b", "c", "a"]); | ||
|
||
expect(function() { postorder(g, "a"); }).to.throw(); | ||
}); | ||
|
||
it("works for a directed tree", function() { | ||
var g = new Digraph(); | ||
g.setEdge("a", "b"); | ||
g.setPath(["a", "c", "d"]); | ||
g.setEdge("c", "e"); | ||
|
||
var result = postorder(g, "a"); | ||
expect(_.sortBy(result)).to.eql(["a", "b", "c", "d", "e"]); | ||
expect(result.indexOf("b")).to.be.lt(result.indexOf("a")); | ||
expect(result.indexOf("c")).to.be.lt(result.indexOf("a")); | ||
expect(result.indexOf("d")).to.be.lt(result.indexOf("c")); | ||
expect(result.indexOf("e")).to.be.lt(result.indexOf("c")); | ||
}); | ||
|
||
it("fails for a directed graph that is not a tree", function() { | ||
var g = new Digraph(); | ||
g.setPath(["a", "b", "a"]); | ||
expect(function() { postorder(g, "a"); }).to.throw(); | ||
}); | ||
|
||
it("fails if root is not in the graph", function() { | ||
var g = new Graph(); | ||
g.setNode("a"); | ||
expect(function() { postorder(g, "b"); }).to.throw(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters