Bellman Ford Algorithm
Introduction:
Graph is a non-linear data structure which consists of nodes and edges. Graphs are used to find 'Shortest Path' if possible. Basically the very popular example for graphs is Google Maps as gives you different path suggestions to reach your destination.
What do you mean by shortest path?
"A path with minimum cost or distance between two vertices is called Shortest Path".
There are many algorithms for finding the shortest paths in a weighted graphs as listed below:
- Dijkstra’s Algorithm
- Bellman Ford Algorithm
- Floyd’s Algorithm
The Bellman-Ford algorithm, like Dijkstra’s Algorithm, uses the principle of relaxation to find increasingly accurate path length. Bellman-Ford, though, tackles two main issues with this process:
- If there are negative weight cycles, the search for a shortest path will go on forever.
- Choosing a bad ordering for relaxations leads to exponential relaxations.
- This step initializes distances from the source to all vertices as infinite and distance to the source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.
- This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph. Do following for each edge u-v
- If dist[v] > dist[u] + weight of edge uv, then update dist[v]
- dist[v] = dist[u] + weight of edge uv
- If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”. The idea is step 3 is that step 2 guarantees the shortest distance if the graph doesn't contain a negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle.
Example of Bellman Ford:
Let the given source vertex be 0. Initialize all distances as infinite, except the distance to the source itself. Total number of vertices in the graph is 5, so all edges must be processed 4 times.
Let all edges are processed in the following order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D). We get the following distances when all edges are processed the first time.The first row shows initial distances. The second row shows distances when edges (B, E), (D, B), (B, D) and (A, B) are processed. The third row shows distances when (A, C) is processed. The fourth row shows when (D, C), (B, C) and (E, D) are processed.
Complexity:
Best case Performance:
Worst case Performance:
Worst case space complexity:
Applications:
Routing- Bellman-Ford is used in the distance-vector routing protocol. This protocol decides how to route packets of data on a network. The distance equation (to decide weights in the network) is the number of routers a certain path must go through to reach its destination.



🤩good job!
ReplyDeleteGood one. Described with good Example ...
ReplyDeleteHi Geetu,
ReplyDeleteIt is very well written. Well done!
Good Geetu💐💐
ReplyDeleteNicely explained with simple examples! Good job.👍
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGood Job. Nicely explained.
ReplyDeleteExcellent Geeta...
ReplyDeleteGeetu...
DeleteMast hain yaar,
DeleteWell done 👍
Well done geetu👍👍
ReplyDelete