Dijkstra's algorithm with negative weights

后端 未结 7 1443
有刺的猬
有刺的猬 2020-11-30 01:06

Can we use Dijkstra\'s algorithm with negative weights?

STOP! Before you think \"lol nub you can just endlessly hop between two points and get an in

7条回答
  •  臣服心动
    2020-11-30 01:35

    If you read the proof of optimality, one of the assumptions made is that all the weights are non-negative. So, no. As Bart recommends, use Bellman-Ford if there are no negative cycles in your graph.

    You have to understand that a negative edge isn't just a negative number --- it implies a reduction in the cost of the path. If you add a negative edge to your path, you have reduced the cost of the path --- if you increment the weights so that this edge is now non-negative, it does not have that reducing property anymore and thus this is a different graph.

    I encourage you to read the proof of optimality --- there you will see that the assumption that adding an edge to an existing path can only increase (or not affect) the cost of the path is critical.

提交回复
热议问题