29

Can we get people to post code of simple, optimized implementations of the A* pathfinding algorithm, in every single language?

This is mostly for fun and to play with what stackoverflow itself is capable of... although I actually am interested in getting an ActionScript 3 version of this.

But the idea is that this "Question" will continue to be updated eternally into the future, even as different programming languages are created!

I don't know of any other place online where you can see pseudocode "translated" into many (much less every) different language. Seems like it's a worthwhile resource, and while not necessarily what this site was designed for, there's no harm in trying it out and seeing if it turns out to be a worthwhile thing that stackoverflow could be used for!

Marcel Levy
  • 3,297
  • 1
  • 25
  • 39
IQpierce
  • 399
  • 2
  • 7
  • 17
  • Would probably be better if you gave more ground rules such as a "test" maze/obstacle field. Ideally each implementation would find the same route or a very common short-list of possible solutions! – Ray Hayes Sep 17 '08 at 16:35
  • @Ray Hayes, Maybe not the same route (as there can be multiple routes to the same goal), but the same length route. – strager Jan 27 '09 at 02:05
  • SPOJ and its spin-off products/sites feature a way to compile code from many different languages online. Maybe you can make a problem there and people can post testable and compilable code. – ziggystar Nov 07 '10 at 18:22
  • 1
    The terms "optimized code" and "simple code" are mutually exclusive. – ziggystar Nov 07 '10 at 18:22
  • 2
    If you're looking for "place online where you can see pseudocode "translated" into many (much less every) different language" you're probably better off with [Rosetta Code](http://rosettacode.org/wiki/Main_Page "Programming Chrestomathy"), a site actually made for that purpose. – JUST MY correct OPINION Feb 25 '11 at 15:10

11 Answers11

11

Here is a JavaScript implementation, along with source code and an online demo I did as a hobby/research project.

It is very simple, but you can change some of the params (grid size, # of walls, debugging info on/off). It will show you the calculated f(x), g(x), and h(x) values for each node that is inspected.

The demo page implementation uses jQuery.

Brian Grinstead
  • 3,394
  • 5
  • 26
  • 23
9

Here's a C++ implementation. It's fairly well tested by now, and used in commercial video games and various AI projects.

http://code.google.com/p/a-star-algorithm-implementation/

And there's a tutorial, which I actually wrote first:

http://www.heyes-jones.com/astar.html

justinhj
  • 10,416
  • 9
  • 52
  • 99
5

Here's a C# implementation done by one of the people who build the language.

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
3

Source codes and demos in different programming languages:

List of demo's for each language:

C++: 1
Java: 3
Processing: 1
Actionscript 3 (Flash): 4
Flex (Flash): 1
Javascript: 6
C#: 1
Ruby: 1
Prolog: 1
Unity: 1
Lua: 1

Pathfinding Demo in different languages

Enjoy :)

Sir
  • 7,735
  • 12
  • 72
  • 138
1

Not an implementation, but I found http://theory.stanford.edu/~amitp/GameProgramming/AStarComparison.html to be a particularly clear explanation of the algorithm. Has pseudocode that makes it very easy to implement, along with an extended review of various data structures that can be used for implementing the open & closed sets, a discussion of different heuristics that are applicable in different situations, modifications to heuristics to get specific behaviours (e.g. getting approximations of straight lines in systems that only support limited angles of movement), common pitfalls (e.g. using a heuristic with a different scale to the actual movement costs), and some optimizations (e.g. working with regions of uniform cost rather than a grid).

Jules
  • 13,417
  • 7
  • 75
  • 117
1

An AS 3 example... http://www.dauntless.be/astar/

Chris
  • 6,269
  • 8
  • 31
  • 48
1

I implemented A* in C as a way to learn C, so I can't promise it's beautiful, but it works! I used it to solve Project Euler #83 and it worked on two test cases.

https://github.com/PeterMitrano/A-star-Pathfinding/blob/master/problem_83.c

Peter Mitrano
  • 1,602
  • 23
  • 24
1

Python and C++ source code along with interactive tutorial. The code is written to work on graphs in general, and is not specific to grids (as you will find in many examples of A* on the web). It uses binary heaps for the priority queue (both Python and C++ have binary heaps in their standard libraries). I have Breadth First Search, Dijkstra's Algorithm, and A* on that page. The code is reasonably short (shorter than most A* sample code I find).

amitp
  • 1,095
  • 10
  • 9
1

A Clojure implementation, heavily based on an example given in PAIP.

Jeff Foster
  • 40,078
  • 10
  • 78
  • 103
0

An optimized Java implementation is available in GraphHopper.

Karussell
  • 16,303
  • 14
  • 88
  • 188
0

A VB6 implementation.

http://www.gandraxa.com/pathfinding_with_a_star.xml

This is particularly useful because you can step through the process and get a good understanding of how the algorithm works. This can be quite valuable when converting the algorithm to another language.

George Mastros
  • 22,662
  • 3
  • 47
  • 56