Skip to content

Commit 2caa474

Browse files
authored
Merge pull request #19 from datavis-tech/return-shortest-path-weight
Shortest Path Weight
2 parents bde16c0 + 9c165fd commit 2caa474

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,19 @@ module.exports = function Graph(serialized){
276276
// predecessor subgraph from destination to source.
277277
function path(){
278278
var nodeList = [];
279+
var weight = 0;
279280
var node = destination;
280281
while(p[node]){
281282
nodeList.push(node);
283+
weight += getEdgeWeight(p[node], node);
282284
node = p[node];
283285
}
284286
if (node !== source) {
285287
throw new Error("No path found");
286288
}
287289
nodeList.push(node);
288290
nodeList.reverse();
291+
nodeList.weight = weight;
289292
return nodeList;
290293
}
291294

test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ function output(graph, name){
1717
outputGraph(graph.serialize(), name);
1818
}
1919

20+
function withWeight(nodeList, weight){
21+
nodeList.weight = weight;
22+
return nodeList;
23+
}
24+
2025
describe("Graph", function() {
2126

2227
describe("Data structure", function() {
@@ -337,14 +342,14 @@ describe("Graph", function() {
337342

338343
it("Should compute shortest path on a single edge.", function (){
339344
var graph = Graph().addEdge("a", "b");
340-
assert.deepEqual(graph.shortestPath("a", "b"), ["a", "b"]);
345+
assert.deepEqual(graph.shortestPath("a", "b"), withWeight(["a", "b"], 1));
341346
});
342347

343348
it("Should compute shortest path on two edges.", function (){
344349
var graph = Graph()
345350
.addEdge("a", "b")
346351
.addEdge("b", "c");
347-
assert.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
352+
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
348353
});
349354

350355
it("Should compute shortest path on example from Cormen text (p. 659).", function (){
@@ -358,8 +363,9 @@ describe("Graph", function() {
358363
.addEdge("y", "z", 2)
359364
.addEdge("x", "z", 4)
360365
.addEdge("z", "x", 6);
361-
assert.deepEqual(graph.shortestPath("s", "z"), ["s", "y", "z"]);
362-
assert.deepEqual(graph.shortestPath("s", "x"), ["s", "y", "t", "x"]);
366+
367+
assert.deepEqual(graph.shortestPath("s", "z"), withWeight(["s", "y", "z"], 5 + 2));
368+
assert.deepEqual(graph.shortestPath("s", "x"), withWeight(["s", "y", "t", "x"], 5 + 3 + 1));
363369
});
364370

365371
it("Should throw error if source node not in graph.", function (){
@@ -384,7 +390,7 @@ describe("Graph", function() {
384390
.addEdge("a", "b")
385391
.addEdge("b", "c")
386392
.addEdge("d", "e");
387-
assert.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
393+
assert.deepEqual(graph.shortestPath("a", "c"), withWeight(["a", "b", "c"], 2));
388394
});
389395
});
390396
});

0 commit comments

Comments
 (0)