Find the shortest path with the least number of edges

前端 未结 5 485
渐次进展
渐次进展 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:07

    Another option would be to add some small number ε to the weight of every edge, where ε << edgeWeights (for integer edge-weights you can choose ε < gcd(edgeWeights)/numberOfEdges)

    The advantage of this approach is that you don't need to write your own pathfinder, you can just use any off-the-shelf implementation.

提交回复
热议问题