Find the shortest path with the least number of edges

前端 未结 5 454
渐次进展
渐次进展 2021-01-18 07:48

I need to modify Dijkstra\'s algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path.

I\'ve been stuck

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 08:17

    Instead of assigning every node with the distance from source you can assign the number of edges traversed so far also. So each node will have (distance,edges) instead of distance only. Everything else works as usual and for every terminal node you record the one with minimum value of (distance,edges).

提交回复
热议问题