Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do Dijkstra and the Bellman-Ford algorithm both fail to find a shortest path?

I know Dijkstra fails when there are negative edge weights, but when do both algorithms fail?

like image 914
pearbear Avatar asked Oct 19 '25 14:10

pearbear


1 Answers

If there are negative cycles (reachable from the source), Bellman-Ford can be considered to fail. The main problem with a negative cycle is that you can just keep traversing it, reducing the cost of the path, thus there exists no finite shortest path to some vertices (so it's arguable whether Bellman-Ford actually failed or not - it can detect these cycles).

Dijkstra's algorithm will have a similar problem with negative cycles (not to mention the more general problem of dealing with negative edge weights).

Another scenario can be unreachable vertices, but again you can just detect that they're unreachable.

like image 130
Bernhard Barker Avatar answered Oct 22 '25 02:10

Bernhard Barker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!