Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand The A* Algorithm quickly and effectively.
211+ students studying
A* is a graph traversal and pathfinding algorithm widely used in computer science for finding the shortest path from a source node to a target node. It is an enhancement of Dijkstra's algorithm that incorporates heuristics to optimise the search process, making it more efficient in certain scenarios.
Inputs:
Outputs:
g(n): The cost from the start node to the current node.
h(n): A heuristic estimate of the cost from the current node to the target node.
f(n) = g(n) + h(n): The total estimated cost of the cheapest path through the current node. Steps:
The heuristic h(n) plays a critical role in guiding the algorithm:
Manhattan Distance: Used in grids where movement is restricted to horizontal and vertical.
Euclidean Distance: Used when diagonal movement is allowed.
The heuristic must be admissible (never overestimates the actual cost) and ideally consistent for optimal performance.
Given a graph:
Nodes: A, B, C, D, E
Edges with weights:
A → B: 1
A → C: 4
B → C: 2
B → D: 6
C → D: 3
C → E: 5
D → E: 1 Heuristic (h) values (to target E):
A: 7, B: 6, C: 2, D: 1, E: 0 Step-by-Step Execution:
METHOD AStar(graph, start, goal)
openSet ← PRIORITY_QUEUE containing start
cameFrom ← EMPTY_MAP
gScore ← MAP with default value ∞
gScore[start] ← 0
fScore ← MAP with default value ∞
fScore[start] ← heuristic(start, goal)
WHILE openSet IS NOT EMPTY
current ← node in openSet with lowest fScore
IF current = goal
RETURN ReconstructPath(cameFrom, current)
REMOVE current FROM openSet
FOR each neighbor of current
tentative_gScore ← gScore[current] + distance(current, neighbor)
IF tentative_gScore < gScore[neighbor]
cameFrom[neighbor] ← current
gScore[neighbor] ← tentative_gScore
fScore[neighbor] ← gScore[neighbor] + heuristic(neighbor, goal)
IF neighbor NOT IN openSet
ADD neighbor TO openSet
RETURN "No Path Found"
import heapq
def a_star(graph, start, goal, heuristic):
open_set = []
heapq.heappush(open_set, (0, start))
came_from = {}
g_score = {node: float('inf') for node in graph}
g_score[start] = 0
f_score = {node: float('inf') for node in graph}
f_score[start] = heuristic[start]
while open_set:
_, current = heapq.heappop(open_set)
if current == goal:
return reconstruct_path(came_from, current)
for neighbor, weight in graph[current]:
tentative_g_score = g_score[current] + weight
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = g_score[neighbor] + heuristic[neighbor]
heapq.heappush(open_set, (f_score[neighbor], neighbor))
return "No Path Found"
def reconstruct_path(came_from, current):
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.reverse()
return path
Given a grid:
Start: S, Goal: G
Step 1: Initialise.
Step 2: Expand S, and calculate costs for neighbours.
Step 3: Expand the next lowest f(n)
Repeat until goal G is reached.
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
120 flashcards
Flashcards on The A* Algorithm
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards12 quizzes
Quizzes on The A* Algorithm
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on The A* Algorithm
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on The A* Algorithm
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on The A* Algorithm
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to The A* Algorithm to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered