What is the most efficient way of finding a path through a small world graph?

前端 未结 7 1195
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 08:47

I have a sea of weighted nodes with edges linking clusters of nodes together. This graph follows the typical small world layout.

I wish to find a path finding algori

相关标签:
7条回答
  • 2020-12-25 09:16

    Definitely A*. A* will either find the best path possible or no path at all if no path exists. E.g. the path of this boat has been calculated using A*


    (source: cokeandcode.com)

    Here's an interactive Java Demo to play with. Please note that this algorithm is slowed down by sleeps, so you see it performing. Without this slow down it would find the path in less than a second.

    The algorithm is simple, yet powerful. Each node has 3 values, g is the cost up to this node. h is the estimated cost from this node to the target and f is the sum of both (it's a guess for the full path). A* maintains two lists, the Open and the Closed list. The Open list contains all nodes that have not been explored so far. The Closed list all nodes that have been explored. A node counts as explored if the algorithm has already tested every node connected to this node (connected could only mean horizontally and vertically, but also diagonal if diagonal moves between nodes are allowed).

    The algorithm could be described as

    1. Let P be the starting point
    2. Assign g, h, and f values to P
    3. Add P to the open list (at this point P is the only node on that list).
    4. Let B be the best node from the Open list (best == lowest f value)
      • If B is the goal node -> quit, you found the path
      • If the Open list is empty -> quit, no path exists
    5. Let C be a valid node connected to B
      • Assign g, h, and f to C
      • Check if C is on the Open or Closed List
        • If yes, check whether new path is most efficient (lower f-value)
          • If so, update the path
        • Else add C to the Open List
      • Repeat step 5 for all nodes connected to B
    6. Add B to the Closed list (we explored all neighbors)
    7. Repeat from step 4.

    Also have a look at Wikipedia for implementation details.

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