A* search algorithm explained

Class:Search algorithm
Data:Graph
Time:

O(

):O(b^d)
Space:

O(

):O(b^d)

A* (pronounced "A-star") is a graph traversal and pathfinding algorithm, which is used in many fields of computer science due to its completeness, optimality, and optimal efficiency.[1] Given a weighted graph, a source node and a goal node, the algorithm finds the shortest path (with respect to the given weights) from source to goal.

One major practical drawback is its

O(bd)

space complexity where is the depth of the solution (the length of the shortest path) and is the branching factor (the average number of successors per state), as it stores all generated nodes in memory. Thus, in practical travel-routing systems, it is generally outperformed by algorithms that can pre-process the graph to attain better performance,[2] as well as by memory-bounded approaches; however, A* is still the best solution in many cases.[3]

Peter Hart, Nils Nilsson and Bertram Raphael of Stanford Research Institute (now SRI International) first published the algorithm in 1968.[4] It can be seen as an extension of Dijkstra's algorithm. A* achieves better performance by using heuristics to guide its search.

Compared to Dijkstra's algorithm, the A* algorithm only finds the shortest path from a specified source to a specified goal, and not the shortest-path tree from a specified source to all possible goals. This is a necessary trade-off for using a specific-goal-directed heuristic. For Dijkstra's algorithm, since the entire shortest-path tree is generated, every node is a goal, and there can be no specific-goal-directed heuristic.

History

A* was created as part of the Shakey project, which had the aim of building a mobile robot that could plan its own actions. Nils Nilsson originally proposed using the Graph Traverser algorithm[5] for Shakey's path planning.[6] Graph Traverser is guided by a heuristic function, the estimated distance from node to the goal node: it entirely ignores, the distance from the start node to . Bertram Raphael suggested using the sum, .[7] Peter Hart invented the concepts we now call admissibility and consistency of heuristic functions. A* was originally designed for finding least-cost paths when the cost of a path is the sum of its costs, but it has been shown that A* can be used to find optimal paths for any problem satisfying the conditions of a cost algebra.[8]

The original 1968 A* paper contained a theorem stating that no A*-like algorithm could expand fewer nodes than A* if the heuristic function is consistent and A*'s tie-breaking rule is suitably chosen. A "correction" was published a few years later[9] claiming that consistency was not required, but this was shown to be false in 1985 in Dechter and Pearl's definitive study of A*'s optimality (now called optimal efficiency), which gave an example of A* with a heuristic that was admissible but not consistent expanding arbitrarily more nodes than an alternative A*-like algorithm.

Description

A* is an informed search algorithm, or a best-first search, meaning that it is formulated in terms of weighted graphs: starting from a specific starting node of a graph, it aims to find a path to the given goal node having the smallest cost (least distance travelled, shortest time, etc.). It does this by maintaining a tree of paths originating at the start node and extending those paths one edge at a time until the goal node is reached.

At each iteration of its main loop, A* needs to determine which of its paths to extend. It does so based on the cost of the path and an estimate of the cost required to extend the path all the way to the goal. Specifically, A* selects the path that minimizes

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

where is the next node on the path, is the cost of the path from the start node to, and is a heuristic function that estimates the cost of the cheapest path from to the goal. The heuristic function is problem-specific. If the heuristic function is admissible – meaning that it never overestimates the actual cost to get to the goal – A* is guaranteed to return a least-cost path from start to goal.

Typical implementations of A* use a priority queue to perform the repeated selection of minimum (estimated) cost nodes to expand. This priority queue is known as the open set, fringe or frontier. At each step of the algorithm, the node with the lowest value is removed from the queue, the and values of its neighbors are updated accordingly, and these neighbors are added to the queue. The algorithm continues until a removed node (thus the node with the lowest value out of all fringe nodes) is a goal node. The value of that goal is then also the cost of the shortest path, since at the goal is zero in an admissible heuristic.

The algorithm described so far gives us only the length of the shortest path. To find the actual sequence of steps, the algorithm can be easily revised so that each node on the path keeps track of its predecessor. After this algorithm is run, the ending node will point to its predecessor, and so on, until some node's predecessor is the start node.

As an example, when searching for the shortest route on a map, might represent the straight-line distance to the goal, since that is physically the smallest possible distance between any two points. For a grid map from a video game, using the Taxicab distance or the Chebyshev distance becomes better depending on the set of movements available (4-way or 8-way).

If the heuristic satisfies the additional condition for every edge of the graph (where denotes the length of that edge), then is called monotone, or consistent. With a consistent heuristic, A* is guaranteed to find an optimal path without processing any node more than once and A* is equivalent to running Dijkstra's algorithm with the reduced cost .[10]

Pseudocode

The following pseudocode describes the algorithm:

function reconstruct_path(cameFrom, current) total_path := while current in cameFrom.Keys: current := cameFrom[current] total_path.prepend(current) return total_path

// A* finds a path from start to goal.// h is the heuristic function. h(n) estimates the cost to reach goal from node n.function A_Star(start, goal, h) // The set of discovered nodes that may need to be (re-)expanded. // Initially, only the start node is known. // This is usually implemented as a min-heap or priority queue rather than a hash-set. openSet :=

// For node n, cameFrom[n] is the node immediately preceding it on the cheapest path from the start // to n currently known. cameFrom := an empty map

// For node n, gScore[n] is the cost of the cheapest path from start to n currently known. gScore := map with default value of Infinity gScore[start] := 0

// For node n, fScore[n] := gScore[n] + h(n). fScore[n] represents our current best guess as to // how cheap a path could be from start to finish if it goes through n. fScore := map with default value of Infinity fScore[start] := h(start)

while openSet is not empty // This operation can occur in O(Log(N)) time if openSet is a min-heap or a priority queue current := the node in openSet having the lowest fScore[] value if current = goal return reconstruct_path(cameFrom, current)

openSet.Remove(current) for each neighbor of current // d(current,neighbor) is the weight of the edge from current to neighbor // tentative_gScore is the distance from start to the neighbor through current tentative_gScore := gScore[current] + d(current, neighbor) if tentative_gScore < gScore[neighbor] // This path to neighbor is better than any previous one. Record it! cameFrom[neighbor] := current gScore[neighbor] := tentative_gScore fScore[neighbor] := tentative_gScore + h(neighbor) if neighbor not in openSet openSet.add(neighbor)

// Open set is empty but goal was never reached return failure

Remark: In this pseudocode, if a node is reached by one path, removed from openSet, and subsequently reached by a cheaper path, it will be added to openSet again. This is essential to guarantee that the path returned is optimal if the heuristic function is admissible but not consistent. If the heuristic is consistent, when a node is removed from openSet the path to it is guaranteed to be optimal so the test ‘tentative_gScore < gScore[neighbor]’ will always fail if the node is reached again.

Example

An example of an A* algorithm in action where nodes are cities connected with roads and h(x) is the straight-line distance to the target point:

Key: green: start; blue: goal; orange: visited

The A* algorithm has real-world applications. In this example, edges are railroads and h(x) is the great-circle distance (the shortest possible distance on a sphere) to the target. The algorithm is searching for a path between Washington, D.C., and Los Angeles.

Implementation details

There are a number of simple optimizations or implementation details that can significantly affect the performance of an A* implementation. The first detail to note is that the way the priority queue handles ties can have a significant effect on performance in some situations. If ties are broken so the queue behaves in a LIFO manner, A* will behave like depth-first search among equal cost paths (avoiding exploring more than one equally optimal solution).

When a path is required at the end of the search, it is common to keep with each node a reference to that node's parent. At the end of the search, these references can be used to recover the optimal path. If these references are being kept then it can be important that the same node doesn't appear in the priority queue more than once (each entry corresponding to a different path to the node, and each with a different cost). A standard approach here is to check if a node about to be added already appears in the priority queue. If it does, then the priority and parent pointers are changed to correspond to the lower-cost path. A standard binary heap based priority queue does not directly support the operation of searching for one of its elements, but it can be augmented with a hash table that maps elements to their position in the heap, allowing this decrease-priority operation to be performed in logarithmic time. Alternatively, a Fibonacci heap can perform the same decrease-priority operations in constant amortized time.

Special cases

Dijkstra's algorithm, as another example of a uniform-cost search algorithm, can be viewed as a special case of A* where for all x.[11] [12] General depth-first search can be implemented using A* by considering that there is a global counter C initialized with a very large value. Every time we process a node we assign C to all of its newly discovered neighbors. After every single assignment, we decrease the counter C by one. Thus the earlier a node is discovered, the higher its value. Both Dijkstra's algorithm and depth-first search can be implemented more efficiently without including an value at each node.

Properties

Termination and completeness

On finite graphs with non-negative edge weights A* is guaranteed to terminate and is complete, i.e. it will always find a solution (a path from start to goal) if one exists. On infinite graphs with a finite branching factor and edge costs that are bounded away from zero (d(x,y)>\varepsilon>0 for some fixed

\varepsilon

), A* is guaranteed to terminate only if there exists a solution.

Admissibility

A search algorithm is said to be admissible if it is guaranteed to return an optimal solution. If the heuristic function used by A* is admissible, then A* is admissible. An intuitive "proof" of this is as follows:

When A* terminates its search, it has found a path from start to goal whose actual cost is lower than the estimated cost of any path from start to goal through any open node (the node's value). When the heuristic is admissible, those estimates are optimistic (not quite—see the next paragraph), so A* can safely ignore those nodes because they cannot possibly lead to a cheaper solution than the one it already has. In other words, A* will never overlook the possibility of a lower-cost path from start to goal and so it will continue to search until no such possibilities exist.

The actual proof is a bit more involved because the values of open nodes are not guaranteed to be optimistic even if the heuristic is admissible. This is because the values of open nodes are not guaranteed to be optimal, so the sum is not guaranteed to be optimistic.

Optimality and consistency

Algorithm A is optimally efficient with respect to a set of alternative algorithms Alts on a set of problems P if for every problem P in P and every algorithm A′ in Alts, the set of nodes expanded by A in solving P is a subset (possibly equal) of the set of nodes expanded by A′ in solving P. The definitive study of the optimal efficiency of A* is due to Rina Dechter and Judea Pearl.[13] They considered a variety of definitions of Alts and P in combination with A*'s heuristic being merely admissible or being both consistent and admissible. The most interesting positive result they proved is that A*, with a consistent heuristic, is optimally efficient with respect to all admissible A*-like search algorithms on all "non-pathological" search problems. Roughly speaking, their notion of the non-pathological problem is what we now mean by "up to tie-breaking". This result does not hold if A*'s heuristic is admissible but not consistent. In that case, Dechter and Pearl showed there exist admissible A*-like algorithms that can expand arbitrarily fewer nodes than A* on some non-pathological problems.

Optimal efficiency is about the set of nodes expanded, not the number of node expansions (the number of iterations of A*'s main loop). When the heuristic being used is admissible but not consistent, it is possible for a node to be expanded by A* many times, an exponential number of times in the worst case.[14] In such circumstances, Dijkstra's algorithm could outperform A* by a large margin. However, more recent research found that this pathological case only occurs in certain contrived situations where the edge weight of the search graph is exponential in the size of the graph and that certain inconsistent (but admissible) heuristics can lead to a reduced number of node expansions in A* searches.[15] [16]

Bounded relaxation

While the admissibility criterion guarantees an optimal solution path, it also means that A* must examine all equally meritorious paths to find the optimal path. To compute approximate shortest paths, it is possible to speed up the search at the expense of optimality by relaxing the admissibility criterion. Oftentimes we want to bound this relaxation, so that we can guarantee that the solution path is no worse than (1 + ε) times the optimal solution path. This new guarantee is referred to as ε-admissible.

There are a number of ε-admissible algorithms:

w(n)=\begin{cases}1-

d(n)
N

&d(n)\leN\ 0&otherwise\end{cases}

, and where is the depth of the search and N is the anticipated length of the solution path.
*
A
\varepsilon
.[20] uses two heuristic functions. The first is the FOCAL list, which is used to select candidate nodes, and the second hF is used to select the most promising node from the FOCAL list.

f\alpha(n)=(1+w\alpha(n))f(n)

, where

w\alpha(n)=\begin{cases}λ&g(\pi(n))\leg(\tilde{n})\ Λ&otherwise\end{cases}

, where λ and Λ are constants with

λ\leΛ

, π(n) is the parent of n, and ñ is the most recently expanded node.

Complexity

The time complexity of A* depends on the heuristic. In the worst case of an unbounded search space, the number of nodes expanded is exponential in the depth of the solution (the shortest path) :, where is the branching factor (the average number of successors per state). This assumes that a goal state exists at all, and is reachable from the start state; if it is not, and the state space is infinite, the algorithm will not terminate.

The heuristic function has a major effect on the practical performance of A* search, since a good heuristic allows A* to prune away many of the nodes that an uninformed search would expand. Its quality can be expressed in terms of the effective branching factor, which can be determined empirically for a problem instance by measuring the number of nodes generated by expansion,, and the depth of the solution, then solving[23]

N+1=1+b*+(b*)2+...+(b*)d.

Good heuristics are those with low effective branching factor (the optimal being).

The time complexity is polynomial when the search space is a tree, there is a single goal state, and the heuristic function h meets the following condition:

|h(x)-h*(x)|=O(logh*(x))

where is the optimal heuristic, the exact cost to get from to the goal. In other words, the error of will not grow faster than the logarithm of the "perfect heuristic" that returns the true distance from to the goal.[24] [25]

The space complexity of A* is roughly the same as that of all other graph search algorithms, as it keeps all generated nodes in memory. In practice, this turns out to be the biggest drawback of the A* search, leading to the development of memory-bounded heuristic searches, such as Iterative deepening A*, memory-bounded A*, and SMA*.

Applications

A* is often used for the common pathfinding problem in applications such as video games, but was originally designed as a general graph traversal algorithm.[4] It finds applications in diverse problems, including the problem of parsing using stochastic grammars in NLP.[26] Other cases include an Informational search with online learning.[27]

Relations to other algorithms

What sets A* apart from a greedy best-first search algorithm is that it takes the cost/distance already traveled,, into account.

Some common variants of Dijkstra's algorithm can be viewed as a special case of A* where the heuristic

h(n)=0

for all nodes;[11] [12] in turn, both Dijkstra and A* are special cases of dynamic programming.[28] A* itself is a special case of a generalization of branch and bound.[29]

A* is similar to beam search except that beam search maintains a limit on the numbers of paths that it has to explore.[30]

Variants

A* can also be adapted to a bidirectional search algorithm. Special care needs to be taken for the stopping criterion.[34]

See also

Further reading

External links

Notes and References

  1. Book: Russell, Stuart J.. Artificial intelligence a modern approach . 2018. Pearson. Norvig, Peter. 978-0134610993. 4th. Boston. 1021874142.
  2. Book: Delling . D. . Sanders . P. . Peter Sanders (computer scientist) . Schultes . D. . Wagner . D. . Dorothea Wagner . 10.1007/978-3-642-02094-0_7 . 117–139 . Springer . Algorithmics of Large and Complex Networks: Design, Analysis, and Simulation . 5515 . 2009. Engineering Route Planning Algorithms . Lecture Notes in Computer Science . 978-3-642-02093-3 .
  3. W. . Zeng . Church, R. L. . Finding shortest paths on real road networks: the case for A* . International Journal of Geographical Information Science . 4 . 531–543 . 2009 . 10.1080/13658810801949850 . 23 . 14833639 .
  4. P. E. . Peter E. Hart . Hart . Nilsson . N.J.. Raphael . B. . Nils Nilsson (researcher) . Bertram Raphael . A Formal Basis for the Heuristic Determination of Minimum Cost Paths . IEEE Transactions on Systems Science and Cybernetics . 2 . 100–7 . 1968 . 10.1109/TSSC.1968.300136 . 4 .
  5. Doran. J. E.. Michie. D.. 1966-09-20. Experiments with the Graph Traverser program. Proc. R. Soc. Lond. A . 294. 1437. 235–259. 10.1098/rspa.1966.0205 . 1966RSPSA.294..235D. 21698093.
  6. Book: Nilsson, Nils J.. The Quest for Artificial Intelligence. 2009-10-30. Cambridge University Press. 9780521122931. Cambridge. en. One of the first problems we considered was how to plan a sequence of 'way points' that Shakey could use in navigating from place to place. […] Shakey's navigation problem is a search problem, similar to ones I have mentioned earlier..
  7. Book: Nilsson, Nils J.. The Quest for Artificial Intelligence. 2009-10-30. Cambridge University Press. 9780521122931. Cambridge. en. Bertram Raphael, who was directing work on Shakey at that time, observed that a better value for the score would be the sum of the distance traveled so far from the initial position plus my heuristic estimate of how far the robot had to go..
  8. Edelkamp. Stefan. Jabbar. Shahid. Lluch-Lafuente. Alberto. 2005. Cost-Algebraic Heuristic Search. Proceedings of the Twentieth National Conference on Artificial Intelligence (AAAI). 1362–7 . 978-1-57735-236-5.
  9. Hart. Peter E.. Nilsson. Nils J.. Raphael. Bertram. 1972-12-01. Correction to 'A Formal Basis for the Heuristic Determination of Minimum Cost Paths'. ACM SIGART Bulletin. 37. 28–29. 10.1145/1056777.1056779. 6386648 .
  10. Nannicini . Giacomo . Delling . Daniel . Schultes . Dominik . Liberti . Leo . 10.1002/NET.20438 . 2 . Networks . 240–251 . Bidirectional A* search on time-dependent road networks . 59 . 2012.
  11. .
  12. .
  13. Rina . Dechter . Judea Pearl . Generalized best-first search strategies and the optimality of A* . . 32 . 3 . 505–536 . 1985 . 10.1145/3828.3830 . 2092415 . free .
  14. Alberto . Martelli . On the Complexity of Admissible Search Algorithms . . 8 . 1 . 1–13 . 1977 . 10.1016/0004-3702(77)90002-9 .
  15. Ariel . Felner . Uzi Zahavi . Inconsistent heuristics in theory and practice . . 175 . 9–10 . 1570–1603 . 2011 . 10.1016/j.artint.2011.02.001. free .
  16. Zhifu . Zhang . N. R. Sturtevant . 2009 . Using Inconsistent Heuristics on A* Search . Twenty-First International Joint Conference on Artificial Intelligence . 634–639.
  17. Ira . Pohl . First results on the effect of error in heuristic search . Machine Intelligence 5 . 219–236 . 978-0-85224-176-9 . 1067280266 . 1970 . Edinburgh University Press.
  18. Ira . Pohl . The avoidance of (relative) catastrophe, heuristic competence, genuine dynamic weighting and computational issues in heuristic problem solving . Proceedings of the Third International Joint Conference on Artificial Intelligence (IJCAI-73) . 3 . 11–17 . California, USA . August 1973 .
  19. Andreas . Köll . Hermann Kaindl . A new approach to dynamic weighting . Proceedings of the Tenth European Conference on Artificial Intelligence (ECAI-92) . 16–17 . Vienna, Austria . August 1992 . 978-0-471-93608-4 . Wiley.
  20. Judea . Pearl . Jin H. Kim . Studies in semi-admissible heuristics . IEEE Transactions on Pattern Analysis and Machine Intelligence . 4 . 4 . 392–399 . 1982. 10.1109/TPAMI.1982.4767270 . 21869053 . 3176931 .
  21. Malik . Ghallab . Dennis Allard . Aε – an efficient near admissible heuristic search algorithm . Proceedings of the Eighth International Joint Conference on Artificial Intelligence (IJCAI-83) . 2 . 789–791 . Karlsruhe, Germany . August 1983 . dead . https://web.archive.org/web/20140806200328/http://ijcai.org/Past%20Proceedings/IJCAI-83-VOL-2/PDF/048.pdf . 2014-08-06 .
  22. Bjørn . Reese . AlphA*: An ε-admissible heuristic search algorithm . 1999 . Institute for Production Technology, University of Southern Denmark . 2014-11-05 . https://web.archive.org/web/20160131214618/http://home1.stofanet.dk/breese/astaralpha-submitted.pdf.gz . 2016-01-31 . dead .
  23. 3 . 103.
  24. Book: Pearl , Judea . Heuristics: Intelligent Search Strategies for Computer Problem Solving . Addison-Wesley . 1984 . 978-0-201-05594-8 .
  25. 2 . 97–104.
  26. A* parsing: fast exact Viterbi parse selection. Klein. Dan. Manning. Christopher D.. Proceedings of the 2003 Human Language Technology Conference of the North American Chapter of the Association for Computational Linguistics. 119–126. 10.3115/1073445.1073461. 2003.
  27. A Group-Testing Algorithm with Online Informational Learning . Kagan E. . Ben-Gal I. . IIE Transactions . 46 . 2 . 164–184 . 2014 . 10.1080/0740817X.2013.803639 . 18588494 . 2016-02-12 . 2016-11-05 . https://web.archive.org/web/20161105103321/http://www.eng.tau.ac.il/~bengal/GTA.pdf . dead.
  28. Dave . Ferguson . Maxim . Likhachev . Anthony . Stentz . A Guide to Heuristic-based Path Planning . 2005 . https://web.archive.org/web/20160629214339/https://www.cs.cmu.edu/afs/cs.cmu.edu/Web/People/maxim/files/hsplanguide_icaps05ws.pdf . 2016-06-29 . live . Proceedings of the international workshop on planning under uncertainty for autonomous systems, international conference on automated planning and scheduling (ICAPS) . 9–18.
  29. Nau . Dana S. . Vipin . Kumar . Laveen . Kanal . General branch and bound, and its relation to A∗ and AO∗ . Artificial Intelligence . 23 . 1 . 1984 . 29–58 . https://web.archive.org/web/20121004051632/http://www.cs.umd.edu/~nau/papers/nau1984general.pdf . 2012-10-04 . live . 10.1016/0004-3702(84)90004-3.
  30. Web site: Variants of A* . 2023-06-09 . theory.stanford.edu.
  31. Hansen . Eric A. . Rong . Zhou . Anytime Heuristic Search . Journal of Artificial Intelligence Research. 28 . 267–297 . 2007 . 10.1613/jair.2096. 9832874 . free . 1110.2737 .
  32. Fareh. Raouf. Baziyad. Mohammed. Rahman. Mohammad H.. Rabie. Tamer. Bettayeb. Maamar. 2019-05-14. Investigating Reduced Path Planning Strategy for Differential Wheeled Mobile Robot. Robotica. en. 38. 2. 235–255. 10.1017/S0263574719000572. 181849209. 0263-5747.
  33. Pijls . Wim . Post . Henk . Yet another bidirectional algorithm for shortest paths . https://web.archive.org/web/20140611141858/http://repub.eur.nl/pub/16100/ei2009-10.pdf . 2014-06-11 . live . EI 2009-10 . Econometric Institute, Erasmus University Rotterdam.
  34. Web site: Goldberg . Andrew V. . Harrelson . Chris . Kaplan . Haim . Werneck . Renato F. . Efficient Point-to-Point Shortest Path Algorithms . . https://web.archive.org/web/20220518121847/https://www.cs.princeton.edu/courses/archive/spr06/cos423/Handouts/EPP%20shortest%20path%20algorithms.pdf . 18 May 2022 . live .