Skip to content
Merged
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
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,19 @@ module.exports = function Graph(serialized){
// predecessor subgraph from destination to source.
function path(){
var nodeList = [];
var weight = 0;
var node = destination;
while(p[node]){
nodeList.push(node);
weight += getEdgeWeight(p[node], node);
node = p[node];
}
if (node !== source) {
throw new Error("No path found");
}
nodeList.push(node);
nodeList.reverse();
nodeList.weight = weight;
return nodeList;
}

Expand Down
16 changes: 11 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function output(graph, name){
outputGraph(graph.serialize(), name);
}

function withWeight(nodeList, weight){
nodeList.weight = weight;
return nodeList;
}

describe("Graph", function() {

describe("Data structure", function() {
Expand Down Expand Up @@ -337,14 +342,14 @@ describe("Graph", function() {

it("Should compute shortest path on a single edge.", function (){
var graph = Graph().addEdge("a", "b");
assert.deepEqual(graph.shortestPath("a", "b"), ["a", "b"]);
assert.deepEqual(graph.shortestPath("a", "b"), withWeight(["a", "b"], 1));
});

it("Should compute shortest path on two edges.", function (){
var graph = Graph()
.addEdge("a", "b")
.addEdge("b", "c");
assert.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
});

it("Should compute shortest path on example from Cormen text (p. 659).", function (){
Expand All @@ -358,8 +363,9 @@ describe("Graph", function() {
.addEdge("y", "z", 2)
.addEdge("x", "z", 4)
.addEdge("z", "x", 6);
assert.deepEqual(graph.shortestPath("s", "z"), ["s", "y", "z"]);
assert.deepEqual(graph.shortestPath("s", "x"), ["s", "y", "t", "x"]);

assert.deepEqual(graph.shortestPath("s", "z"), withWeight(["s", "y", "z"], 5 + 2));
assert.deepEqual(graph.shortestPath("s", "x"), withWeight(["s", "y", "t", "x"], 5 + 3 + 1));
});

it("Should throw error if source node not in graph.", function (){
Expand All @@ -384,7 +390,7 @@ describe("Graph", function() {
.addEdge("a", "b")
.addEdge("b", "c")
.addEdge("d", "e");
assert.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
});
});
});
Expand Down