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

Implemented Bellman-Ford algorithm #48

Open
wants to merge 1 commit 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
48 changes: 48 additions & 0 deletions src/algorithms/graph/bellman-ford.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

const getAllEdges = (G) => {
const edges = [];
G.vertices.forEach((vertex) => {
const neighbours = G.getNeighbours(vertex);
if (neighbours && neighbours.length) {
neighbours.forEach((n) => {
edges.push({
from: vertex,
to: n,
weight: G.getEdgeWeight(vertex, n)
});
});
}
});
return edges;
};

/**
* Calculates GCD of two numbers
* @param {Graph} G weighted digraph (with positive or negative weights on edges)
* withouth negative cycles (vertices should have names starting from 0, 1, 2... n)
* @return {Array[Array]} shortest paths between all vertices
*
* References: https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
*/
const bellmanford = (G, source) => {
const graphSize = G.size;

const distance = new Array(graphSize);
for (let i = 0; i < graphSize; i += 1) {
distance[i] = Infinity;
}
distance[source] = 0;

const edges = getAllEdges(G);
for (let i = 0; i < graphSize - 1; i += 1) {
edges.forEach(({ from, to, weight }) => {
if (distance[from] + weight < distance[to]) {
distance[to] = distance[from] + weight;
}
});
}
return distance;
};


module.exports = bellmanford;
5 changes: 5 additions & 0 deletions src/algorithms/graph/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const bellmanford = require('./bellman-ford');

module.exports = {
bellmanford
};
2 changes: 2 additions & 0 deletions src/algorithms/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const geometry = require('./geometry');
const graph = require('./graph');
const math = require('./math');
const string = require('./string');
const search = require('./search');
const sort = require('./sort');

module.exports = {
graph,
geometry,
math,
string,
Expand Down
30 changes: 30 additions & 0 deletions test/algorithms/graph/testBellmanFord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-env mocha */
const bellmanford = require('../../../src').algorithms.graph.bellmanford;
const Graph = require('../../../src').datastructures.Graph;
const assert = require('assert');

describe('Bellman-Ford', () => {
it('should return correct shortest path for directed graph', () => {
const inst = new Graph(true);
inst.addVertex(0);
inst.addVertex(1);
inst.addVertex(2);
inst.addVertex(3);
inst.addVertex(4);
inst.addVertex(5);


inst.addEdge(0, 1, 10);
inst.addEdge(0, 5, 8);
inst.addEdge(1, 3, 2);
inst.addEdge(2, 1, 1);
inst.addEdge(3, 2, -2);

inst.addEdge(4, 3, -1);
inst.addEdge(4, 1, -4);
inst.addEdge(5, 4, 1);

const result = bellmanford(inst, 0);
assert.deepStrictEqual(result, [0, 5, 5, 7, 9, 8]);
});
});