Skip to main content

Section 3.4 Interactive A* Pathfinding ActiveCode

Now let’s bring all these concepts together: grid representation, Manhattan distance heuristics, and tracking path costs \(g(n)\) and \(f(n)\text{.}\)
The A* Search Processing Flow repeats a two-step cycle: first, pull the node with the lowest \(f(n)\) score from the open list. If that node is the goal, the path is reconstructed and the search stops. Otherwise, the search evaluates all of that node’s 4-connected neighbors, computing \(g(\text{neighbor}) = g(\text{current}) + 1\) and \(f(\text{neighbor}) = g + h\) for each one, and the cycle repeats by pulling the next lowest-\(f(n)\) node.
Figure 3.4.1. The A* core loop selects the lowest-cost node, checks for the goal, and otherwise evaluates neighboring cells before continuing the search.

Subsection 3.4.1 Section 3.4 Interactive Exercises

Subsubsection 3.4.1.1 Exercise 3.4.1: A* Pathfinding Completion Challenge

Below is a lightweight Python implementation of A* pathfinding on a 2D matrix grid.
Task:
  1. Complete the heuristic function to return the Manhattan distance between cell and goal.
  2. Complete the \(f(n)\) cost score update line: f_score = tentative_g + h_score.
  3. Run the code to trace the shortest path found around the obstacle wall!
You have attempted of activities on this page.