Skip to content

Commit

Permalink
Dfs should use successors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Shepheard committed Nov 19, 2015
1 parent bbc3346 commit ffcc1a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/alg/dfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function doDfs(g, v, postorder, visited, acc) {
visited[v] = true;

if (!postorder) { acc.push(v); }
_.each(g.neighbors(v), function(w) {
_.each(g.successors(v), function(w) {
doDfs(g, w, postorder, visited, acc);
});
if (postorder) { acc.push(v); }
Expand Down
13 changes: 13 additions & 0 deletions test/alg/postorder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ describe("alg.postorder", function() {
expect(nodes.indexOf("d")).to.be.lt(nodes.indexOf("c"));
});

it("works for multiple connected roots", function() {
var g = new Graph();
g.setEdge("a", "b");
g.setEdge("a", "c");
g.setEdge("d", "c");

var nodes = postorder(g, ["a", "d"]);
expect(_.sortBy(nodes)).to.eql(["a", "b", "c", "d"]);
expect(nodes.indexOf("b")).to.be.lt(nodes.indexOf("a"));
expect(nodes.indexOf("c")).to.be.lt(nodes.indexOf("a"));
expect(nodes.indexOf("c")).to.be.lt(nodes.indexOf("d"));
});

it("fails if root is not in the graph", function() {
var g = new Graph();
g.setNode("a");
Expand Down

0 comments on commit ffcc1a1

Please sign in to comment.