Explanation
- **Bellman-Ford Algorithm** is a dynamic programming algorithm used for finding the shortest path in graphs, even with negative weight edges. It also detects negative weight cycles.
-
-
Steps:
- Initialize distances to all vertices as infinity, and set the distance to the source vertex as 0.
- Relax all edges
V-1times (whereVis the number of vertices). - Check for negative weight cycles by attempting to relax all edges once more.
-
Time Complexity:
- O(V * E), where
Vis the number of vertices andEis the number of edges.
- O(V * E), where