# Module 3.2: Grid Search and Graph Planning

- Course: [Course 3: Motion Planning & Navigation](https://roboticsconcepts.dev/courses/motion-planning-navigation.md)
- HTML Page: https://roboticsconcepts.dev/courses/motion-planning-navigation/grid-search-and-graph-planning/
- Key Concepts: Occupancy grids & costmaps, BFS, Dijkstra & A* heuristics, Admissibility & search frontier

Once you have a clean 2D map — either a floorplan for a wheeled robot or a 2-joint C-space grid — the next question is simple: how does software actually find a path from Start to Goal without wandering around forever?

## Turning a continuous floor into a checkerboard

Computers prefer discrete steps over infinite continuous rooms, so mobile robots slice the floor into a grid of small squares called an occupancy grid. Each cell holds a belief: open floor, solid wall, or unknown territory.

In real warehouse and home robots, you rarely want to hug walls at zero clearance. So we layer a costmap on top of the occupancy grid — inflating obstacles slightly and adding a soft penalty to cells right next to furniture so the planner naturally stays near the middle of hallways.

## BFS, Dijkstra, and why A* feels so much smarter

With a grid in hand, three classic graph search algorithms show up everywhere:

- Breadth-First Search (BFS): expands outward in even rings like ripples in a pond. It finds the fewest-step path on an unweighted grid, but ignores movement costs and looks equally hard in the wrong direction.
- Dijkstra's Algorithm: expands the lowest total cost-so-far first. It handles variable terrain or wall-proximity costs cleanly, though it still searches blindly in every direction around the start.
- A* Search: keeps Dijkstra's exact cost-so-far g(n), then adds a guess h(n) — usually straight-line or Manhattan distance — for how far the goal still is. Sorting by f(n) = g(n) + h(n) pulls the search cone straight toward the goal.

## Interactive Sandbox: BFS, Dijkstra, A*, and RRT Pathfinding Visualizer

Switch between BFS, Dijkstra, and A* on the U-Trap and Narrow Gap maps below, or click cells to build your own maze. Watch how many squares each algorithm has to visit before reaching the goal.

## Why the heuristic cannot lie too much

You might wonder: if adding a heuristic speeds things up, why not multiply h(n) by 10 to race straight for the goal? You can, and weighted A* often runs much faster — but you give up optimality.

A* only guarantees the true shortest path when its heuristic h(n) never overestimates the real remaining cost (called an admissible heuristic). Straight-line Euclidean distance never overestimates because you can never travel shorter than a crow flies. Once h(n) stays optimistic, A* explores far fewer cells than Dijkstra while still finding the exact optimal path.

## The A* priority score

```text
f(n) = g(n) + h(n)   with   0 ≤ h(n) ≤ h*(n)
```

g(n) is the known cost paid from start to cell n, and h(n) is an admissible estimate of the remaining cost to the goal. Keeping h(n) ≤ true remaining cost h*(n) guarantees A* returns an optimal path.

## Check Your Intuition

**Question:** On a U-shaped cul-de-sac pointing toward the goal, what happens to standard A* compared to a naive greedy search that only follows h(n)?

- [ ] A* gets permanently stuck inside the U-trap
- [✓] A* hits the inside of the U, sees g(n) rising while paths around the wings stay competitive, and routes cleanly around the outside
- [ ] A* teleports over the wall using the heuristic

**Explanation:** Because A* balances g(n) + h(n), dead-end cells inside the trap accumulate high g(n) without escaping, so the priority queue naturally backtracks and expands around the outer rim.

## Where You'll See This in Modern Robotics

Grid planners like Nav2's Smac Planner (Hybrid A*) still power most autonomous mobile robots in warehouses and hospitals, extending 2D A* with heading angle θ so cars and forklifts don't try to slide sideways.

## Companion Python Notebook: 02 · Grid Search, Dijkstra & A* Heuristics Lab

Implement BFS, Dijkstra, and A* on 2D occupancy grids with obstacle costmaps and compare expanded node counts side-by-side.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-3/02_grid_search_astar.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-3/02_grid_search_astar.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-3/02_grid_search_astar.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-3/02_grid_search_astar.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-3/02_grid_search_astar.py`
