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

AlgorithmDijkstraShortest PathBellman Ford

Algorithm Problem Overview


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.

Algorithm Solutions


Solution 1 - Algorithm

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

Solution 2 - Algorithm

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.

Solution 3 - Algorithm

There are 4 major difference among them I know:-

  1. bellman time complexity is O(VE) and Dijkstra Algo has O(ElogV)in case of maxheap is used.

  2. Bellman does relaxation for n-1 times and Dijkstra Algo only 1 time.

  3. Bellman can handle negative weights but Dijkstra Algo can't.

  4. Bellman visit a vertex more then once but Dijkstra Algo only once.

Solution 4 - Algorithm

The only difference is that Dijkstra's algorithm cannot handle negative edge weights which Bellman-ford handles.And bellman-ford also tells us whether the graph contains negative cycle. If graph doesn't contain negative edges then Dijkstra's is always better.

An efficient alternative for Bellman-ford is Directed Acyclic Graph (DAG) which uses topological sorting.

http://www.geeksforgeeks.org/shortest-path-for-directed-acyclic-graphs/

Solution 5 - Algorithm

Dijkstra Algo
Dijkstra algo is not capable to differentiate between Negative edge weight cycle is present in graph or not

1. Positive edge weight:- Dijkstra always PASS if all edge weight in a graph is positive
2. Negative edge wt. and No -ve edge wt. cycle:- Dijkstra always PASS even if we have some edges weight as Negative but NO cycle/loop in graph having negative edge weight.
[i.e No Negative edge weight cycle is present]
3. Negative edge wt. and -ve edge wt. cycle:- Dijkstra may PASS/FAIL even if we have some edges weight as negative along with cycle/loop in graph having negative edge weight.

Solution 6 - Algorithm

I do not agree completely, difference is in implementation and complexity, Dijsktra's algorithm is faster (O(n^2)) but difficult to implement, while Bellman Ford complexity is O(n^3) but is easier to implement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncrazyCoderView Question on Stackoverflow
Solution 1 - AlgorithmRahul TripathiView Answer on Stackoverflow
Solution 2 - AlgorithmHalberdierView Answer on Stackoverflow
Solution 3 - AlgorithmApporva AryaView Answer on Stackoverflow
Solution 4 - AlgorithmRahul BagadView Answer on Stackoverflow
Solution 5 - AlgorithmAjayView Answer on Stackoverflow
Solution 6 - Algorithm12KView Answer on Stackoverflow