diff --git a/lib/graph.js b/lib/graph.js index d3e5bd4f..71f3818a 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -122,7 +122,9 @@ Graph.prototype.sources = function() { Graph.prototype.sinks = function() { var self = this; return _.filter(this.nodes(), function(v) { - return _.isEmpty(self._out[v]); + if(self._isDirected) + return _.isEmpty(self._out[v]); + return _.isEmpty(self._in[v]) && _.isEmpty(self._out[v]); }); }; diff --git a/test/graph-test.js b/test/graph-test.js index 8aede0be..cc3df92a 100644 --- a/test/graph-test.js +++ b/test/graph-test.js @@ -4,9 +4,13 @@ var Graph = require("..").Graph; describe("Graph", function() { var g; + var gUndirected; beforeEach(function() { g = new Graph(); + gUndirected = new Graph({ + directed: false + }); }); describe("initial state", function() { @@ -82,11 +86,17 @@ describe("Graph", function() { }); describe("sinks", function() { - it("returns nodes in the graph that have no out-edges", function() { + it("returns nodes in the graph that have no out-edges (directed graph)", function() { g.setPath(["a", "b", "c"]); g.setNode("d"); expect(_.sortBy(g.sinks())).to.eql(["c", "d"]); }); + + it("returns nodes in the graph that have no edges (undirected graph)", function() { + gUndirected.setPath(["a", "b", "c", "d"]); + gUndirected.removeNode("c"); + expect(_.sortBy(gUndirected.sinks())).to.eql(["d"]); + }); }); describe("filterNodes", function() {