Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
};

Expand Down
12 changes: 11 additions & 1 deletion test/graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down