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
Let's take a look at Bellman Ford Algorithm.


Bellman Ford Algorithm:

Imagine a scenario where you need to get to a baseball game from your house. Along the way, on each road, one of two things can happen. First, sometimes the road you're using is a toll road, and you have to pay a certain amount of money. Second, sometimes someone you know lives on that street (like a family member or a friend). Those people can give you money to help you restock your wallet. You need to get across town, and you want to arrive across town with as much money as possible so you can buy hot dogs. Given that you know which roads are toll roads and which roads have people who can give you money, you can use Bellman-Ford to help plan the optimal route.

The Bellman-Ford algorithm operates on an input graph, G, with |V| vertices and |E| edges. A single source vertex, s, must be provided as well, as the Bellman-Ford algorithm is a single-source shortest path algorithm. No destination vertex needs to be supplied, however, because Bellman-Ford calculates the shortest distance to all vertices in the graph from the source vertex.

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.
The algorithm is as follows:
  1. 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.
  2. 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
     3. This step reports if there is a negative weight cycle in graph. Do following for each edge 
         (u-v)

  • 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.


The first iteration guarantees to give all shortest paths which are at most 1 edge long. We get the following distances when all edges are processed second time (The last row shows final values).


The second iteration guarantees to give all shortest paths which are at most 2 edges long. The algorithm processes all edges 2 more times. The distances are minimized after the second iteration, so third and fourth iterations don’t update the distances.


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.



Comments

Post a Comment