问题
Dijkstra's algorithm is f(n) = g(n)
and A* is f(n) = g(n) + h(n)
.
g(n) is the cost of the path from the start node to n.
h(n) is a heuristic function that estimates the cost of the cheapest path from n to the goal.
Is g(n) needed? Can't it find the shortest path without g(n)?
Why does A* need g(n)?
回答1:
We need g(n)
Consider the case when h(n)
is 0
for all nodes on some given path to the goal (which is a perfectly valid, i.e. admissible, heuristic) and non-zero for all other nodes.
If we ignore the cost so far (g(n)
), clearly we will pick the nodes on this path no matter what the actual cost is, so the path we end up with can have a much greater cost than some other path.
start
g(n)=0 O --
5 | \ 1
h(n)=0,g(n)=5 O O h(n)=1,g(n)=1
5 | / 1
h(n)=0,g(n)=10 O --
goal
In the above example, we will pick the node on the left, and then the goal, since h(n) = 0
for both (which is greater than h(n) = 1
for the node on the right). This will give us a path with cost 10
, where the cheapest path involves picking the node on the right, for a cost of 2
.
This is perhaps an extreme example, but the same idea applies in many other cases. For example, you could also add 10 to all values in my example and have it be part of a larger graph and it would still end up incorrectly picking the left side above the right.
The more general conclusion here is that you can have a choice between 2 nodes n1
and n2
where h(n1) < h(n2)
, thus we'll pick n1
, but n2
is on the cheapest path, not n1
.
We can also pick the wrong node if we include g(n)
. But, in that case, for some node n
on the path including n1
, f(n)
will be greater than the cheapest path (in the worst case n
will be the goal and f(n)
will be the true cost to reach it through n1
, which is clearly more expensive than the actual cheapest path), and thus also greater than f(n2)
(since a heuristic needs to underestimate the cost), so we'll explore n2
before reaching the goal.
If h(n)
were the true cost (and not an estimate)
Then we indeed wouldn't need g(n)
.
But only considering h(n)
will make it a greedy algorithm in this case (assuming nonnegative edge weights), since h(n)
will decrease for each node we pick (since we're moving close to the goal), so at the starting node we'd pick a node on the optimal path (since it will have the lowest h(n)
), and from there we'll just keep picking nodes on the optimal path.
来源:https://stackoverflow.com/questions/52420788/why-does-the-a-star-algorithm-need-gn