How A* Works
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
| Term | Meaning |
|---|---|
| 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 point | Notes |
|---|---|
| Manhattan distance | Useful for grid movement |
| Euclidean distance | Straight-line estimate |
| A* vs Dijkstra | A* 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.
| Edge | A–B | A–C | B–D | C–D | C–E | D–F | E–F |
|---|---|---|---|---|---|---|---|
| Weight | 3 | 4 | 6 | 2 | 5 | 4 | 7 |
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.
| Node | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| h | 8 | 9 | 5 | 3 | 6 | 0 |
For each neighbour, g is the distance travelled from A and f = g + h.
| Node visited | Updates 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 |
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.
• 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