top of page

ALL POSTS:

A* path finding algorithm

  • Lucy Emmett
  • Jul 1, 2018
  • 1 min read

Updated: Jul 2, 2018

This is another algorithm in python that I wrote from watching a Coding Train video! I really would recommend watching The Coding Train channel as it has a ton of playlists on different projects and challenges he has done.


The links to the three videos on The Coding Train:

https://www.youtube.com/watch?v=aKYlikFAV4k - creates the A* algorithm in a grid with no diagonals.

https://www.youtube.com/watch?v=EaZxUCWAjb0 - adds diagonals and walls

https://www.youtube.com/watch?v=jwRT4PCT6RU - makes the path into a line and changes walls to circles so that the path looks find going to through a diagonal line of walls


The A star algorithm is an algorithm that given a graph of nodes and a start and a finish will give you the shortest path from A to B. The main point of interest in the algorithm for me was this formula:

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

h = heuristic, a guess of the distance from this point to the end.

g = the distance to the current point from the start.

f = how far it would be if you chose this spot (low = good, high = bad)


Every node is given a f score once "discovered", which allows the algorithm to skip trying a lot of nodes that would likely make an long path. It basically makes a queue that allows it to only try the best candidates to make a good path. If a node made you go further away from the end, the algorithm is unlikely to pick it because it will have a higher f score.


Here is the code after watching the three videos, if you want the code I wrote after just the first video ask me in the comments



Comments


bottom of page