20

I'm developing a path finding program. It is said theoretically that A* is better than Dijkstra. In fact, the latter is a special case of the former. However, when testing in the real world, I begin to doubt that is A* really better?

I used data of New York City, from 9th DIMACS Implementation Challenge - Shortest Paths, in which each node's latitude and longitude is given.

When applying A*, I need to calculate the spherical distance between two points, using Haversine Formula, which involves sin, cos, arcsin, square root. All of those are very very time-consuming.

The result is,

Using Dijkstra: 39.953 ms, expanded 256540 nodes.

Using A*, 108.475 ms, expanded 255135 nodes.

Noticing that in A*, we expanded less 1405 nodes. However, the time to compute a heuristic is much more than that saved.

To my understanding, the reason is that in a very large real graph, the weight of the heuristic will be very small, and the effect of it can be ignored, while the computing time is dominating.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HanXu
  • 5,507
  • 6
  • 52
  • 76
  • 2
    `It is said theoretically that A* is better than Dijkstra` -- Citation, please. Also, by "better," you really mean faster, right? Do you mean faster as in Big O faster? – Robert Harvey Apr 15 '13 at 16:36
  • 3
    @RobertHarvey, take A* and give the heuristic as `h(node) = 1 and h(goal) = 0`. A* is then reduced to dijkstra. So since A* can emulate dijkstra, it's either equally powerful or better. – Shahbaz Apr 15 '13 at 16:46
  • 4
    I've implemented A* and Dijkstra for real world road networks in Java (see graphhopper). And A* is nearly 2 times faster. Where as bidirectional A* to bidir Dijkstra did not gave me such a boost – Karussell Apr 16 '13 at 12:58
  • The performance of an algorithm greatly depends on its implementation – Bill May 04 '13 at 23:08
  • Just a comment: Have you considered Jump Point Search? It would be interesting to see if practically performs as well as it does theoretically. See [here](http://harablog.wordpress.com/2011/09/07/jump-point-search/) and [here](http://qiao.github.io/PathFinding.js/visual/) – dialer May 05 '13 at 09:09
  • 3
    @HanXu A* is "only" better than plain Dijkstra, if only a fraction of the nodes must be examined. That's the whole purpose of the heuristic. But your numbers indicate, that you examine roughly the same number of nodes with both algorithms. That sounds bogus to me. – A.H. May 09 '13 at 16:16

5 Answers5

36

I think you're more or less missing the point of A*. It is intended to be a very performant algorithm, partially by intentionally doing more work but with cheap heuristics, and you're kind of tearing that to bits when burdening it with a heavy extremely accurate prediction heuristic.

For node selection in A* you should just use an approximation of distance. Simply using (latdiff^2)+(lngdiff^2) as the approximated distance should make your algorithm much more performant than Dijkstra, and not give much worse results in any real world scenario. Actually the results should even be exactly the same if you do calculate the travelled distance on a selected node properly with the Haversine. Just use a cheap algorithm for selecting potential next traversals.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • 4
    Further to this, all that A* requires of a heuristic is that it never overestimate the distance. So, for the heuristic it would be perfectly [admissible](http://en.wikipedia.org/wiki/Admissible_heuristic) to use the euclidean distance. – Alan Apr 15 '13 at 16:43
  • 5
    As a minor addition to my own answer: note that I don't even suggest taking the square root of the approximated distance - it's a needless burdening of the CPU since all you need is relative distance, not absolute, so for comparison it's just as usable. – Niels Keurentjes Apr 15 '13 at 16:46
  • 3
    @Niels: In order for the A* heuristic to be admissible, it must never overestimate the distance to the goal. Your proposed heuristic (dx^x+dy^2) often *will* overestimate the distance, unless you take the square root -- so you're no longer guaranteed to find the best solution if you use that function as your heuristic. – Edward Loper May 13 '13 at 20:12
11

A* can be reduced to Dijkstra by setting some trivial parameters. There are three possible ways in which it does not improve on Dijkstra:

  1. The heuristic used is incorrect: it is not a so-called admissible heuristic. A* should use a heuristic which does not overestimate the distance to the goal as part of its cost function.
  2. The heuristic is too expensive to calculate.
  3. The real-world graph structure is special.

In the case of the latter you should try to build on existing research, e.g. "Highway Dimension, Shortest Paths, and Provably Efficient Algorithms" by Abraham et al.

Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39
9

Like everything else in the universe, there's a trade-off. You can take dijkstra's algorithm to precisely calculate the heuristic, but that would defeat the purpose wouldn't it?

A* is a great algorithm in that it makes you lean towards the goal having a general idea of which direction to expand first. That said, you should keep the heuristic as simple as possible because all you need is a general direction.

In fact, more precise geometric calculations that are not based on the actual data do not necessarily give you a better direction. As long as they are not based on the data, all those heuristics give you just a direction which are all equally (in)correct.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
4

In general A* is more performant than Dijkstra's but it really depends the heuristic function you use in A*. You'll want an h(n) that's optimistic and finds the lowest cost path, h(n) should be less than the true cost. If h(n) >= cost, then you'll end up in a situation like the one you've described.

Could you post your heuristic function?

rhinoinrepose
  • 2,110
  • 2
  • 19
  • 26
3

Also bear in mind that the performance of these algorithms highly depends on the nature of the graph, which is closely related to the accuracy of the heuristic.

If you compare Dijkstra vs A* when navigating out of a labyrinth where each passage corresponds to a single graph edge, there is probably very little difference in performance. On the other hand, if the graph has many edges between "far-away" nodes, the difference can be quite dramatic. Think of a robot (or an AI-controlled computer game character) navigating in almost open terrain with a few obstacles.

My point is that, even though the New York dataset you used is definitely a good example of "real-world" graph, it is not representative of all real-world path finding problems.

oseiskar
  • 3,282
  • 2
  • 22
  • 22