Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Bellman Ford and Dijkstra's algorithm

   2           1
1----------2---------4
|          |         |
|3         |3        |1
|    6     |         |
3---------5 ---------

Ok, so this is the graph. My source node is 1 and destination node is 5

My question is.

Are both the algorithms going to give the same output or not? That is, will both return 1->2->4->5? (Except that negative weights are not allowed in dijkstra's)

Thanks in advance for help.

like image 941
Gaurav Gandhi Avatar asked Apr 29 '13 07:04

Gaurav Gandhi


People also ask

What are Dijkstra and Bellman-Ford algorithms?

1. Overview In this tutorial, we’ll give an overview of the Dijkstra and Bellman-Ford algorithms. We’ll discuss their similarities and differences. Then, we’ll summarize when to use each algorithm. 2. Dijkstra’s Algorithm Dijkstra’s algorithm is one of the SSSP (Single Source Shortest Path) algorithms.

Does Bellman Ford’s algorithm work when there is negative weight edge?

Bellman Ford’s Algorithm works when there is negative weight edge, it also detects the negative weight cycle. Dijkstra’s Algorithm doesn’t work when there is negative weight edge.

Why does Dijkstra’s algorithm fail to work with negative weights?

When working with graphs that have negative weights, Dijkstra’s algorithm fails to calculate the shortest paths correctly. The reason is that might be negative, which will make it possible to reach from at a lower cost. Therefore, we can’t prove the optimality of choosing the node that has the lowest cost.

What are the weaknesses of Bellman-Ford algorithm?

However, this algorithm also has a weakness which is a graph that has a negative cycle [1] [2], so for graphs that have negative cycles it cannot calculate the shortest path. ... In this study a review of the existing Bellman-Ford Algorithm by conducting tests to see the accuracy of the route data or the shortest route.


1 Answers

Bellman-Ford algorithm is a single-source shortest path algorithm, which allows for negative edge weight and can detect negative cycles in a graph.

Dijkstra algorithm is also another single-source shortest path algorithm. However, the weight of all the edges must be non-negative.

For your case, as far as the total cost is concerned, there will be no difference, since the edges in the graph have non-negative weight. However, Dijkstra's algorithm is usually used, since the typical implementation with binary heap has Theta((|E|+|V|)log|V|) time complexity, while Bellman-Ford algorithm has O(|V||E|) complexity.

If there are more than 1 path that has minimum cost, the actual path returned is implementation dependent (even for the same algorithm).

like image 198
nhahtdh Avatar answered Oct 13 '22 18:10

nhahtdh