A Shortest Path Algorithm With Minimum Number Of Nodes Traversed

后端 未结 4 989
夕颜
夕颜 2021-01-01 07:38

I am looking for a Dijkstra\'s algorithm implementation, that also takes into consideration the number of nodes traversed.

What I mean is, a typical Dijkstra\'s algo

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 08:26

    The way you can do that is adapt the weights of the edges to always be 1, so that you traverse 5 nodes, and you've gone a distance of "5". The algorithm would be the same at that point, optimizing for number of nodes traversed rather than distance traveled.

    If you want some sort of hybrid, you need to determine how much importance to give to traversing a node and the distance. The weight used in calculations should look something like:

    weight = node_importance * 1 + (1 - node_importance) * distance

    Where node_importance would be a percentage which gauges how much distance is a factor and how much minimum node traversal is important. Though you may have to normalize the distances to be an average of 1.

提交回复
热议问题