dhruv's space

Understanding the A star algorithm

I recently learnt about the workings of the A* algorithm through Udacity’s Intro to Self Driving Cars Nanodegree, and wanted to understand it in detail, hence this post on it.

Wikipedia article on A* states: “A star is a computer algorithm that is widely used in pathfinding and graph traversal, which is the process of finding a path between multiple points, called “nodes”. It enjoys widespread use due to its performance and accuracy.”

It was devised by Peter Hart, Nils Nilsson and Bertram Raphael of Stanford Research Institute (now SRI International) who first published the algorithm in 1968.

Setup

We have a map of interconnected nodes and the problem that we want to solve is to find the shortest path between two nodes.

Terminology

Let’s define some terminology that will help us understand the algorithm.

The A-star Algorithm

Let’s start understanding these concepts using the map shown above. Let’s use node 8 as start goal and node 2 as goal node.

A* uses a metric called f-score which is a score to gauge how fruitful moving to the neighbor will be in finding the optimal path. $$ f = g + h $$ where $f$ is the f-score, $g$ or g-score is the path cost of a node, and h is a heuristic used to estimate distance between a node and the goal. In this case, we’ll be using euclidian distance between two nodes as the heuristic.

Currently, the g-score of node 8 is zero while that of all others is set to infinity (as we don’t have that knowledge yet). As a result, the f-score of all nodes except 8 is infinity. f-score of 8 is just the value of $h$ for 8.

To make our lives easier we’ll divide up the state space into separate segments:

We’ll start with the frontier consisting just the start node. Until the frontier is empty or we reach the goal node, we’ll do the following:

This is how the map looks after one iteration of exploration. represents the frontier set.

This is how the map looks after second iteration. The explored set is indicated by and includes the nodes 8 and 33. (8 is highlighted as since it’s the start node).

This is how things look after the third iteration.

A-star summed up

Demo

Below is a video which shows the implementation of A* for finding shortest route between nodes 8 and 2.

represents the start node.
represents the goal node.
represents nodes in the frontier set.
represents nodes in the explored set.
represents current node, ie, the node with the lowest f-score.
represents neighbors of the current node being considered for exploration.


Shortest route between nodes 20 and 22:


Shortest route between nodes 38 and 21:

Code for the implementation can be found here.

References

#Computer-Science