Graph Theory Intermediate

Bellman-Ford Algorithm Visualizer

Step through edge relaxation in a directed weighted graph — handles negative weights and detects negative cycles. Run all at once or advance one edge at a time.

Handles negative edge weights
Detects negative cycles
Live
Graph Edges
FromToWeight
400ms
Node Checking Relaxed Neg Cycle
Step Log
Log appears as you step...
Current Step: READY
Bellman-Ford finds the shortest path from a source node to every other node — even when some edges have negative weights (which Dijkstra cannot handle).

Press Run All to animate, or Step to go one edge at a time.
The algorithm relaxes every edge up to |V|−1 times. "Relaxing" means: if we can reach B faster by going through A first, update B's distance. A final extra pass checks for negative cycles.
Graph Visualization
Distance Table
Bellman-Ford Algorithm
if dist[u] + w(u,v) < dist[v]: dist[v] = dist[u] + w(u,v)

Bellman-Ford solves the single-source shortest path problem on directed graphs with negative-weight edges — where Dijkstra fails. The key idea: relax every edge |V|−1 times.

After |V|−1 iterations, an extra pass that still finds an improvement signals a negative cycle — a cycle whose total weight is negative, making "shortest path" undefined.

Time complexity is O(VE) vs Dijkstra's O((V+E) log V). Use Bellman-Ford whenever negative edges exist or you need cycle detection. Otherwise prefer Dijkstra.
When To Use Bellman-Ford
  • Graphs with negative edge weights
  • Detecting negative cycles
  • Currency arbitrage detection
  • Routing protocols (distance-vector, e.g. RIP)
  • When Dijkstra's non-negativity assumption fails

Seeing the steps but need to understand the proof?

One-on-one tutoring in graph algorithms, dynamic programming, and discrete math — from Bellman-Ford through network flows.

Book a Free Consultation →