Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bellman-Ford vs Dijkstra: Under what circumstances is Bellman-Ford better?

After a lot of Googling, I've found that most sources say that the Dijkstra algorithm is "more efficient" than the Bellman-Ford algorithm. But under what circumstances is the Bellman-Ford algorithm better than the Dijkstra algorithm?

I know "better" is a broad statement, so specifically I mean in terms of speed and also space if that applies. Surely there is some situation in which the Bellman-Ford approach is better than the Dijkstra approach.

like image 662
crazyCoder Avatar asked Oct 20 '13 20:10

crazyCoder


People also ask

Which is better Bellman-Ford or Dijkstra?

As we can see, Dijkstra's algorithm is better when it comes to reducing the time complexity. However, when we have negative weights, we have to go with the Bellman-Ford algorithm. Also, if we want to know whether the graph contains negative cycles or not, the Bellman-Ford algorithm can help us with that.

In what circumstances might we want to use Dijkstra's algorithm to compute all pairs shortest paths over the Floyd warshall algorithm?

If you E = O(V) , then running Dijkstra for all nodes if better both in theory and in practice. Basically, run Dijkstra from all nodes if you expect to have about as many edges as you have nodes, and run Floyd if you expect to have almost complete graphs.

What is the principle difference between Bellman-Ford and Dijkstra's algorithm implemented for Unicast routing?

In djikstra algo, we do relaxation of every node/vertices in a loop where as in bellmanford algo we perform relaxation only|v-1| times .


2 Answers

Bellman-Ford algorithm is a single-source shortest path algorithm, so when you have negative edge weight then it can detect negative cycles in a graph.

The only difference between the two is that Bellman-Ford is also capable of handling negative weights whereas Dijkstra Algorithm can only handle positives.

From wiki

However, Dijkstra's algorithm greedily selects the minimum-weight node that has not yet been processed, and performs this relaxation process on all of its outgoing edges; in contrast, the Bellman–Ford algorithm simply relaxes all the edges, and does this |V | − 1 times, where |V | is the number of vertices in the graph. In each of these repetitions, the number of vertices with correctly calculated distances grows, from which it follows that eventually all vertices will have their correct distances. This method allows the Bellman–Ford algorithm to be applied to a wider class of inputs than Dijkstra.

Dijkstra is however generally considered better in the absence of negative weight edges, as a typical binary heap priority queue implementation has O((|E|+|V|)log|V|) time complexity [A Fibonacci heap priority queue gives O(|V|log|V| + |E|)], while the Bellman-Ford algorithm has O(|V||E|) complexity

like image 84
Rahul Tripathi Avatar answered Sep 22 '22 06:09

Rahul Tripathi


As already stated in the chosen answer, Bellman-Ford performs the check on all the vertices, Dijkstra only on the one with the best distance calculated so far. Again already noted, this improves the complexity of the Dijkstra approach, however it requires to compare all the vertices to find out the minimum distance value. Being this not necessary in the Bellman-Ford, it is easier to implement in a distributed environment. That's why it is used in Distance Vector routing protocols (e.g., RIP and IGRP), where mostly local information is used. To use Dijkstra in routing protocols, instead, it is necessary first to distribute the entire topology, and this is what happens in Link State protocols, such as OSPF and ISIS.

like image 35
Halberdier Avatar answered Sep 19 '22 06:09

Halberdier