我一直在研究均匀成本搜索算法,尽管我能够理解整个优先队列的过程,但我无法理解算法的最后阶段。
如果我们查看这个图表,在应用算法后,我将获得每个节点的最小距离,但假设我想知道从A到G的路径(就像示例中那样),我该如何计算呢?
回答:
通常,你会为每个尚未探索的节点设置一个无限的总成本。然后你可以稍微调整算法来保存前驱节点:
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
之后,你只需从目标节点开始,检查前驱节点的序列即可。