Codacoda
Back to Academy

graphs

Bellman-Ford

Bellman-Ford computes shortest paths from a single source vertex to all other vertices, even when edge weights are negative. It relaxes all edges V-1 times and can detect negative-weight cycles. It is preferred over Dijkstra's when the graph may contain negative edge weights.

Use Cases

  • Graphs with negative edge weights
  • Detecting negative-weight cycles in financial arbitrage
  • Distance-vector routing protocols (RIP)

Complexity Analysis

MetricBestAverageWorst
TimeO(V * E)O(V * E)O(V * E)
SpaceO(V)

Visualization

4251810263ABCDEF
Speed:1x
Graph readyStep 1 / 12

Implementation

Output

Click "Run Code" to see output...