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

Add Bellman-Ford algorithm #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

mstou
Copy link

@mstou mstou commented Aug 24, 2018

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

@lutzroeder
Copy link
Contributor

@dionyziz can you help reviewing and testing this change?

lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
Copy link

@dionyziz dionyziz left a 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 Show resolved Hide resolved


//Initialization
nodes.forEach( function(v) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: avoid space after (

lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Outdated Show resolved Hide resolved
lib/alg/bellman-ford.js Show resolved Hide resolved
@mstou
Copy link
Author

mstou commented Aug 25, 2018

Thanks for the feedback @dionyziz @lutzroeder . Im working on the changes

@mstou
Copy link
Author

mstou commented Aug 25, 2018

@dionyziz @lutzroeder

  • I fixed the code format in all files
  • I changed the edge traversal as requested and added the relaxAllEdges() function in bellman-ford.js
  • I also corrected the behavior of bellman-ford regarding the reversed edges ( lines 37-40 )
  • I merged the common test cases of bellman-ford and dijkstra in shortest-paths-tests.js

@dionyziz
Copy link

@pkakelas could also help with this review

@pkakelas
Copy link

LGTM, good job :)

@mstou mstou mentioned this pull request Aug 29, 2018
@mstou
Copy link
Author

mstou commented Sep 2, 2018

This pull request now fixes #87 .
Summary:

  • Implemented the bellman-ford algorithm
  • Created shortestPaths() which in Θ(E) checks whether the graph contains a negative weight edge and chooses to use dijkstra or bellman-ford accordingly
  • Added the shortest-paths-test.js file, which tests the shortestPaths function and also exports a test function for shortest paths algorithms that is used in bellman-ford and dijkstra tests to avoid code duplication

@mstou
Copy link
Author

mstou commented Sep 4, 2018

PTAL

@assafsun
Copy link

Hi @mstou

Thanks for the work!

I'm trying to review the open PR in order to reduce the amount of issues and PRs.
In case you want to continue your work, I will appreciate if you can rebase and create a new iteration.
I will review it and try to make sure it will be pushed as I think this is a nice algorithm to support.

If not, I will try to continue it.

@mstou
Copy link
Author

mstou commented Apr 10, 2020

Hi @assafsun, I'll be glad to rebase this to master and squash the commits

@assafsun
Copy link

@mstou Thanks for the update, I will follow this PR.

@mstou
Copy link
Author

mstou commented Apr 29, 2020

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;
Copy link

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) {
Copy link

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);
Copy link

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"),
Copy link

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) {
Copy link

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?

@assafsun
Copy link

assafsun commented May 1, 2020

@mstou Thank you for the new iteration. I ran your code and the tests passed.
I left some small comments.

@sajaddp
Copy link

sajaddp commented Aug 20, 2024

Personally, I really like this pr to get the final approval.
Bellman-Ford algorithm is really practical. Is there an update about this PR or is it better to provide a new code as a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Bellman–Ford
6 participants