Saturday, February 28, 2015

Compare A* with Dijkstra algorithm

A* (A Star) algorithm, as I have heard, has been widely used in games for path search. As I am not really a game person, have not played that many games myself, so cannot really provide any reference here. And Dijkstra algorithm is also very well-known, the one example I know is Cisco route uses the algorithm for finding the shortest path between two networking nodes. There is tons of pieces of information (discussions/articles) that describes the details about these algorithms, but I am more a visual person, really need to see it with my own eyes. So I decided to implements the two algorithms myself, and visualize it to be able to measure the difference quantitatively. OK, here it's.

http://youtu.be/g024lzsknDo


The green cell is the departure, the red cell is the destination, while the path is in yellow. The A* algorithm uses heuristics, so is almost always faster than Dijkstra algorithm, however, the path it finds may not be the shortest/most-optimal one, which is a trade for the speed. Even though Dijkstra algorithm came earlier than A*, A* can be seen as a more general form of Dijkstra, a "Dijkstra with heuristics.

The project can be found on:

https://github.com/kevinwang1975/PathFinder

The project contains the Java implementation of the A* and Dijkstra path search algorithms, which can be used alone in any application. A GUI demo is provided for the visualization that animates the search progress, also shows the cost of the path and the number of nodes visited during the search, so the difference between the two algorithm could not be more clear.

To get the source code:

git clone https://github.com/kevinwang1975/PathFinder

No comments:

Post a Comment