Skip to content
Questions
Definition
A* (A-Star) is a pathfinding algorithm that improves on Dijkstra's by using heuristics to prioritise paths that appear closer to the goal. Widely used in games and AI.

How A* Works

pseudocode
function aStar(graph, start, goal):
    openSet = [start]      // Nodes to evaluate
    closedSet = []         // Already evaluated
    
    gScore[start] = 0      // Cost from start
    fScore[start] = heuristic(start, goal)  // Estimated total cost
    
    while openSet is not empty:
        current = node in openSet with lowest fScore
        
        if current == goal then
            return reconstructPath(current)
        end if
        
        move current from openSet to closedSet
        
        for each neighbour of current:
            if neighbour in closedSet then
                continue
            end if
            
            tentativeG = gScore[current] + dist(current, neighbour)
            
            if neighbour not in openSet then
                add neighbour to openSet
            elseif tentativeG >= gScore[neighbour] then
                continue  // Not a better path
            end if
            
            // Best path found
            cameFrom[neighbour] = current
            gScore[neighbour] = tentativeG
            fScore[neighbour] = gScore[neighbour] + heuristic(neighbour, goal)
        end for
    end while
    
    return FAILURE  // No path found
end function

A* Formula

Key Point
f(n) = g(n) + h(n)
TermMeaning
g(n)Actual cost from start node to n
h(n)Heuristic estimate from n to goal
f(n)Estimated total path cost through n
Heuristic/comparison pointNotes
Manhattan distanceUseful for grid movement
Euclidean distanceStraight-line estimate
A* vs DijkstraA* usually explores fewer nodes; with admissible heuristic it still finds optimal path

Worked Example: Tracing the A* Algorithm

Find the shortest path from A to F in a graph that has these edges.

EdgeA–BA–CB–DC–DC–ED–FE–F
Weight3462547

Each node also has a heuristic value, h: an estimate of its distance from the goal, F. The estimate must never be greater than the true distance, or the path found may not be the shortest.

NodeABCDEF
h895360
Worked Example
Working: the node with the lowest f is visited next

For each neighbour, g is the distance travelled from A and f = g + h.
Node visitedUpdates to its neighbours
A (f = 0 + 8 = 8)B: g = 3, f = 3 + 9 = 12 (from A); C: g = 4, f = 4 + 5 = 9 (from A)
C (f = 9)D: g = 6, f = 6 + 3 = 9 (from C); E: g = 9, f = 9 + 6 = 15 (from C)
D (f = 9)F: g = 10, f = 10 + 0 = 10 (from D); B through D would have g = 12, which is not less than 3, so B is unchanged
F (f = 10)Goal reached, so the search stops
Shortest path: A, C, D, F. Distance: 10.

B (f = 12) and E (f = 15) are never visited, because another node always has a lower f. Dijkstra's algorithm would visit all six nodes of this graph to find the same path.
Exam Tip
Exam Tips for 2.3.1 Algorithms:
• Know Big O notation for time and space complexity
• Be able to trace linear and binary search step by step
• Understand binary search requires sorted data
• Know how all four sorting algorithms work (bubble, insertion, merge, quick)
• Be able to trace sorting algorithms with examples
• Know Big O complexities: bubble/insertion O(n²), merge/quick O(n log n)
• Understand Dijkstra's algorithm for shortest path
• Know A* formula: f(n) = g(n) + h(n)
• Compare algorithms using Big O notation
• Know when to use each algorithm based on data characteristics