A Shortest Path Algorithm With Minimum Number Of Nodes Traversed

后端 未结 4 972
夕颜
夕颜 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条回答
  • 2021-01-01 08:17

    I'm going to go out on a limb here, but have you tried the A* algorithm? I may have understood your question wrong, but it seems like A* would be a good starting point for what you want to do.

    Check out: http://en.wikipedia.org/wiki/A*_search_algorithm

    Some pseudo code there to help you out too :)

    0 讨论(0)
  • 2021-01-01 08:25

    Let me demonstrate that adding a constant value to all edges can change which route is "shortest" (least total weight of edges).

    Here's the original graph (a triangle):

    A-------B
     \  5  /
    2 \   / 2
       \ /
        C
    

    Shortest path from A to B is via C. Now add constant 2 to all edges. The shortest path becomes instead the single step from A directly to B (owing to "penalty" we've introduced for using additional edges).

    Note that the number of edges used is (excluding the node you start from) the same as the number of nodes visited.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 08:34

    If i understood the question correctly then its best analogy would be that used to find the best network path.

    In network communication a path may not only be selected because it is shortest but has many hop counts(node), thus may lead to distortion, interference and noise due to node connection.

    So the best path calculation contains the minimizing the function of variables as in your case Distance and Hop Count(nodes).

    You have to derive a functional equation that could relate the distance and node counts with quality.

    so something as suppose
    1 hop count change = 5 unit distance (which means the impact is same for 5unit distace or 1 node change)

    so to minimize the loss you can use it in the linear equation.
    minimize(distance + hopcount);
    where hopcount can be expressed as distance.

    0 讨论(0)
提交回复
热议问题