-
Notifications
You must be signed in to change notification settings - Fork 234
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
Add Bellman-Ford algorithm #88
base: master
Are you sure you want to change the base?
Conversation
@dionyziz can you help reviewing and testing this change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
lib/alg/bellman-ford.js
Outdated
|
||
|
||
//Initialization | ||
nodes.forEach( function(v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: avoid space after (
Thanks for the feedback @dionyziz @lutzroeder . Im working on the changes |
|
@pkakelas could also help with this review |
LGTM, good job :) |
This pull request now fixes #87 .
|
PTAL |
64a1b87
to
cf48a25
Compare
Hi @mstou Thanks for the work! I'm trying to review the open PR in order to reduce the amount of issues and PRs. If not, I will try to continue it. |
Hi @assafsun, I'll be glad to rebase this to master and squash the commits |
@mstou Thanks for the update, I will follow this PR. |
Hi @assafsun, sorry for the delay, I rebased to master and squashed the fixup commits. Take a look whenever you have time :) |
var outVertex = inVertex === edge.v ? edge.w : edge.v; | ||
|
||
if (weightFn({ v: inVertex, w: outVertex }) < 0) { | ||
negativeEdgeExists = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can break in case a negative edge was found
} | ||
} | ||
|
||
if (negativeEdgeExists) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can extract this if outside the scope and just write:
return negativeEdgeExists? bellmanFord(g, source, weightFn, edgeFn): dijkstra(g, source, weightFn, edgeFn)
nodes = g.nodes(); | ||
|
||
var relaxEdge = function(edge) { | ||
var edgeWeight = weightFn(edge); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we switch some of the 'var' declarations in this PR to let?
@@ -8,6 +9,7 @@ module.exports = { | |||
postorder: require("./postorder"), | |||
preorder: require("./preorder"), | |||
prim: require("./prim"), | |||
shortestPaths: require("./shortest-paths"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good that you extract it but the documentation will need to mention that this API can run bellman ford or Dijkstra
|
||
module.exports = tests; | ||
|
||
function tests(algorithm) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe adding a comment that this reflect for bellman ford or Dijkstra?
@mstou Thank you for the new iteration. I ran your code and the tests passed. |
Personally, I really like this pr to get the final approval. |
Fixes #87 .
As requested in issue #87, I implemented the bellman-ford algorithm, respecting the coding style of the repository. I included the bellmanFord function in the module exported by lib/alg and added some tests in tests/alg/bellman-ford-tests.js