Bellman_Ford_Algorithm
Bellman_Ford_Algorithm
1. Purpose: The Bellman-Ford algorithm is used to find the shortest paths from a single source
vertex to all other vertices in a weighted graph, even if the graph contains negative weight edges.
2. Working Principle: It relaxes each edge of the graph up to |V|-1 times, where |V| is the number of
vertices. Relaxation involves updating the shortest path estimate for an edge if a shorter path is
found.
3. Negative Weight Handling: The algorithm detects negative weight cycles by checking for further
relaxation in the |V|-th iteration. If relaxation is possible, the graph contains a negative weight cycle.
4. Complexity: Its time complexity is O(VE), where V is the number of vertices and E is the number
of edges, making it suitable for dense graphs or graphs with negative weights.
1. Handles Negative Weights: Unlike Dijkstra's algorithm, Bellman-Ford works with graphs that have
2. Cycle Detection: It can detect negative weight cycles in a graph, which is crucial for certain
applications.
1. Less Efficient: It is slower compared to other shortest path algorithms like Dijkstra, especially for
2. Not Suitable for All Applications: The algorithm's high computational complexity makes it
Example:
Graph Details:
- Vertices: A, B, C, D
- Edges with Weights:
A -> B (1), B -> C (3), A -> C (4), C -> D (2), D -> B (-2)
Steps:
Output: