Skip to main content

Bellman Ford Graph Algorithm

The Shortest path algorithms so you go to google maps and you want to find the shortest path from one city to another.  Two algorithms can help you, they both calculate the shortest distance from a source node into all other nodes, one node can handle negative weights with cycles and another cannot, Dijkstra cannot and bellman ford can.

One is Dijkstra if you run the Dijkstra algorithm on this map its input would be a single source node and its output would be the path to all other vertices.  However, there is a caveat if Elon mask comes and with some magic creates a black hole loop which makes one of the edges negative weight then the Dijkstra algorithm would fail to give you the answer.

This is where bellman Ford algorithm comes into place, it's like the Dijkstra algorithm only it knows to handle well negative weight in edges.

Dijkstra has an issue handling negative weights and cycles

Bellman's ford algorithm target is to find the shortest path from a single node in a graph to all other nodes.

When we discussed Dijkstra's algorithm we saw that it's moving forward and it's quite efficient with a heap it's only O(E+V) * O(VlgV) however if the graph is having negative edge weights then Dijkstra's algorithm does not take care of it, it would simply fail Dijkstra algorithm is simply not intended for graphs with negative edge weights.  The reason for the above time complexity is that the Dijkstra's algorithm simply scan's the graph in a rather BFS way it's not a strict BFS there are rules to which vertices to visit next but it's like swooping the graph from your source node into all other nodes the reason for the O(VlgV) is that when it chooses which next node to choose then we use a heap sort to find the one currently having the minimal weight.

To recap here are the steps for Dijkstra algorithm are:
  1. Initialize distances to infinity.
  2. Pick first node and calculate distances to adjacent nodes.
  3. Pick next node with minimal distance; repeat adjacent node distance calculations.
  4. Final result of shortest-path tree.

So as Bellman's ford algorithm can handle negative edges it can also detect negative cycles.  Why would we need to detect negative cycle? in arbitrage trading we can find we could make money of thin air by selling on one stock exchange high and buying on another low, this is a negative cycle because we can continue this cycle take the money we made on the high stock exchange and with the negative edge buy on a lower price on the lower priced stock exchange.

Of course if we find out that our graph contains a negative cycle to a target node this means there would be no shortest path to this target node or it would be minus infinity.

So while the bellman ford algorithm can handle negative weighted graphs while the Dijkstra's cannot nothing in life comes for free and bellman ford is slower than Dijkstra's algorithm after all it's making fewer assumptions about the weights they should not all be non-negative.

When we have a negative weights its possible for a negative to exist and then Dijkstra algorithm would find better and better paths.

What would a negative infinity mean to us, it depends on our problem it can be a good thing to have negative infinity path but it could also mean to have a bad thing to have a negative infinity path.

Before we get to the actual Bellman ford algorithm let's reap the terms we have for graphs

E is the edges more specifically we are interested in the number of edges

A directed edge is an edge going from a source node to a target node with some weight.

V is the vertices more specifically we are interested in the number of vertices
S is the starting node
D is an array of size V that tracks the best distance from S to each node.

The distance D array first is initialized to positive infinity.

In our first step we go over array D which stores the distance the best distance from S to any node as infinity, we don't know yet of any distance, so we start with infinity.

However, we have a starting point, so we already know the distance to the starting node which means that D[S] = 0.  The path of the starting node to itself is zero.

Now we start reducing the weights of these edges, we are going to do this stay tuned:

For each edge (order does not matter) check if the edge makes the target node weight lower if so replace it with lower number, repeat this whole process for all edges V-1 Times
When we start with this process we are going to update all weights because any weight would be smaller than infinity which we initialized all weights in the shortest distance to.
  
 How do we detect negative cycles?

We run the algorithm a second time, if any node value changed then we update that node to having cost of negative infinity and that's it. When we say that we run the algorithm a second time we run again the V-1 times of edges relaxing.

At the end you return the distance array D which contains all the weights. Then if you want to know the shortest path from a node to another one you just find that target node in the distance array.

And that was bellman ford algorithm.

Comments

Popular posts from this blog

API Design Paper Summary and Review

API Design Paper Summary Introduction Is building API a solvable question, how far can we get into having good API’s and what is a good API at all? these are all very hard questions, usually you know the answers once you designed multiple APIs and got experience and then reviewed the decisions you have taken. Fortunately there are papers dealing with this problem exactly, for complex API’s used by a huge amount of people, the Qt API for example a very populate framework for desktop GUI building, and today we are going to go through a summary of that paper.

“The Little Manual of API Design” is a very nice paper written by Jasmin Blanchette has released a paper while working in trolltech, which is a Nokia company. I found it to be very clear and concise, and reassuring what we think of API design. It’s a difficult task that includes both artistic, social, programming and scientific skills. We are going to summarize this paper for you.

When you write an API you combine a set of symb…

Dev OnCall Patterns

IntroductionBeing On-Call is not easy. So does writing software. Being On-Call is not just a magic solution, anyone who has been On-Call can tell you that, it's a stressful, you could be woken up at the middle of the night, and be undress stress, there are way's to mitigate that. White having software developers as On-Calls has its benefits, in order to preserve the benefits you should take special measurements in order to mitigate the stress and lack of sleep missing work-life balance that comes along with it. Many software developers can tell you that even if they were not being contacted the thought of being available 24/7 had its toll on them. But on the contrary a software developer who is an On-Call's gains many insights into troubleshooting, responsibility and deeper understanding of the code that he and his peers wrote. Being an On-Call all has become a natural part of software development. Please note I do not call software development software engineering because …

Recursion Trees Primer

Recursion trees.

Controlling the fundamentals stands at the cornerstone of controlling a topic.  In our case in order to be a good developer its not enough or even not at all important to control the latest Java/JavaScript/big data technology but what's really important is the basics.  And the basics in computer science are maths, stats, algorithms and computer structure.

Steve wosniak the co-founder of apple said the same, what gave him his relative advantage was his deep understanding of programming and computer structure, this is what gave him the ability to create computer's which are less costly than the competitors (not that there were many) and by the way there were 3 founders to apple company one responsible for the technical side, one for the product and sales (Steve Jobs) and the third responsible for the company structure and growth, each of the three extremely important, it was not only the two Steve's but that's a topic for another episode.

And with that l…