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.
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With