Bellman Ford Calculator

Compute shortest paths across weighted graphs with detailed iteration tracking. Detect negative cycles with certainty. Review routes, relaxations, and final distances with confidence today.

Calculator Input

Optional. Separate labels with commas or spaces.
This is the starting point for all shortest paths.
Optional. Shows a focused route summary.
Undirected mode automatically adds reverse edges.
Useful for study, debugging, and verification.
The example uses a classic weighted graph dataset.
Accepted formats: A,B,6 or A B 6. Enter one edge on each line with a numeric weight.
Reset Page

Example Data Table

From To Weight Expected Shortest Distance from A
AB6B = 2
AD7D = 7
BC5C = 4
BD8A = 0
BE-4E = -2
CB-2Graph Type = Directed
DC-3Source = A
DE9No reachable negative cycle

This sample demonstrates negative weights without producing a reachable negative cycle.

Formula Used

The Bellman–Ford method relaxes every edge repeatedly. Each relaxation checks whether traveling through one vertex improves the best known distance to another vertex.

Relaxation rule: d(v) = min(d(v), d(u) + w(u,v)) Where: d(v) = current best distance to vertex v d(u) = current best distance to vertex u w(u,v) = edge weight from u to v Process: 1. Set source distance to 0. 2. Set all other distances to infinity. 3. Relax every edge |V| - 1 times. 4. Run one more pass to detect any reachable negative cycle.

Path reconstruction uses the predecessor array. Each vertex stores the previous vertex that produced its current best distance.

How to Use This Calculator

  1. Enter the vertex labels, or leave them blank to infer from edges.
  2. Choose a source vertex and optionally a target vertex.
  3. Select directed or undirected graph mode.
  4. Enter one weighted edge per line in the edge list box.
  5. Enable the detailed log if you want pass-by-pass relaxation details.
  6. Press Submit to calculate distances, routes, and cycle checks.
  7. Use the CSV or PDF buttons to export the results.

Frequently Asked Questions

1. What does this calculator compute?

It computes shortest-path distances from one source vertex to all other vertices. It also reconstructs routes, records passes, and checks for reachable negative cycles.

2. Can Bellman–Ford handle negative edge weights?

Yes. That is one of its main advantages. It works with negative weights, provided there is no reachable negative cycle affecting the requested shortest paths.

3. What happens if a negative cycle exists?

The calculator flags the cycle and marks affected vertices as risky. For those vertices, a true shortest path is undefined because repeated cycling can keep reducing total cost.

4. Why is the path shown as unreachable?

That means no valid route from the source reaches that vertex using the provided edges. Its distance remains infinity, and no predecessor chain can be formed.

5. When should I use directed mode?

Use directed mode when edge direction matters, such as one-way streets or state transitions. Use undirected mode when travel in both directions has equal weight.

6. Why are there several passes in the output?

Each pass relaxes all edges once. The algorithm needs up to |V|−1 passes because the longest simple shortest path can contain at most that many edges.

7. Is this suitable for study and assignments?

Yes. The detailed log and pass snapshots make it useful for learning relaxation mechanics, verifying manual solutions, and documenting intermediate algorithm states.

8. What format should I use for the edge list?

Enter one edge per line using from,to,weight or from to weight. For example: A,B,6 or A B 6. Weights may be positive, zero, or negative.

Related Calculators

adjacency matrix generatortopological sort calculatorminimum spanning tree calculatorgraph distance calculatorbreadth first search calculatorford fulkerson calculatorvertex degree calculatorconnected components counterfloyd warshall calculatoreulerian path calculator

Important Note: All the Calculators listed in this site are for educational purpose only and we do not guarentee the accuracy of results. Please do consult with other sources as well.