ADA Unit 5- Bellman Ford Algorithm
ADA Unit 5- Bellman Ford Algorithm
Bellman Ford Algorithm: is a classic algorithm used for finding the shortest path
from a single source vertex to all other vertices in a weighted graph. It works even
when some of the edges have negative weights, unlike Dijkstra’s algorithm,
which assumes non-negative edge weights.
Example: Imagine you have a map with different cities connected by roads, each
road having a certain distance. The Bellman–Ford algorithm is like a guide that
helps you find the shortest path from one city to all other cities, even if some
roads have negative lengths.
Problem Statement:
Given a weighted graph with both positive and negative edge weights, the goal
of the Bellman-Ford algorithm is to compute the shortest paths from a source
vertex s to all other vertices in the graph. It can also detect negative weight cycles,
which are cycles in the graph that can reduce the total path weight indefinitely.
Working of Bellman-Ford Algorithm
Question: For a graph which is given below and find whether there exists a
negative cycle or not using Bellman-Ford.
Solution:
Step 1: Initialize a distance array Dist[] to store the shortest distance for each
vertex from the source vertex. Initially distance of source will be 0 and Distance
of other vertices will be INFINITY.
Since the graph has six vertices so it will have five iterations.
First iteration
Consider the edge (A, B). Denote vertex 'A' as 'u' and vertex 'B' as 'v'. Now use
the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 6
Since (0 + 6) is less than ∞, so update
d(v) = d(u) + c(u , v)
d(v) = 0 + 6 = 6
Therefore, the distance of vertex B is 6.
Consider the edge (A, C). Denote vertex 'A' as 'u' and vertex 'C' as 'v'. Now use
the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 4
Since (0 + 4) is less than ∞, so update
d(v) = d(u) + c(u , v)
d(v) = 0 + 4 = 4
Consider the edge (A, D). Denote vertex 'A' as 'u' and vertex 'D' as 'v'. Now use
the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 5
Consider the edge (B, E). Denote vertex 'B' as 'u' and vertex 'E' as 'v'. Now use
the relaxing formula:
d(u) = 6
d(v) = ∞
c(u , v) = -1
Since (6 - 1) is less than ∞, so update
d(v) = d(u) + c(u , v)
d(v) = 6 - 1= 5
Therefore, the distance of vertex E is 5.
Consider the edge (C, E). Denote vertex 'C' as 'u' and vertex 'E' as 'v'. Now use
the relaxing formula:
d(u) = 4
d(v) = 5
c(u , v) = 3
Since (4 + 3) is greater than 5, so there will be no updation. The value at vertex
E is 5.
Consider the edge (D, C). Denote vertex 'D' as 'u' and vertex 'C' as 'v'. Now use
the relaxing formula:
d(u) = 5
d(v) = 4
c(u , v) = -2
Since (5 -2) is less than 4, so update
d(v) = d(u) + c(u , v)
d(v) = 5 - 2 = 3
Therefore, the distance of vertex C is 3.
Consider the edge (D, F). Denote vertex 'D' as 'u' and vertex 'F' as 'v'. Now use
the relaxing formula:
d(u) = 5
d(v) = ∞
c(u , v) = -1
Since (5 -1) is less than ∞, so update
d(v) = d(u) + c(u , v)
d(v) = 5 - 1 = 4
Therefore, the distance of vertex F is 4.
Consider the edge (E, F). Denote vertex 'E' as 'u' and vertex 'F' as 'v'. Now use the
relaxing formula:
d(u) = 5
d(v) = ∞
c(u , v) = 3
Since (5 + 3) is greater than 4, so there would be no updation on the distance
value of vertex F.
Consider the edge (C, B). Denote vertex 'C' as 'u' and vertex 'B' as 'v'. Now use
the relaxing formula:
d(u) = 3
d(v) = 6
c(u , v) = -2
Since (3 - 2) is less than 6, so update
d(v) = d(u) + c(u , v)
d(v) = 3 - 2 = 1
Therefore, the distance of vertex B is 1.
Now the first iteration is completed. We move to the second iteration.
Second iteration:
In the second iteration, we again check all the edges. The first edge is (A, B).
Since (0 + 6) is greater than 1 so there would be no updation in the vertex B.
The next edge is (A, C). Since (0 + 4) is greater than 3 so there would be no
updation in the vertex C.
The next edge is (A, D). Since (0 + 5) equals to 5 so there would be no updation
in the vertex D.
The next edge is (B, E). Since (1 - 1) equals to 0 which is less than 5 so update:
d(v) = d(u) + c(u, v)
d(E) = d(B) +c(B , E)
=1-1=0
The next edge is (C, E). Since (3 + 3) equals to 6 which is greater than 5 so there
would be no updation in the vertex E.
The next edge is (D, C). Since (5 - 2) equals to 3 so there would be no updation
in the vertex C.
The next edge is (D, F). Since (5 - 1) equals to 4 so there would be no updation
in the vertex F.
The next edge is (E, F). Since (5 + 3) equals to 8 which is greater than 4 so there
would be no updation in the vertex F.
The next edge is (C, B). Since (3 - 2) equals to 1` so there would be no updation
in the vertex B.
Third iteration
We will perform the same steps as we did in the previous iterations. We will
observe that there will be no updation in the distance of vertices.
The following are the distances of vertices:
1. A: 0
2. B: 1
3. C: 3
4. D: 5
5. E: 0
6. F: 3
Question: Explain with the help of an example that the bellman ford
algorithm does not produce a correct answer if the sum of the edges of a cycle
is negative.
Solution:
In the above graph, we consider vertex 1 as the source vertex and provides 0 value
to it. We provide infinity value to other vertices shown as below: