Intermediate 25 min

How Computers Find Paths

You open a maps app. You type in your destination. The app shows you several routes and picks the fastest one. How does it do that?

Behind the scenes, it’s solving a pathfinding problem. There are many possible routes from point A to point B. You want the cheapest one - shortest distance, fastest time, or lowest cost.

This same problem appears everywhere: routing in maps, game AI movement, network routing, robot navigation. The core question is always the same: what’s the best path?

The Core Problem

Think about it this way:

  • You have a starting point
  • You have a destination
  • There are obstacles or costs along the way
  • You want the path with the lowest total cost

That’s pathfinding in a nutshell.

Real-World Scenarios

Pathfinding algorithms power many systems you use every day. Let’s look at a few examples:

City Navigation 🗺️

The Problem: Find the fastest route from your home to a restaurant, avoiding traffic and construction.

How it works:

  • Streets are edges in a graph
  • Intersections are nodes
  • Edge weights represent travel time or distance
  • The algorithm finds the path with minimum total time

Why it matters: Saves time, reduces fuel consumption, helps avoid traffic.

Game Character Movement 🎮

The Problem: Make a game character move from point A to point B, avoiding walls and obstacles.

How it works:

  • The game world is a grid
  • Each cell is a node
  • Walls block certain paths
  • The algorithm finds a valid path around obstacles

Why it matters: Creates believable AI movement, makes games feel natural.

Robot Navigation 🤖

The Problem: Guide a warehouse robot from one location to another, avoiding shelves and other robots.

How it works:

  • The warehouse floor is a grid
  • Obstacles are marked as blocked cells
  • The algorithm finds a safe path
  • Can update in real-time as obstacles move

Why it matters: Enables autonomous navigation, improves warehouse efficiency.

Network Routing 🌐

The Problem: Route data packets through a network from source to destination.

How it works:

  • Network routers are nodes
  • Connections are edges
  • Edge weights represent latency or bandwidth
  • The algorithm finds the fastest or most reliable path

Why it matters: Ensures fast internet connections, reduces network congestion.

Interactive Scenario Selector

Choose a scenario to see how pathfinding applies:

Scenario Selector

Choose a scenario to see how it affects the system:

Selected: None

  • Requests per second: -
  • Average processing time: -
  • Expected capacity: -

What Makes a Path “Best”?

The definition of “best” depends on your problem:

Shortest distance: Minimize total length traveled

  • Example: Walking directions that minimize steps

Fastest time: Minimize total travel time

  • Example: Driving directions that avoid traffic

Lowest cost: Minimize total cost (money, energy, etc.)

  • Example: Shipping routes that minimize fuel costs

Safest path: Avoid dangerous areas

  • Example: Robot navigation avoiding hazards

In this tutorial, we’ll focus on finding the path with the lowest total cost. The same algorithms work for all these cases - you just change what the “cost” means.

The Algorithms We’ll Learn

We’ll explore two classic pathfinding algorithms:

Dijkstra’s Algorithm:

  • Explores all directions equally
  • Guarantees finding the optimal path
  • Works when you don’t know where the goal is
  • Good for finding paths to many destinations

A (A-Star) Algorithm:*

  • Uses a “heuristic” to guide the search
  • Focuses exploration toward the goal
  • Also finds optimal paths (with the right heuristic)
  • Usually faster than Dijkstra for single goals

Both algorithms solve the same problem, but they approach it differently. By the end of this tutorial, you’ll understand when to use each one.

What You’ll Build

In this tutorial, we’ll work with a simple grid world:

  • A 2D grid of cells
  • Some cells are walls (obstacles)
  • A start position
  • A goal position
  • Movement costs (usually 1 per step, but can vary)

We’ll visualize how algorithms explore this grid, step by step. You’ll see:

  • Which cells get explored
  • How the path gets discovered
  • Why one algorithm might be faster than another

Key Takeaways

Before moving forward, remember:

  1. Pathfinding is everywhere - Maps, games, robots, networks
  2. The core problem - Find the cheapest path from start to goal
  3. “Cheapest” depends on context - Distance, time, cost, or safety
  4. Two main approaches - Dijkstra (explore all) vs A* (guided search)
  5. Grids are graphs - We’ll model grids as graphs with nodes and edges

What’s Next?

In the next page, we’ll learn how to model a 2D grid as a graph. You’ll see how grid cells become nodes, and movement between cells becomes edges. This foundation makes the algorithms much easier to understand.

Progress 14%
Page 1 of 7
Previous Next