Interactive Case Study

The Last Mile

The hardest part of any delivery is the very last stretch.

Your courier needs the fastest way from the depot to a customer across town — but the map has dozens of roads, each with its own drive distace/time. Guess wrong, and every delivery runs long.

The same problem shows up everywhere: a GPS app picking a route, a flight connecting through airports, a data packet hopping across a network. Every time, there are too many paths to just eyeball the fastest one.

How this works

A whole city of locations, connected by streets — each labeled with its walking time in minutes.

Goal: walk from your start to your destination as fast as possible
  • Drop two pins: tap a location on the map for your start, then tap another for your destination.
  • Walk it yourself: click a connected (highlighted) location to take a step. Keep going until you reach your destination.
  • Compare: once you arrive, see your time against the optimal route Dijkstra's algorithm would have taken — or reveal it early if you get stuck.

Find the fastest route

This is your city: dozens of locations connected by streets, each labeled with its walking time in minutes. Tap a location on the map to set your start, tap another for your destination, then walk your way across town — try to beat Dijkstra's optimal time.

1 Set your start
2 Set destination
3 Walk there
Start Destination You are here Visited Unvisited

Play-by-play

Your move-by-move route will show up here once you begin walking.

Reflect

Before we name what you just did, sit with what you noticed.

1

When you were choosing which location to visit next, what helped you decide where to go?

2

Did you always choose the road with the smallest distance/time shown on it? What happened when you did?

3

When you reached a location through different routes, how did you decide which route to keep?

4

As you visited more locations, how did the distance/time you had already found help you choose your next move?

5

What did you notice about the way the fastest route was built from one location to another?

Understanding Dijkstra's algorithm

In plain terms

Dijkstra's algorithm finds the shortest path from one starting node to every other node in a graph, by always taking the smallest guaranteed step next. It keeps a running "shortest known distance" for every node, and at each turn it locks in whichever unvisited node currently has the smallest number in that table — because once every cheaper path has already been checked, that number can't get any smaller. Locking it in, then rechecking its neighbors for shortcuts, is what lets every node's shortest distance fall into place one at a time, without ever having to try every possible path by hand.

dist(v) = min( dist(v), dist(u) + weight(u, v) )

Whenever a shorter path to a node v is found by passing through a just-visited node u, its distance is updated. This single check, repeated over and over, is called "relaxing" the edge.

Example: the shortest path found

🧭 44 Paths to check by hand A brute-force search has to consider every possible path through the graph — and that count explodes as more edges are added.
📍 8 Nodes visited — once each Dijkstra's algorithm settles each node's shortest distance exactly once, then moves on. It never has to revisit or backtrack.

Exercises

A few "what would you do" scenarios — no map required, just the idea.

1
Delivery routing

A delivery rider needs to travel from a warehouse to a customer through several roads with different travel times. How could Dijkstra's approach help the rider determine the route with the least total travel time?

2
Worked example

Consider the following road network: A–B = 4, A–C = 2, B–C = 1, B–D = 5, C–D = 8. Starting from A, apply Dijkstra's algorithm step by step and determine the shortest path from A to D and its total cost.

3
Equal-weight roads

Suppose every road in a network takes exactly the same amount of time to travel. Would Dijkstra's algorithm still be necessary? Which simpler approach could be used instead, and why would it produce the same shortest path?

4
Updating distances

While finding a shortest path, a location may first be reached through one route and later through another shorter route. How does Dijkstra's algorithm handle this change, and why is updating the previously found distance important?

5
Negative weights

A network contains some connections with negative costs, such as receiving a reward for taking a particular connection. Why can Dijkstra's algorithm fail to find the correct shortest path in this situation? What algorithm could be used instead, and what efficiency trade-off does it involve?

6
All-pairs paths

A company has 100 warehouses and wants to know the shortest route between every pair of warehouses, rather than from one warehouse to all others. How does this change the problem, and which algorithm could be used to solve all-pairs shortest paths?

7
Ant colony approaches

Dijkstra's algorithm systematically builds shortest paths using the costs already found, while ant colony-based approaches discover paths through repeated exploration and reinforcement. How do these two approaches differ in the way they find good paths, and what might be an advantage of using repeated exploration?

8
Relaxing a road

You have reached location B with a current travel time of 10 minutes. Later, you find a route to B through location C that takes 4 minutes to reach C and 3 more minutes from C to B. What should happen to B's recorded travel time, and why?

9
Why relax at all

While exploring a graph, Dijkstra's algorithm repeatedly checks whether reaching a neighbouring location through the current location gives a shorter route than the one already known. Why is this step necessary for finding the overall shortest paths?

10
Bellman-Ford trade-off

Dijkstra's algorithm may give an incorrect result when a graph contains negative-weight connections. Bellman-Ford can handle such connections but requires more computation. Why does the presence of negative weights change which algorithm should be used, and what efficiency trade-off is involved?