如何获得“统一成本搜索”算法中的路径?

时间:2012-10-06 01:25:48

标签: algorithm search data-structures artificial-intelligence

我一直在使用统一成本搜索的算法,即使我能够理解整个优先级队列过程,我也无法理解算法的最后阶段。

如果我们查看at this graph,在应用算法后,我将获得每个节点的最小距离,但假设我想知道A到G之间的路径(就像示例一样),我将如何计算?

2 个答案:

答案 0 :(得分:12)

通常,您开始为尚未探索的每个节点提供无限的总成本。然后您可以稍微调整算法以保存前一个:

for each of node's neighbours n
    if n is not in explored
        if n is not in frontier
            frontier.add(n)
            set n's predecessor to node
        elif n is in frontier with higher cost
            replace existing node with n
            set n's predecessor to node

之后你可以从你的目标开始检查前辈的顺序。

答案 1 :(得分:0)

访问以获取更多信息  https://www.youtube.com/watch?v=9vNvrRP0ymw

Insert the root into the queue
While the queue is not empty
      Dequeue the maximum priority element from the queue
      (If priorities are same, alphabetically smaller path is chosen)
      If the path is ending in the goal state, print the path and exit
      Else
            Insert all the children of the dequeued element, with the cumulative costs as priority