← Back to list

How Robots Find Their Way: A Simple Guide to Dijkstra’s Algorithm

Ever wondered how delivery robots, self-driving cars, or GPS navigation find the fastest route? The answer lies in a 70-year-old algorithm…

Santosh Srinivasaiah · 2026-02-02 02:27 · 2 claps · 3.5 min read
#dijkstras-algorithm #python #shortest-path
Open on Medium ↗
Wiki topics: 💻 · Programming

How Robots Find Their Way: A Simple Guide to Dijkstra’s Algorithm

Ever wondered how delivery robots, self-driving cars, or GPS navigation find the fastest route? The answer lies in a 70-year-old algorithm that’s still powering our modern world.

The Problem: Getting from A to B

Imagine you’re a delivery robot in a warehouse. You need to reach a specific shelf, but there are multiple paths you could take. Some paths are longer, some have obstacles, and some might be congested with other robots.

How do you pick the best route?

This is exactly the problem that Dutch computer scientist Edsger Dijkstra solved in 1956 — reportedly while sitting at a café with his fiancée, sketching on a napkin.

The Core Idea

Dijkstra’s algorithm finds the shortest path by being methodical and greedy (in a good way). Here’s how it thinks:

  1. Start where you are and mark your current location as “distance zero”
  2. Look at all neighbors and calculate how far each one is from the start
  3. Pick the closest unvisited neighbor and move there
  4. Repeat until you reach your destination

The key insight is simple: always explore the closest unexplored option first. This guarantees you’ll find the shortest path.

A Visual Example

Let’s say a robot needs to travel from point A to point E in this network:

          2
    A -------- B
    |          |
  1 |          | 3
    |    1     |
    C -------- D
     \        /
    4 \      / 1
       \    /
         E

The numbers represent distance (or time, or energy cost — whatever matters for your robot).

Step by step:

Notice how at step 4, we discovered a better route to E (through D) than our initial estimate (through C directly).

Simple Python Code

Here’s a minimal implementation you can run yourself:

import heapq
def dijkstra(graph, start, end):
    # Priority queue: (distance, node)
    queue = [(0, start)]
    distances = {start: 0}
    previous = {}

    while queue:
        current_dist, current = heapq.heappop(queue)

        if current == end:
            # Reconstruct path
            path = []
            while current in previous:
                path.append(current)
                current = previous[current]
            path.append(start)
            return path[::-1], current_dist

        for neighbor, weight in graph[current].items():
            distance = current_dist + weight
            if neighbor not in distances or distance < distances[neighbor]:
                distances[neighbor] = distance
                previous[neighbor] = current
                heapq.heappush(queue, (distance, neighbor))

    return None, float('inf')
# Our example graph
graph = {
    'A': {'B': 2, 'C': 1},
    'B': {'A': 2, 'D': 3},
    'C': {'A': 1, 'D': 1, 'E': 4},
    'D': {'B': 3, 'C': 1, 'E': 1},
    'E': {'C': 4, 'D': 1}
}
path, distance = dijkstra(graph, 'A', 'E')
print(f"Shortest path: {' → '.join(path)}")
print(f"Total distance: {distance}")

Output:

Shortest path: A → C → D → E
Total distance: 3

Why It Works for Robots

Dijkstra’s algorithm is perfect for robotics because:

  • It guarantees the optimal solution — no path will be shorter
  • It’s efficient — it doesn’t waste time exploring obviously bad routes
  • It’s flexible — edge weights can represent distance, time, energy consumption, or safety scores

Real-World Use Cases

Warehouse Robots

Amazon’s Kiva robots use pathfinding algorithms to navigate massive fulfillment centers. When you order something, robots race through a grid of shelves, calculating optimal routes while avoiding collisions with hundreds of other robots.

Self-Driving Cars

Autonomous vehicles use variations of Dijkstra’s algorithm to plan routes through city streets. The “distance” isn’t just physical — it factors in traffic, road conditions, and even the number of left turns (which are statistically more dangerous).

Delivery Drones

Companies like Wing and Zipline use pathfinding to navigate airspace. The algorithm helps drones avoid no-fly zones, buildings, and other aircraft while minimizing battery usage.

Video Game NPCs

Every time a game character walks around an obstacle to reach you, it’s likely using A* (a descendant of Dijkstra’s algorithm). Games like StarCraft process thousands of pathfinding queries per second.

Network Routing

The internet itself uses Dijkstra-based protocols. When you send a message, routers use shortest-path algorithms to bounce your data packets through the fastest series of connections.

Medical Robotics

Surgical robots plan instrument paths through the body to minimize tissue damage. The “cost” here isn’t distance — it’s the risk of harming healthy tissue.

Space Exploration

Mars rovers use pathfinding to navigate rocky terrain autonomously. With communication delays of up to 20 minutes, they can’t wait for human instructions — they must find safe paths on their own.

The Takeaway

Dijkstra’s algorithm is beautifully simple yet incredibly powerful. Whether it’s a warehouse robot picking your order, a self-driving car navigating rush hour, or a Mars rover avoiding boulders, the same fundamental idea applies: explore systematically, always choosing the most promising option, and you’ll find the best path.

Next time you get turn-by-turn directions or watch a robot navigate a space, you’ll know there’s a 70-year-old algorithm quietly doing the heavy lifting.

Want to experiment? The code above runs in any Python environment. Try adding new nodes, changing the weights, or finding paths between different points.

Inspired by Vitaliy Kaurov’s visualization of multiple robots finding shortest paths on road networks.


메타데이터
post_id
b1e01f8f5bc9
slug
how-robots-find-their-way-a-simple-guide-to-dijkstras-algorithm-b1e01f8f5bc9
url
https://medium.com/@srinivas.santosh/how-robots-find-their-way-a-simple-guide-to-dijkstras-algorithm-b1e01f8f5bc9
canonical_url
https://medium.com/@srinivas.santosh/how-robots-find-their-way-a-simple-guide-to-dijkstras-algorithm-b1e01f8f5bc9
author_url
https://medium.com/@srinivas.santosh
status
ok
fetched_at
2026-07-07 05:31:20