76

I read this: http://en.wikipedia.org/wiki/A*_search_algorithm

It says A* is faster than using dijkstra and uses best-first-search to speed things up.

If I need the algorithm to run in milliseconds, when does A* become the most prominent choice.

From what I understand it does not necessarily return the best results.

If I need quick results, is it better to pre-compute the paths? It may take megabytes of space to store them.

Ginés Hidalgo
  • 717
  • 9
  • 20
AturSams
  • 7,568
  • 18
  • 64
  • 98
  • What is your problemsize? My first bet is that if you can count in milliseconds, then you don't need to worry and can use Dijstra. – Zane Oct 23 '12 at 13:28
  • 11
    Donald Knuth explains it well here http://xkcd.com/342/ – TtT23 Mar 19 '13 at 06:09
  • Another good explanation: https://www.cs.princeton.edu/courses/archive/spr06/cos423/Handouts/EPP%20shortest%20path%20algorithms.pdf – Fedor Nov 11 '22 at 19:29

5 Answers5

106

It says A* is faster than using dijkstra and uses best-first-search to speed things up.

A* is basically an informed variation of Dijkstra.
A* is considered a "best first search" because it greedily chooses which vertex to explore next, according to the value of f(v) [f(v) = h(v) + g(v)] - where h is the heuristic and g is the cost so far.

Note that if you use a non informative heuristic function: h(v) = 0 for each v: you get that A* chooses which vertex to develop next according to the "so far cost" (g(v)) only, same as Dijkstra's algorithm - so if h(v) = 0, A* defaults to Dijkstra's Algorithm.

If I need the algorithm to run in milliseconds, when does A* become the most prominent choice.

Not quite, it depends on a lot of things. If you have a decent heuristic function - from my personal experience, greedy best first (choosing according to the heuristic function alone) - is usually significantly faster than A* (but is not even near optimal).

From what I understand it does not necessarily return the best results.

A* is both complete (finds a path if one exists) and optimal (always finds the shortest path) if you use an Admissible heuristic function. If your function is not admissible - all bets are off.

If I need quick results, is it better to pre-compute the paths? It may take megabytes of space to store them.

This is a common optimization done on some problems, for example on the 15-puzzle problem, but it is more advanced. A path from point A to point B is called a Macro. Some paths are very useful and should be remembered. A Machine Learning component is added to the algorithm in order to speed things up by remembering these Macros.

Note that the path from point A to point B in here is usually not on the states graph - but in the problem itself (for example, how to move a square from the lowest line to the upper line...)

To speed things up:
If you have a heuristic and you find it too slow, and you want a quicker solution, even if not optimal - A* Epsilon is usually faster then A*, while giving you a bound on the optimality of the path (how close it is to being optimal).

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
amit
  • 175,853
  • 27
  • 231
  • 333
27

Dijkstra is a special case for A* (when the heuristics is zero).

A* search:

It has two cost function.

g(n): same as Dijkstra. The real cost to reach a node n.
h(n): approximate cost from node n to goal node. It is a heuristic function. This heuristic function should never overestimate the cost. That means, the real cost to reach goal node from node n should be greater than or equal h(n). It is called admissible heuristic.

The total cost of each node is calculated by f(n)=g(n)+h(n)

Dijkstra's:

It has one cost function, which is real cost value from source to each node: f(n)=g(n) It finds the shortest path from source to every other node by considering only real cost.

17

A* is just like Dijkstra, the only difference is that A* tries to look for a better path by using a heuristic function which gives priority to nodes that are supposed to be better than others while Dijkstra's just explore all possible paths.

Its optimality depends on the heuristic function used, so yes it can return a non optimal result because of this and at the same time better the heuristic for your specific layout, and better will be the results (and possibly the speed).

It is meant to be faster than Dijkstra even if it requires more memory and more operations per node since it explores a lot less nodes and the gain is good in any case.

Precomputing the paths could be the only way if you need realtime results and the graph is quite large, but usually you wish to pathfind the route less frequently (I'm assuming you want to calculate it often).

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 7
    Dijkstra doesn't "just explore all possible paths" (there's an exponential number of paths). It simply explores the graph in a different (and less optimized) order. – leo Oct 23 '12 at 14:58
  • 2
    All possible, until reaching the goal. That was implied. Why should an algorithm just keeping exploring nodes after goal has been reached? (unless just going randomly) – Jack Oct 23 '12 at 15:04
6

These algorithems can be used in pathfinding and graph traversal, the process of plotting an efficiently directed path between multiple points, called nodes.

Formula for a* is f =g + h., g means actual cost and h means heuristic cost. formula for Dijktras is f = g. there is no heuristic cost. when we are using a* and if heuristic cost is 0 then it'll equal to Dijktras algorithem.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • And the heuristic cost could be for instance the distance function if the graph is embedded in a metric space. If not that, it should always be a lower bound for the A* to produce the correct result. – AturSams Apr 21 '17 at 06:02
4

Short answer: A* uses heuristics to optimize the search. That is, you are able to define a function that to some degree can estimate the cost from one node to the target. This is particulary useful when you are searching for a path on a geographical representation (map) where you can, for instance, guess the distance to the target from a given graph node. Hence, typically A* is used for path finding in games etc. Where Djikstra is used in more generic cases.

No, A* won't always give the best path. If heuristic is the "geographical" distance, the following example might give the non optimal path.

[airport] - [road] - [start] -> [road] -> [road] -> [road] -> [road] -> [target] - [airport]
        |----------------------------------------------------------------|
Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
  • 9
    You're wrong, A* has be proven to return the optimal path if one exist, under these two conditions: 1) The heuristic is admissible, i.e. the estimated cost is always lower than the real cost. 2) Enough memory is available to find the solution and represent it. The 2nd requirement is a major one, and when graphs/trees become too large, it may be impossible to solve the problem using A*. You can then use IDA* or SMA* which are slower but can be used no matter the memory available. – francoisr Oct 02 '14 at 12:18
  • 1
    Yes, but as I said - it won't give the optimal path if the heurestic model wrong. – Petter Nordlander Nov 07 '17 at 13:09
  • Maybe it's just me, but I find your example for non-optimal path unclear: (1) what do you mean by the "geographical" distance (I suppose what wikipedia defines as it, i.e. the distance along the earth's surface, right?) but under what circumstances would the cost estimated given by it then be _higher_ than the actual cost? only if you could somehow tunnel through earth I suppose? (2) what is the "graph" example formatted as code supposed to display exactly? – codeling Dec 17 '21 at 10:56
  • @codeling very very old answer. But I probably explained in a bad way that your heuristic must be the same unit as your definition of optimal. Use distance as heuristic to solve for shortest path and time for heuristic to solve for fastest path to make A* guarantee the best optimal path. – Petter Nordlander Dec 17 '21 at 11:54