Pathfinding (Graph Search)

The quiet mathematics behind Google Maps, game characters, and logistics.

Pathfinding Algorithms

AnalogyDefinition

Imagine you're in an unfamiliar city looking for the train station. Different search strategies exist:

Uninformed search (like BFS or Dijkstra): Systematically explores all streets without knowing where the station is. Thorough, but time-consuming. Informed search (like A* or Greedy): Uses a 'compass' - estimates the direction to the goal and prioritizes paths leading that way.

The right choice of algorithm depends on your situation: Do you need the shortest path? How much do you know about the goal's location?

This demo shows how different pathfinding algorithms work. You can watch the algorithm in action and see how it finds the path step by step.

Interacting with the Demo

Draw walls on the grid to create obstacles. Set start and goal points. Choose an algorithm and click 'Start' to watch the search unfold.

What the Colors Mean

Yellow cells are in the Open List (waiting to be explored). Blue cells have already been visited (Closed List). The green path shows the found route.

The Different Algorithms

Each algorithm searches differently: A* uses a heuristic for direction, Dijkstra searches evenly in all directions, and Greedy runs straight toward the goal. Compare them to see their strengths and weaknesses.

Interactive Pathfinding Demo

What happens on the grid

This demo searches for the shortest path across a grid. Here is what you see on screen — the numbers below the grid keep count of it all.

What you see
A grid of tiles with a start (🤖), a goal (🚩) and black walls in between. Around the start lies a patch of coloured cells that reaches around the walls.
What happens
From the start, a front of visited cells grows outward and flows around the walls — like spreading water. The moment it reaches the goal, the shortest path lights up as one connected line from start to goal.
What you can do
Draw walls or move the start and goal, pick an algorithm, then run the search or step through it one move at a time. The speed control sets how fast the front spreads.
What to watch for
The search does not flail blindly in every direction. Some methods head straight for the goal, others spread out evenly — and either way the shortest path is found, guaranteed.
Ready! Click Start or draw walls and then start the algorithm.
⚡ x5 (200ms)
x1x100
Choose the search algorithm. Different algorithms explore the grid in different ways.

Named after Manhattan's rectangular street grid: You can only move horizontally or vertically, never diagonally. The distance is the sum of steps in X and Y direction.

ScenariosDraw Mode

Choose a scenario to observe different algorithm behaviors:

Random Maze:

How A* Works

TheoryPseudo-CodeStep by StepFlow Diagram

The A* Search Algorithm

A* (pronounced 'A-star') is one of the most popular pathfinding algorithms. It was developed in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael. A* combines the best of Dijkstra's algorithm (guaranteed shortest path) with Greedy Best-First Search (fast directional search).

The key insight of A* is the formula f(n) = g(n) + h(n), where g(n) is the actual cost from start to current node, and h(n) is the estimated cost from current node to goal (the heuristic). By always expanding the node with lowest f-value, A* efficiently finds the optimal path.

The Heuristic Function

The heuristic h(n) is an estimate of the remaining distance. Common heuristics include: Manhattan distance (sum of horizontal and vertical distances), Euclidean distance (straight-line distance), and Chebyshev distance (maximum of horizontal and vertical distances).

For A* to guarantee the optimal path, the heuristic must be admissible - it must never overestimate the actual cost. An optimistic heuristic ensures we never miss a better path.

Advantages of A*

  • Optimal: Guarantees the shortest path when using an admissible heuristic
  • Efficient: Explores fewer nodes than Dijkstra by using directional guidance
  • Flexible: Works with different heuristics for various scenarios
  • Complete: Will find a path if one exists

Practical Applications

A* is used everywhere: video game pathfinding, GPS navigation, robotics, network routing, puzzle solving (like the 15-puzzle), and AI planning. Its efficiency and optimality make it the go-to algorithm for pathfinding problems.

Try the demo! Watch how A* uses the heuristic to find the shortest path efficiently. Compare it with Dijkstra to see the difference!

How Dijkstra Works

TheoryPseudocodeStep by StepFlowchart

Dijkstra's Algorithm

Dijkstra's algorithm, invented by Edsger Dijkstra in 1956, finds the shortest path from a start node to all other nodes in a graph. Unlike A*, it doesn't use any heuristic - it explores systematically in all directions.

The algorithm maintains a distance value for each node. Starting from 0 at the start node, it always expands the node with the smallest known distance. This guarantees finding the optimal path.

How it Works

Dijkstra expands in a circular pattern from the start, like ripples on water. It visits nodes in order of their distance from start, ensuring the shortest path is found when the goal is reached.

Properties

  • Optimal: Always finds the shortest path
  • Complete: Will find a path if one exists
  • No Heuristic: Doesn't know goal direction
  • More Exploration: Often visits more nodes than A*

Watch the demo! See how Dijkstra explores in all directions - compare with A* to see the difference!

How Greedy Best-First Works

TheoryPseudocodeStep by StepFlowchart

Greedy Best-First Search

Greedy Best-First Search always moves toward the goal as directly as possible. It only considers the heuristic h(n) - the estimated distance to the goal - ignoring how far it has already traveled.

This makes it very fast when there are no obstacles, but it can get trapped in dead ends because it doesn't consider the actual path cost.

The Greedy Approach

Like someone walking toward a mountain - always heading in its direction without considering if there's a cliff in the way. Fast when the path is clear, problematic when obstacles exist.

Properties

  • Fast: Goes directly toward goal
  • Not Optimal: May not find shortest path
  • Can Get Stuck: Traps fool it easily
  • Low Memory: Explores fewer nodes

Try the spiral scenario! Watch how Greedy walks into the trap while A* finds the way around.

How Breadth-First Search Works

TheoryPseudocodeStep by StepFlowchart

Breadth-First Search (BFS)

BFS explores a graph level by level, like ripples spreading from a stone dropped in water. It visits all nodes at distance 1 first, then distance 2, then distance 3, and so on.

Using a queue (FIFO), BFS guarantees finding the shortest path in unweighted graphs - where all edges have the same cost.

The Wave Pattern

Imagine dropping a stone in a pond - the waves spread outward evenly in all directions. BFS works the same way, exploring all directions equally before moving further from start.

Properties

  • Optimal: Shortest path in unweighted graphs
  • Complete: Always finds a path if exists
  • Fair: Explores all directions equally
  • High Memory: Stores all nodes at current level

Watch the demo! See how BFS expands like a wave - compare with DFS to see the difference!

How Depth-First Search Works

TheoryPseudocodeStep by StepFlowchart

Depth-First Search (DFS)

DFS explores as far as possible along each branch before backtracking. It goes deep into the graph first, only exploring other paths when it hits a dead end.

Using a stack (LIFO), DFS is memory-efficient but doesn't guarantee finding the shortest path.

The Maze Explorer

Imagine exploring a maze by always turning the same direction (e.g., right) at every intersection. You'll eventually explore everything, but you might take long detours.

Properties

  • Memory Efficient: Only stores current path
  • Not Optimal: May find long detours
  • Complete: Finds a path if exists (finite graphs)
  • Fast for Deep Goals: Good when goal is far from start

Watch the demo! See how DFS dives deep before backtracking - compare with BFS!

Test Your Knowledge

Question 1 / 10
Not completed

What does the formula f(n) = g(n) + h(n) calculate in A*?

Select one answer
Answer Key: 1) B · 2) C · 3) A · 4) D · 5) B · 6) B · 7) C · 8) B · 9) B · 10) B