The question is, like, what happens if the closest node can't be reached from the previous closest node?
This step isn't necessary. As in, you aren't computing a path from the previous closest to the current closest, you are trying to get to your goal node, and the current closest is the only thing that matters (e.g. the algorithm doesn't care that last iteration you were 100km away, because this iteration you are only 96km away).
As a broad introduction, A* doesn't directly construct a path: it explores until it definitely knows that the path is contained within the region it has explored, and then constructs the path based on the information recorded during the exploration.
(I'm going to use the code in the Wikipedia article as a reference implementation to aid my explanation.)
You have a two sets of nodes: closedset
and openset
closedset
holds nodes that have been fully evaluated, that is, you know exactly how far they are from start
and all their neighbours are in one of the two sets. This there is no more computation you can do with them and so we can (sort of) ignore them. (Basically these are completely contained within the border.)
openset
holds "border" nodes, you know how far these are from start
, but you haven't touched their neighbours yet, so they are on the edge of your search so far.
(Implicitly, there is a third set: completely untouched nodes. But you don't really touch them until they are in openset
so they don't matter.)
At a given iteration, if you've got nodes to explore (that is, nodes in openset
), you need to work out which one to explore. This is the job of the heuristic, it basically gives you a hint about which point on the border will be the best to explore next by telling you which node it thinks will have the shortest path to goal
.
The previous closest node is irrelevant, it just expanded the border a bit, adding new nodes to openset
. These new nodes are now candidates for the closest node in this iteration.
At first, openset
only contains start
, but then you iterate and at each step the border is expanded a little (in the most promising direction), until you eventually reach goal
.
When A* is actually doing the exploration, it doesn't worry about which nodes came from where. It doesn't need to, because it knows their distance from start
and the heuristic function and that's all it needs.
However to reconstruct the path later, you need to have some record of the path, this is what camefrom
is. For a given node, camefrom
links it to the node that is closest to start
, so you can reconstruct the shortest path by following the links backwards from goal
.
How does one actually take a "graph" as a function argument?
By passing one of the representations of a graph.
I don't really see how A* applies to the TSP. I mean, finding a route from A to B, sure, I get that. But the TSP? I don't see the connection.
You need a different heuristic and a different end condition: goal
is no longer a single node any more, but the state of having everything connected; and your heuristic is some estimate of the length of the shortest path connecting the remaining nodes.