-
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.
Implement the shortestPaths function that chooses whether to use Dijk…
…stra or Bellmanford.
- Loading branch information
Showing
6 changed files
with
77 additions
and
10 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,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); | ||
} |
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