Search for a command to run...
Relax directed edges even when weights can be negative
Graph playground
Edit directed weighted edges and replay relaxation passes.
Initialize distances
Set node 0 to distance 0 and every other node to infinity.
Bellman-Ford Algorithm
1function bellmanFord(edges, start, nodeCount) {2 const distance = initializeDistances(start);3 //start: 0target: 5pass: 04 for (let pass = 1; pass < nodeCount; pass++) {5 for (const edge of edges) {6 relax(edge, distance);7 }8 }9 return distance;10}