What is the purpose of the visited set in Dijkstra?

后端 未结 3 679
遇见更好的自我
遇见更好的自我 2021-02-07 23:11

On the Wikipedia page for Dijkstra\'s algorithm, they mark visited nodes so they wouldn\'t be added to the queue again. However, if a node is visited then there can be no dista

3条回答
  •  独厮守ぢ
    2021-02-07 23:30

    Yes, you're right. There's no need for the visited vector.

    If a node is visited then there cannot exist a distance to that node that is shorter, so, as you said, checking alt < dist[v] is enough.

    Take a look here:

提交回复
热议问题