A*
This article introduces the A* search algorithm.
The A search algorithm (pronounced A-star), abbreviated as A algorithm, is an algorithm for finding the shortest path between a given start and end node in a weighted directed graph. It belongs to graph traversal and best-first search algorithms, and is an improvement over BFS.
Process
The goal of the A* algorithm is to find the shortest path from the start node \(s\) to the end node \(t\) in a directed graph. Let \(d(x,y)\) be the distance between nodes \(x\) and \(y\), i.e., the length of the shortest path between them. Denote \(g(x)=d(s,x)\) as the distance function from the start node \(s\) to node \(x\), \(h^*(x)\) as the distance function from node \(x\) to the end node \(t\), and \(h(x)\) as an estimate of \(h^*(x)\)1. Finally, denote the estimated shortest path length from \(s\) to \(t\) via \(x\) as
During the search, the A* algorithm each time extracts a node with the smallest \(f\) from the priority queue. Then, it pushes all its successor nodes \(x\) into the priority queue and updates \(f(x)\) using the actual recorded \(g(x)\) and the estimated \(h(x)\).
Properties
Since the actual value of \(h^*(x)\) is unknown during the search, an easily computable \(h(x)\) must be used as its estimate. The actual complexity of the A search depends on the properties of this estimation function \(h(x)\). It is easy to imagine that if \(h\equiv h^*\), i.e., the estimate is exact, then the search process will strictly follow the shortest path. If \(h\equiv 0\), then the A algorithm degenerates to Dijkstra's algorithm; when \(h\equiv 0\) and edge weights are \(1\), this becomes BFS.
Assume the graph has no negative-weight edges. If the estimate \(h(x)\) never exceeds the actual distance \(h^*(x)\), i.e., \(0\le h\le h^*\), then the A algorithm is guaranteed to find the optimal solution. An estimation function \(h(x)\) satisfying this condition is called admissible. According to the previous discussion, the closer \(h\) is to \(h^*\), the more efficient the corresponding A algorithm is. Generally, in the worst case, the algorithm will traverse all nodes satisfying
where \(C^*\) is the shortest distance between the start node \(s\) and the end node \(t\). Intuitively, the closer \(h\) is to \(h^*\), the fewer successor nodes satisfy this condition each time an expansion is performed, and thus the fewer branches the algorithm searches. Therefore, the A* algorithm can be seen as a "pruning" optimization of the search algorithm.
If \(h\) is not only admissible but also consistent, i.e.,
then the A* algorithm will not re-add nodes that have already been popped from the queue to the queue. The consistency condition can be understood as the triangle inequality among nodes \(x\), \(y\), and \(t\).
Example Problems
A classic application of the A algorithm is solving the k-th shortest path problem. For the description of this problem, the A approach, and the more complexity-efficient persistent mergeable heap approach, please refer to the k-th shortest path problem page.
This section introduces a classic problem that can be solved using the A* algorithm.
Eight Puzzle
On a \(3\times 3\) board, there are eight tiles, each marked with a number from \(1\) to \(8\). There is one empty space on the board, represented by \(0\). Tiles adjacent to the empty space can be moved into it, making the original position become the empty space. Given an initial layout and a target layout (to simplify the problem, the target state is set as follows), find a method to move from the initial layout to the target layout with the minimum number of steps.
Solution Idea
The \(h\) function can be defined as the number of tiles not in their correct positions. It is easy to see that \(h\) is both admissible and consistent. This problem can be solved using the A* algorithm.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
References and Notes
-
Here \(h\) stands for heuristic. See Heuristic search - Wikipedia and the Bounded relaxation section of A* search algorithm - Wikipedia. ↩
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用