Your favourite algorithm and the lesson it taught you
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
One of my favorite algorithms, which has a profound impact on both my understanding and appreciation of computer science, is the A* (A-star) search algorithm. A* is widely recognized in the domain of pathfinding and graph traversal, typically used in spatial navigation problems, such as those found in video games, robotics, and logistics. The lesson A* taught me is the power of heuristics in optimizing search processes, which fundamentally altered my approach to solving complex problems.
A* Search Algorithm: An Overview
The A* algorithm combines features of Dijkstra's Algorithm and Greedy Best-First Search. By doing so, it balances between exploring the shortest known paths and prioritizing paths that seem most promising. The elegance of A* lies in its use of heuristics, which enables it to efficiently find the least cost path between an initial node and a target node.
The Algorithm in Detail
A* uses a scoring function, , for each node , defined as:
• : The cost from the start node to node . • : The heuristic estimate of the cost from node to the goal.
The algorithm proceeds as follows:
- Initialize the open list and closed list: • Open list contains nodes to be evaluated (starting with the initial node). • Closed list contains nodes already evaluated.
- While the open list is not empty: • Select the node from the open list with the lowest . • If is the goal node, reconstruct and return the path. • Move to the closed list. • For each successor of : • If is already in the closed list, skip it. • Calculate tentative . • If is not in the open list, add it. • If is better than previously recorded, update and parent pointer.
The efficiency of A* depends on a well-chosen heuristic function . When is admissible (never overestimates the actual cost to reach the goal), A* is guaranteed to find the optimal path.
Example: Pathfinding on a Grid
Consider a 5x5 grid where we need to navigate from the top-left corner (start) to the bottom-right corner (goal). Some cells have obstacles that we must avoid. The algorithm makes use of the Manhattan Distance as its heuristic:
Here's a simple illustration:
• S: Start • G: Goal • X: Obstacle • Efficiency: Proper heuristic choice greatly reduces exploration, minimizing computation time. • Versatility: By tweaking the heuristic, you can adapt A* for different types of problems. • Optimality: With admissible heuristics, you ensure optimal solutions. • Video Games: A* is popular in AI for NPC pathfinding, offering real-time route calculations. • Robotics: Allows robots to efficiently navigate through physical environments. • GIS and Mapping: Used in route optimization, being essential for apps like GPS navigation systems.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.