What does it mean to find the least expensive route from A to B if you get paid every time you travel from C to D?
If there is negative weight between the two nodes, the “shortest path” is to cycle back and forth between the two nodes forever. The more jumps, the shorter the path will be.
This is not related to the algorithm, and all this is due to the inability to answer such a question.
Edit
The above claim involves bidirectional links. If there are no cycles that have a common negative weight, you have no way to go in cycles forever by paying.
In this case, Dijkstra's algorithm may still crash:
Consider two ways:
- the optimal path that can withstand a cost of 100 before crossing the end edge, which has -25 weight, giving a total of 75, and
- suboptimal path that does not have negatively weighted edges with a total cost of 90.
The Dijkstra algorithm first explores the suboptimal path and declares itself complete when it finds it. It will never track a subpath that is worse than the first found.
Oddthinking
source share