0% found this document useful (0 votes)
24 views

ADA Unit 5- Bellman Ford Algorithm

Uploaded by

vani.cs4014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

ADA Unit 5- Bellman Ford Algorithm

Uploaded by

vani.cs4014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit 5

Bellman Ford Algorithm,


Union Find Data Structures -
Application Kruskal’s 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.

Step 2: Start relaxing the edges, during 1st Relaxation:


• Current Distance of B > (Distance of A) + (Weight of an edge from A to B)
i.e. Infinity > 0 + 5
o Therefore, Dist[B] = 5

Step 3: During 2nd Relaxation:


• Current Distance of D > (Distance of B) + (Weight of B to D) i.e. Infinity
>5+2
o Dist[D] = 7
• Current Distance of C > (Distance of B) + (Weight of B to C) i.e. Infinity
>5+1
o Dist[C] = 6

Step 4: During 3rd Relaxation:


• Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. Infinity
>7+2
o Dist[F] = 9
• Current Distance of E > (Distance of C ) + (Weight of C to E) i.e. Infinity
>6+1
o Dist[E] = 7
Step 5: During 4th Relaxation:
• Current Distance of D > (Distance of E) + (Weight of E to D) i.e. 7 > 7 +
(-1)
o Dist[D] = 6
• Current Distance of E > (Distance of F ) + (Weight of F to E) i.e. 7 > 9 +
(-3)
o Dist[E] = 6
Step 6: During 5th Relaxation:
• Current Distance of F > (Distance of D) + (Weight of D to F) i.e. 9 > 6 +
2
o Dist[F] = 8
• Current Distance of D > (Distance of E ) + (Weight of E to D) i.e. 6 > 6 +
(-1)
o Dist[D] = 5
• Since the graph h 6 vertices, So during the 5th relaxation the shortest
distance for all the vertices should have been calculated.
Step 7: Now the final relaxation i.e. the 6th relaxation should indicate the
presence of negative cycle if there is any changes in the distance array of 5th
relaxation.
During the 6th relaxation, following changes can be seen:
• Current Distance of E > (Distance of F) + (Weight of F to E) i.e. 6 > 8 +
(-3)
o Dist[E]=5
• Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. 8 > 5 +
2
o Dist[F]=7
Since, we observed changes in the Distance array Hence ,we can conclude the
presence of a negative cycle in the graph.
A negative cycle (D->F->E) exists in the graph.

Algorithm to Find Negative Cycle in a Directed Weighted Graph Using Bellman-


Ford:
• Initialize distance array dist[] for each vertex ‘v‘ as dist[v] = INFINITY.
• Assume any vertex (let’s say ‘0’) as source and assign dist = 0.
• Relax all the edges(u,v,weight) N-1 times as per the below condition:
o dist[v] = minimum(dist[v], distance[u] + weight)
• Now, Relax all the edges one more time i.e. the Nth time and based on the
below two cases we can detect the negative cycle:
o Case 1 (Negative cycle exists): For any edge(u, v, weight), if dist[u]
+ weight < dist[v]
o Case 2 (No Negative cycle) : case 1 fails for all the edges.

Time and Space Complexity of Bellman–Ford Algorithm


It has a time complexity of O(V*E), where V is the number of vertices and E is
the number of edges in the graph. In the worst-case scenario, the algorithm needs
to iterate through all edges for each vertex, resulting in this time complexity.
The space complexity of the Bellman-Ford algorithm is O(V), where V is the
number of vertices in the graph. This space complexity is mainly due to storing
the distances from the source vertex to all other vertices in the graph.

Operation Time Complexity Space Complexity

Initialization O(V) O(V)

Relaxation O(V*E) O(1)

Overall Complexity O(V*E) O(V)

Time Complexity of Bellman-Ford Algorithm:


Best Case: O(E)
• The best-case time complexity of O(E) occurs when no relaxation is
required during Bellman-Ford algorithm execution.
• Algorithm terminates after a single pass through all edges without updating
any distances.
• Initial distances assigned to vertices are already the shortest paths from the
source vertex.
• With no relaxations needed, algorithm efficiently traverses each edge once
to verify validity.
• Time complexity directly proportional to number of edges, as algorithm
performs only one pass through all edges.
• Efficiency in edge traversal results in reduced time complexity, particularly
in sparse graphs with fewer edges.
Average Case: O(V*E)
• The average-case time complexity of Bellman-Ford algorithm
remains O(V*E).
• This complexity is maintained across various graph structures and
densities, as the algorithm's performance primarily depends on the number
of vertices and edges.
• In practical scenarios, the average case is similar to the worst case,
especially in dense graphs with many edges.
Worst Case: O(V*E)
• The worst-case time complexity of Bellman-Ford algorithm is
also O(V*E).
• This scenario occurs when the algorithm needs to iterate through all
vertices and edges for |V| - 1 passes, followed by one more pass for
detecting negative weight cycles.
• It's important to note that the worst case includes the presence of negative
weight cycles, which can cause the algorithm to run indefinitely if not
handled properly.
Question: Apply Bellman Ford Algorithm
To find the shortest path of the above graph, the first step is note down all the
edges which are given below:
(A, B), (A, C), (A, D), (B, E), (C, E), (D, C), (D, F), (E, F), (C, B)
Let's consider the source vertex as 'A'; therefore, the distance value at vertex A is
0 and the distance value at all the other vertices as infinity shown as below:

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

Therefore, the distance of vertex C is 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

Since (0 + 5) is less than ∞, so update


d(v) = d(u) + c(u , v)
d(v) = 0 + 5 = 5
Therefore, the distance of vertex D is 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:

Edges can be written as:


(1, 3), (1, 2), (2, 4), (3, 2), (4, 3)
First iteration
Consider the edge (1, 3). Denote vertex '1' as 'u' and vertex '3' as 'v'. Now
use the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 5
Since (0 + 5) is less than ∞, so update
1. d(v) = d(u) + c(u , v)
d(v) = 0 + 5 = 5
Therefore, the distance of vertex 3 is 5.
Consider the edge (1, 2). Denote vertex '1' as 'u' and vertex '2' as 'v'. Now
use the relaxing formula:
d(u) = 0
d(v) = ∞
c(u , v) = 4
Since (0 + 4) is less than ∞, so update
1. d(v) = d(u) + c(u , v)
d(v) = 0 + 4 = 4
Therefore, the distance of vertex 2 is 4.
Consider the edge (3, 2). Denote vertex '3' as 'u' and vertex '2' as 'v'. Now
use the relaxing formula:
d(u) = 5
d(v) = 4
c(u , v) = 7
Since (5 + 7) is greater than 4, so there would be no updation in the vertex 2.
Consider the edge (2, 4). Denote vertex '2' as 'u' and vertex '4' as 'v'. Now
use the relaxing formula:
d(u) = 4
d(v) = ∞
c(u , v) = 7
Since (4 + 7) equals to 11 which is less than ∞, so update
1. d(v) = d(u) + c(u , v)
d(v) = 4 + 7 = 11
Therefore, the distance of vertex 4 is 11.
Consider the edge (4, 3). Denote vertex '4' as 'u' and vertex '3' as 'v'. Now
use the relaxing formula:
d(u) = 11
d(v) = 5
c(u , v) = -15
Since (11 - 15) equals to -4 which is less than 5, so update
1. d(v) = d(u) + c(u , v)
d(v) = 11 - 15 = -4
Therefore, the distance of vertex 3 is -4.
Now we move to the second iteration.
Second iteration
Now, again we will check all the edges. The first edge is (1, 3). Since (0 + 5)
equals to 5 which is greater than -4 so there would be no updation in the vertex
3.
The next edge is (1, 2). Since (0 + 4) equals to 4 so there would be no updation
in the vertex 2.
The next edge is (3, 2). Since (-4 + 7) equals to 3 which is less than 4 so update:
d(v) = d(u) + c(u, v)
d(2) = d(3) +c(3, 2)
= -4 + 7 = 3
Therefore, the value at vertex 2 is 3.
The next edge is (2, 4). Since ( 3+7) equals to 10 which is less than 11 so update
d(v) = d(u) + c(u, v)
d(4) = d(2) +c(2, 4)
= 3 + 7 = 10
Therefore, the value at vertex 4 is 10.
The next edge is (4, 3). Since (10 - 15) equals to -5 which is less than -4 so update:
d(v) = d(u) + c(u, v)
d(3) = d(4) +c(4, 3)
= 10 - 15 = -5
Therefore, the value at vertex 3 is -5.
Now we move to the third iteration.
Third iteration
Now again we will check all the edges. The first edge is (1, 3). Since (0 + 5)
equals to 5 which is greater than -5 so there would be no updation in the vertex
3.
The next edge is (1, 2). Since (0 + 4) equals to 4 which is greater than 3 so there
would be no updation in the vertex 2.
The next edge is (3, 2). Since (-5 + 7) equals to 2 which is less than 3 so update:
d(v) = d(u) + c(u, v)
d(2) = d(3) +c(3, 2)
= -5 + 7 = 2
Therefore, the value at vertex 2 is 2.
The next edge is (2, 4). Since (2 + 7) equals to 9 which is less than 10 so update:
d(v) = d(u) + c(u, v)
d(4) = d(2) +c(2, 4)
=2+7=9
Therefore, the value at vertex 4 is 9.
The next edge is (4, 3). Since (9 - 15) equals to -6 which is less than -5 so update:
d(v) = d(u) + c(u, v)
d(3) = d(4) +c(4, 3)
= 9 - 15 = -6
Therefore, the value at vertex 3 is -6.
Since the graph contains 4 vertices, so according to the bellman ford algorithm,
there would be only 3 iterations. If we try to perform 4 th iteration on the graph,
the distance of the vertices from the given vertex should not change. If the
distance varies, it means that the bellman ford algorithm is not providing the
correct answer.
4th iteration
The first edge is (1, 3). Since (0 +5) equals to 5 which is greater than -6 so there
would be no change in the vertex 3.
The next edge is (1, 2). Since (0 + 4) is greater than 2 so there would be no
updation.
The next edge is (3, 2). Since (-6 + 7) equals to 1 which is less than 3 so update:
d(v) = d(u) + c(u, v)
d(2) = d(3) +c(3, 2)
= -6 + 7 = 1
In this case, the value of the vertex is updated. So, we conclude that the bellman
ford algorithm does not work when the graph contains the negative weight cycle.
Therefore, the value at vertex 2 is 1.

You might also like