-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a function that runs the common tests of all shortest paths al…
…gorithms.
- Loading branch information
Showing
3 changed files
with
92 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
var expect = require("../chai").expect, | ||
Graph = require("../..").Graph; | ||
|
||
module.exports = tests; | ||
|
||
function tests(algorithm) { | ||
describe( "Shortest Path Algorithms", function() { | ||
it("assigns distance 0 for the source node", function() { | ||
var g = new Graph(); | ||
g.setNode("source"); | ||
expect(algorithm(g, "source")).to.eql({ source: { distance: 0 } }); | ||
}); | ||
|
||
it("returns Number.POSITIVE_INFINITY for unconnected nodes", function() { | ||
var g = new Graph(); | ||
g.setNode("a"); | ||
g.setNode("b"); | ||
expect(algorithm(g, "a")).to.eql({ | ||
a: { distance: 0 }, | ||
b: { distance: Number.POSITIVE_INFINITY } | ||
}); | ||
}); | ||
|
||
it("returns the distance and path from the source node to other nodes", function() { | ||
var g = new Graph(); | ||
g.setPath(["a", "b", "c"]); | ||
g.setEdge("b", "d"); | ||
expect(algorithm(g, "a")).to.eql({ | ||
a: { distance: 0 }, | ||
b: { distance: 1, predecessor: "a" }, | ||
c: { distance: 2, predecessor: "b" }, | ||
d: { distance: 2, predecessor: "b" } | ||
}); | ||
}); | ||
|
||
it("works for undirected graphs", function() { | ||
var g = new Graph({ directed: false }); | ||
g.setPath(["a", "b", "c"]); | ||
g.setEdge("b", "d"); | ||
expect(algorithm(g, "a")).to.eql({ | ||
a: { distance: 0 }, | ||
b: { distance: 1, predecessor: "a" }, | ||
c: { distance: 2, predecessor: "b" }, | ||
d: { distance: 2, predecessor: "b" } | ||
}); | ||
}); | ||
|
||
it("uses an optionally supplied weight function", function() { | ||
var g = new Graph(); | ||
g.setEdge("a", "b", 1); | ||
g.setEdge("a", "c", 2); | ||
g.setEdge("b", "d", 3); | ||
g.setEdge("c", "d", 3); | ||
|
||
expect(algorithm(g, "a", weightFn(g))).to.eql({ | ||
a: { distance: 0 }, | ||
b: { distance: 1, predecessor: "a" }, | ||
c: { distance: 2, predecessor: "a" }, | ||
d: { distance: 4, predecessor: "b" } | ||
}); | ||
}); | ||
|
||
it("uses an optionally supplied edge function", function() { | ||
var g = new Graph(); | ||
g.setPath(["a", "c", "d"]); | ||
g.setEdge("b", "c"); | ||
|
||
expect(algorithm(g, "d", undefined, function(e) { return g.inEdges(e); })).to.eql({ | ||
a: { distance: 2, predecessor: "c" }, | ||
b: { distance: 2, predecessor: "c" }, | ||
c: { distance: 1, predecessor: "d" }, | ||
d: { distance: 0 } | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
function weightFn(g) { | ||
return function(e) { | ||
return g.edge(e); | ||
}; | ||
} |