algorithms
personal experience
lessons learned
programming
computer science

Your favourite algorithm and the lesson it taught you

Codemia Knowledge Hub ·

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, f(n)f(n), for each node nn, defined as:

f(n)=g(n)+h(n)f(n) = g(n) + h(n)

• g(n)g(n): The cost from the start node to node nn. • h(n)h(n): The heuristic estimate of the cost from node nn to the goal.

The algorithm proceeds as follows:

  1. 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.
  2. While the open list is not empty: • Select the node nn from the open list with the lowest f(n)f(n). • If nn is the goal node, reconstruct and return the path. • Move nn to the closed list. • For each successor ss of nn: • If ss is already in the closed list, skip it. • Calculate tentative g(s)=g(n)+cost(n,s)g(s) = g(n) + \text{cost}(n, s). • If ss is not in the open list, add it. • If g(s)g(s) is better than previously recorded, update g(s)g(s) and parent pointer.

The efficiency of A* depends on a well-chosen heuristic function h(n)h(n). When h(n)h(n) 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:

h(x1,y1,x2,y2)=∣x2−x1∣+∣y2−y1∣h(x_1, y_1, x_2, y_2) = |x_2 - x_1| + |y_2 - y_1|

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.


Course
Intermediate
27 lessons
15 hours
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 course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms