Skip to content

Commit

Permalink
Implement the shortestPaths function that chooses whether to use Dijk…
Browse files Browse the repository at this point in the history
…stra or Bellmanford.
  • Loading branch information
mstou committed Apr 29, 2020
1 parent fcce9e6 commit f4b8e9c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/alg/bellman-ford.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function bellmanFord(g, source, weightFn, edgeFn) {

function runBellmanFord(g, source, weightFn, edgeFn) {
var results = {},
didADistanceUpgrade = true,
iterations = 0,
nodes = g.nodes();
didADistanceUpgrade = true,
iterations = 0,
nodes = g.nodes();

var relaxEdge = function(edge) {
var edgeWeight = weightFn(edge);
Expand Down
1 change: 1 addition & 0 deletions lib/alg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
postorder: require("./postorder"),
preorder: require("./preorder"),
prim: require("./prim"),
shortestPaths: require("./shortest-paths"),
tarjan: require("./tarjan"),
topsort: require("./topsort")
};
42 changes: 42 additions & 0 deletions lib/alg/shortest-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var dijkstra = require("./dijkstra"),
bellmanFord = require("./bellman-ford");

module.exports = shortestPaths;

function shortestPaths(g, source, weightFn, edgeFn){
return runShortestPaths(
g,
source,
weightFn,
edgeFn || function(v) { return g.outEdges(v); }
);
}

function runShortestPaths(g, source, weightFn, edgeFn) {
if (weightFn === undefined) {
return dijkstra(g, source, weightFn, edgeFn);
}

var negativeEdgeExists = false;
var nodes = g.nodes();

for (var i = 0; i < nodes.length; i++) {
var adjList = edgeFn(nodes[i]);

for (var j = 0; j < adjList.length; j++) {
var edge = adjList[j];
var inVertex = edge.v === nodes[i] ? edge.v : edge.w;
var outVertex = inVertex === edge.v ? edge.w : edge.v;

if (weightFn({ v: inVertex, w: outVertex }) < 0) {
negativeEdgeExists = true;
}
}

if (negativeEdgeExists) {
return bellmanFord(g, source, weightFn, edgeFn);
}
}

return dijkstra(g, source, weightFn, edgeFn);
}
6 changes: 3 additions & 3 deletions test/alg/bellman-ford-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var expect = require("../chai").expect,
Graph = require("../..").Graph,
bellmanFord = require("../..").alg.bellmanFord,
shortestPathsTests = require("./shortest-paths-tests.js");
Graph = require("../..").Graph,
bellmanFord = require("../..").alg.bellmanFord,
shortestPathsTests = require("./shortest-paths-tests.js");

describe("alg.bellmanFord", function(){
shortestPathsTests(bellmanFord);
Expand Down
6 changes: 3 additions & 3 deletions test/alg/dijkstra-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var expect = require("../chai").expect,
Graph = require("../..").Graph,
dijkstra = require("../..").alg.dijkstra,
shortestPathsTests = require("./shortest-paths-tests.js");
Graph = require("../..").Graph,
dijkstra = require("../..").alg.dijkstra,
shortestPathsTests = require("./shortest-paths-tests.js");

describe("alg.dijkstra", function() {
shortestPathsTests(dijkstra);
Expand Down
26 changes: 25 additions & 1 deletion test/alg/shortest-paths-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var expect = require("../chai").expect,
Graph = require("../..").Graph;
Graph = require("../..").Graph,
alg = require("../..").alg;

module.exports = tests;

Expand Down Expand Up @@ -75,6 +76,29 @@ function tests(algorithm) {
});
}

// Test shortestPaths() function
describe("alg.shortestPaths", function() {
tests(alg.shortestPaths);

it("uses dijkstra if no weightFn is provided", function() {
var g = new Graph();
g.setPath(["a", "b", "c"]);
g.setEdge("b", "d", -10);

expect(alg.shortestPaths(g, "a")).to.eql(alg.dijkstra(g, "a"));
});

it("uses bellman-ford if the graph contains a negative edge", function() {
var g = new Graph();
g.setEdge("a", "b", 10);
g.setEdge("b", "c", 8);
g.setEdge("a", "d", -3);
g.setEdge("d", "c", 2);

expect(alg.shortestPaths(g, "a", weightFn(g))).to.eql(alg.bellmanFord(g, "a", weightFn(g)));
});
});

function weightFn(g) {
return function(e) {
return g.edge(e);
Expand Down

0 comments on commit f4b8e9c

Please sign in to comment.