Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dijkstra undirected #92

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/alg/dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ module.exports = dijkstra;
var DEFAULT_WEIGHT_FUNC = _.constant(1);

function dijkstra(g, source, weightFn, edgeFn) {
var defaultEdgeFn = function(v) {
return g.outEdges(v);
};

return runDijkstra(g, String(source),
weightFn || DEFAULT_WEIGHT_FUNC,
edgeFn || function(v) { return g.outEdges(v); });
edgeFn || defaultEdgeFn);
}

function runDijkstra(g, source, weightFn, edgeFn) {
Expand Down
42 changes: 24 additions & 18 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,9 @@ Graph.prototype.setEdge = function() {

this._edgeLabels[e] = valueSpecified ? value : this._defaultEdgeLabelFn(v, w, name);

var edgeObj = edgeArgsToObj(this._isDirected, v, w, name);
// Ensure we add undirected edges in a consistent way.
var edgeObj = edgeArgsToObj(this._isDirected, v, w, name);

v = edgeObj.v;
w = edgeObj.w;

Expand Down Expand Up @@ -460,32 +461,37 @@ Graph.prototype.removeEdge = function(v, w, name) {
};

Graph.prototype.inEdges = function(v, u) {
var inV = this._in[v];
if (inV) {
var edges = _.values(inV);
if (!u) {
return edges;
}
return _.filter(edges, function(edge) { return edge.v === u; });
if (this.isDirected()) {
return this._filterEdges(this._in[v], v, u);
}
return this.nodeEdges(v, u);
};

Graph.prototype.outEdges = function(v, w) {
var outV = this._out[v];
if (outV) {
var edges = _.values(outV);
if (!w) {
return edges;
}
return _.filter(edges, function(edge) { return edge.w === w; });
if (this.isDirected()) {
return this._filterEdges(this._out[v], v, w);
}
return this.nodeEdges(v, w);
};

Graph.prototype.nodeEdges = function(v, w) {
var inEdges = this.inEdges(v, w);
if (inEdges) {
return inEdges.concat(this.outEdges(v, w));
if (v in this._nodes) {
return this._filterEdges(_.merge(this._in[v], this._out[v]), v, w);
}
};

Graph.prototype._filterEdges = function(setV, localEdge, remoteEdge) {
if (!setV) {
return;
}
var edges = _.values(setV);
if (!remoteEdge) {
return edges;
}
return _.filter(edges, function(edge) {
return edge.v === localEdge && edge.w === remoteEdge
|| edge.v === remoteEdge && edge.w === localEdge;
});
};

function incrementOrInitEntry(map, k) {
Expand Down
14 changes: 14 additions & 0 deletions test/alg/dijkstra-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe("alg.dijkstra", function() {
});
});

it("works for undirected graphs when edges have a different natural order",
function() {
var g = new Graph({ directed: false });
g.setPath(["a", "b", "c"]);
g.setEdge("b", "d");
expect(dijkstra(g, "d")).to.eql({
a: { distance: 2, predecessor: "b" },
b: { distance: 1, predecessor: "d" },
c: { distance: 2, predecessor: "b" },
d: { distance: 0 }
});
}
);

it("uses an optionally supplied weight function", function() {
var g = new Graph();
g.setEdge("a", "b", 1);
Expand Down