在A*搜索算法中,为什么要加上g(n)?

In the A* Search Algorithm, why do we add g(n)?

Per the wikipedia page

...f(n) = g(n) + h(n)

where n is the last node on the path, g(n) is the cost of the path from the start node to n, and h(n) is a heuristic that estimates the cost of the cheapest path from n to the goal.

为什么我们要考虑从起始节点到我们当前位置的路径成本?我一直在尝试实现这个算法来解决一个问题,并且一直在使用优先级队列,当我执行 g(n) + h(n) 时,它比仅使用严格的 h(n) 花费的时间更长。仅使用 h(n) 是否有意义,因为假设如果启发式算法是准确的,您只关心您离目标有多近?

编辑:实际上我刚刚发现我的 g(n) 函数计算错误,但逻辑上我仍然不明白为什么 g(n) + h(n) 会比 h(n) 更好.

了解为什么遗漏 g(n) 会导致算法不正确的一个简单方法是,实际成本永远不会进入算法。它完全依赖于启发式,只能保证是下限。

贪心搜索算法使用 f(n)=h(n) 因此它们是 non-complete 算法(换句话说,它不保证找到解决方案)。事实上,您在描述 爬山算法