Skip to content
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
25 changes: 25 additions & 0 deletions src/algorithms/graph/bellman-ford/__test__/bellmanFord.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import Graph from '../../../../data-structures/graph/Graph';
import bellmanFord from '../bellmanFord';

describe('bellmanFord', () => {
it('should stop relaxing a complete unit-weight graph after distances settle', () => {
const graph = new Graph(true);
const vertices = Array.from({ length: 12 }, (_, index) => new GraphVertex(`v${index}`));
let weightReads = 0;
vertices.forEach((start) => {
vertices.forEach((end) => {
if (start !== end) {
const edge = new GraphEdge(start, end, 1);
Object.defineProperty(edge, 'weight', {
get: () => {
weightReads += 1;
return 1;
},
});
graph.addEdge(edge);
}
});
});

const { distances } = bellmanFord(graph, vertices[0]);
expect(distances.v0).toBe(0);
vertices.slice(1).forEach((vertex) => expect(distances[vertex.getKey()]).toBe(1));
expect(weightReads).toBeLessThanOrEqual(2 * graph.getAllEdges().length);
});

it('should find minimum paths to all vertices for undirected graph', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
Expand Down
16 changes: 12 additions & 4 deletions src/algorithms/graph/bellman-ford/bellmanFord.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ export default function bellmanFord(graph, startVertex) {
}
});

// We need (|V| - 1) iterations.
for (let iteration = 0; iteration < (graph.getAllVertices().length - 1); iteration += 1) {
// We need at most (|V| - 1) iterations.
const vertexCount = graph.getAllVertices().length;
for (let iteration = 0; iteration < (vertexCount - 1); iteration += 1) {
let relaxed = false;
// During each iteration go through all vertices.
Object.keys(distances).forEach((vertexKey) => {
const vertex = graph.getVertexByKey(vertexKey);

// Go through all vertex edges.
graph.getNeighbors(vertex).forEach((neighbor) => {
const edge = graph.findEdge(vertex, neighbor);
vertex.getEdges().forEach((edge) => {
// In an undirected graph, either endpoint may be the current vertex.
const neighbor = edge.startVertex === vertex ? edge.endVertex : edge.startVertex;
// Find out if the distance to the neighbor is shorter in this iteration
// then in previous one.
const distanceToVertex = distances[vertex.getKey()];
const distanceToNeighbor = distanceToVertex + edge.weight;
if (distanceToNeighbor < distances[neighbor.getKey()]) {
distances[neighbor.getKey()] = distanceToNeighbor;
previousVertices[neighbor.getKey()] = vertex;
relaxed = true;
}
});
});

if (!relaxed) {
break;
}
}

return {
Expand Down