Bellman Ford Algorithm.
Algorithm
1. Initialize distances from source to all vertices as infinity ans set source distance to 0. Create an array distance[] of size |V| with all values as infinite except distance[source].
2. Calculate shortest distances by doing the following |V| - 1 times where |V| is the number of vertices.(In this visualization we dont perform relaxation 800 times as it is time consuming considering some vertices wont change after n number of relaxations).
1. For each edge (u,v) if distance[v] > distance[u] + weight of edge (u,v), update distance[v]. (distance[v] = distance[u] + weight(u, v)).
3. If distance[v] > distance[u] + weight(u,v) then graph contains a negative weight cycle therefore terminate the algorithm and return.
Step 2 guarantees the shortest distances if the graph does not contain negative cycles, which means if the next iteration produces a shorter path for any vertex then there exists a negative weight cycle.Computational complexity
The algorithm has O(VE) time complexity in the average and best cases and O(E) time complexity in the best case. The space complexity is O(V) as we store vertices in memory.Applications
- Detecting negative cycles.
- Network packet routing protocols
- telephone networking.
- Distributed systems.
- maps and gps navigation software.
- Drone/robotic path routing.
References
bellman ford algorithm