Bellman-Ford

In one example

by: Juan Rodriguez

Definition

The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph[1]

It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.

Worst-case performance
Best-case performance
Worst-case space complexity
{\displaystyle \Theta (|V||E|)}{\displaystyle \Theta (|V||E|)}
{\displaystyle \Theta (|E|)}{\displaystyle \Theta (|E|)}
{\displaystyle \Theta (|V|)}{\displaystyle \Theta (|V|)}

Example

  1. Set 0 to source node
  2. At each iteration we need to examine all the edges
  3. Update distances in the table based on the shortest path from the source node
  4. Repeat steps if any distance is updated
0 1 2 8 Inf Inf
A B C D E F

1st Iteration

6 vertices =  max 5 iterations

0 1 2 7 4 10
A B C D E F

2st Iteration

Example

0 1 2 7 4 8
A B C D E F

3st Iteration

6 vertices =  max 5 iterations

0 1 2 7 4 8
A B C D E F

4st Iteration

Example

No changes, finished!

6 vertices =  max 5 iterations

References

[1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm

[2] https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/

Bellman-Ford

By Juan Rodríguez