Atlas Robotics
Navigation BasicsIntermediate

What is path planning? How Dijkstra and A* search a grid

Atlasbot · 17/8/2026 · 12 min read

What is path planning? How Dijkstra and A* search a grid
Dijkstra equidistant in all directions, A* preferred towards the destination

The article What is navigation? puts Path Planning as the "which path from here to destination" step. This is a classic computer science problem, long before robotics — that treats the map (the Occupancy Grid problem) as a graph, each empty cell as a vertex, adjacent cells connected by edges.

Key concepts

The two most basic algorithms for the problem of finding the shortest path on a graph:

  • Dijkstra— expands equally in all directions, regardless of the destination direction
  • A*— Dijkstra plus "heuristic" towards the target

The result of both is only a static path, point sequence without time information (mentioned in Trajectory, Robot Control column) — making this path into real-world motion over time is the business of Path Following (next article).

How it works

From the starting point, Dijkstra gradually explored adjacent cells in ascending order of costs — always dealing with the next lowest total cost cell, regardless of whether or not the cell was near the target:

1. Set the starting cell's cost to 0 and every other cell's cost to infinity
2. Repeat: choose the UNPROCESSED cell with the lowest cost
3. Update neighboring cells' costs if going through the current cell is cheaper
4. Stop when the goal cell has been processed

Ensuring the finding of the absolute shortest path — but wasting time exploring regions unrelated to the destination, as there is no notion of "go-to".

A* (pronounced "A star") adds a heuristic, an estimate of the remaining distance to the goal (usually straight-line distance), to the formula for selecting the next cell to process. The cell with the smallest f(cell) is processed first — this biases exploration toward the goal rather than expanding uniformly in every direction like Dijkstra, finding a path much faster in most practical cases.

Finally: Dijkstra = A* with heuristic always equal to zero (no "sensor" of destination direction) — Essentially, A* is the more general version, Dijkstra is only a special case of it.

Technical details

Formulas

f(cell) = g(cell) + h(cell)
  g(cell) = actual cost traveled from the start to this cell
  h(cell) = estimated remaining cost from this cell to the goal (heuristic)

A* still ensures the shortest path if the heuristic never overestimates the real-world (called heuristic "admissible") — line distance to the destination is always a valid heuristic admissible on the grid, since no real-world path can be shorter than the line.

Example: the current cell is 10 cells from the goal along x and 5 along y
h(cell) = √(10² + 5²) ≈ 11.18 (straight-line distance, admissible)

Compare: h(cell) = 10 + 5 = 15 (Manhattan distance — admissible only
if the robot moves horizontally/vertically, without diagonal movement)

Parameters

DijkstraA*
HeuristicNoneYes (estimated distance to destination)
speedSlower (equal exploration in all directions)Faster (goal preference)
Optimal assuranceYesYes (if heuristic admissible)
Used whenPreferably on an absolutely optimal path, regardless of the speed.Need to balance speed and quality — more common in real-world

Practical applications

In real-world Nav2, popular planners (NavFn, SmacPlanner — or Planner in Nav2) are all based on this A*/Dijkstra platform, differing only in how to optimize computational speed and the resulting path quality — e.g. SmacPlanner additionally supports kinematics constraints (robot cannot rotate in situ like Ackermann) that are not taken into account by pure grid Dijkstra/A*. With AMR in the warehouse, selecting the appropriate heuristic (Euclidean for robot moving freely in all directions, or heuristic taking into account the kinematics constraint for car-type robot) directly affects the quality of the generated real-world path, not just the computational speed.

Practical example

import heapq

def a_star(grid, start, goal):
    def heuristic(a, b):
        return ((a[0]-b[0])**2 + (a[1]-b[1])**2) ** 0.5

    open_set = [(0, start)]
    g_score = {start: 0}
    came_from = {}

    while open_set:
        _, current = heapq.heappop(open_set)
        if current == goal:
            path = [current]
            while current in came_from:
                current = came_from[current]
                path.append(current)
            return path[::-1]

        for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
            neighbor = (current[0]+dx, current[1]+dy)
            if neighbor not in grid or grid[neighbor] == 1:  # 1 = occupied
                continue
            tentative_g = g_score[current] + 1
            if neighbor not in g_score or tentative_g < g_score[neighbor]:
                g_score[neighbor] = tentative_g
                f = tentative_g + heuristic(neighbor, goal)
                heapq.heappush(open_set, (f, neighbor))
                came_from[neighbor] = current
    return None
For the 5x5 free grid, start = (0,0), goal = (4,4):
Output path: [(0,0), (1,1), (2,2), (3,3), (4,4)]

The above code is a minimalist A* on-grid without obstacles — Nav2 real-world code which is much more complex (including costmap, kinematics constraint) but the core principle (preferential expansion under f(cell) = g(cell) + h(cell)) is completely the same.

Common mistakes

  • Using the non-acceptably heuristic— breaks the shortest path-finding guarantee of A*, which can return the non-optimal path without explicit error warnings.
  • Using the Manhattan distance for diagonally walkable— robots as in the Formula, the Manhattan distance is no longer admissible when the robot can walk diagonally, requiring Euclidean distances instead.
  • Morbidity of Path Planning (Run once with a new goal) with Path Following (Run continuously) — falsely expects that the planner will automatically dodge dynamic obstacles that appear in the middle of the path, whereas it is the task of Path Following/local costmap.
  • Non-calculating costmap when expanding the neighborhood cell— A*/Dijkstra pure on grid free/occupied 2 states ignores the safety buffer (inflation, learned in Navigation Layer in AMR) around the obstacle, leading to paths closer to the obstacle than the desired safety.

Conclusion

Dijkstra and A* are the mathematical underpinnings of every pathfinder in robotics — A* is just that Dijkstra is equipped with an additional "sensor" of destination direction via heuristics, in exchange for a much faster computational speed that retains optimal assurance if the heuristics are valid. The next article in the column goes into Path Following — step which transforms this static point sequence into real-world motion over time.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

What is path planning? How Dijkstra and A* search a grid — Atlas Robotics