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

Extract path #94

Open
wants to merge 3 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
27 changes: 27 additions & 0 deletions lib/alg/extract-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = extractPath;

function extractPath(shortestPaths, source, destination) {
if (shortestPaths[source].predecessor !== undefined) {
throw new Error("Invalid source vertex");
}
if (shortestPaths[destination].predecessor === undefined && destination !== source) {
throw new Error("Invalid destination vertex");
}

return {
weight: shortestPaths[destination].distance,
path: runExtractPath(shortestPaths, source, destination)
};
}

function runExtractPath(shortestPaths, source, destination) {
var path = [];
var currentNode = destination;

while(currentNode !== source) {
path.push(currentNode);
currentNode = shortestPaths[currentNode].predecessor;
}
path.push(source);
return path.reverse();
}
1 change: 1 addition & 0 deletions lib/alg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
components: require("./components"),
dijkstra: require("./dijkstra"),
dijkstraAll: require("./dijkstra-all"),
extractPath: require("./extract-path"),
findCycles: require("./find-cycles"),
floydWarshall: require("./floyd-warshall"),
isAcyclic: require("./is-acyclic"),
Expand Down
49 changes: 49 additions & 0 deletions test/alg/extract-path-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var expect = require("../chai").expect,
extractPath = require("../..").alg.extractPath;

describe("alg.extractPath", function() {
it("returns weight: 0 and path: [source], from source to source", function() {
var shortestPaths = {
"a": { distance: 0 },
"b": { distance: 73, predecessor: "a" }
};
expect(extractPath(shortestPaths, "a", "a")).to.eql(
{ weight: 0,
path: ["a"]
});
});

it("returns weight and path from source to destination", function() {
var shortestPaths = {
"a": { distance: 0 },
"b": { distance: 25, predecessor: "a" },
"c": { distance: 55, predecessor: "b" },
"d": { distance: 44, predecessor: "b" },
"e": { distance: 73, predecessor: "c" },
"f": { distance: 65, predecessor: "d" },
"g": { distance: 67, predecessor: "b" },
};
expect(extractPath(shortestPaths, "a", "e")).to.eql(
{ weight: 73,
path: ["a", "b", "c", "e"]
});
});

it("throws an error when provided with an invalid source vertex", function() {
var shortestPaths = {
"a": { distance: 0 },
"b": { distance: 17, predecessor: "c" },
"c": { distance: 42, predecessor: "a" }
};
expect(function() { extractPath(shortestPaths, "b", "c"); }).to.throw();
});

it("throws an error when given an invalid destination vertex", function() {
var shortestPaths = {
"a": { distance: 0 },
"b": { distance: 99, predecessor: "a" },
"c": { distance: 100 }
};
expect(function() { extractPath(shortestPaths, "a", "c"); }).to.throw();
});
});