How does shortest path work
When you open a map to find a route, the road with the fewest turns isn't always the fastest. A route may look short, but if it's congested or winds around, it's slower. That's because each road has a different length. So how do you find the fastest route?
Each line has a different length
You already know how to connect points with lines. But real roads have one more thing: each line has a different length.
Some roads are short and some are long, so we write a number on each line. That number is the distance.
Press the roads below to check their distances.
Press a road to see its distance. (line = road · number = that road's distance)
Why does it matter that distances differ? Even a route with fewer junctions can be slower overall if each stretch is long.
Conversely, a route that passes more junctions can be faster overall if each stretch is short.
So you can't choose a route while ignoring distance.
Counting steps in order isn't enough
Earlier we saw the method that spreads out from nearest to farthest: one step, two steps, three steps, counting how many steps away from the start a point is.
But that only works when every road is exactly one step long.
When roads have different lengths, the route with fewer steps isn't necessarily closer.
Press the two routes to compare. (fewer steps vs shorter distance · not the same)
The route with fewer junctions turned out to be longer in distance. Count only steps, and you fall into this trap.
So instead of counting steps, we have to add up distances to find the truly closest route.
Lock in the nearest first
So what do we do? Think back to greedy, which picks the best option in front of it.
Starting from the start point, among the points whose distance isn't settled yet, pick the one that's closest right now and lock in its distance.
As long as no road has a negative length, there can't be a shorter way to the closest point, so locking it in is safe.
Press the closest point to lock it in. (each time, pick the unsettled point with the shortest distance)
This is picking the best option in front of you. Instead of working out the whole map, you lock in points one at a time, starting from the closest.
Once a point is locked in, its neighbors can become closer by going through it.
This method is called Dijkstra's algorithm.
Widening the frontier
As more points are locked in, a "region of settled distances" forms, and its edge is the frontier.
Each time you pull in the closest point just outside the frontier, the region grows by one point.
It's like ripples spreading when you toss a stone into still water.
Press a point just outside the frontier to grow the region. (the settled region grows outward one point at a time)
As the frontier keeps spreading outward, eventually every point is inside the settled region.
By then you know the fastest distance from the start to every point. Nearest first, step by step: that's the secret.
Summary
Here's the fastest-route search in one line. When each line has a different length, the route with fewer steps isn't necessarily the fastest, so counting steps in order isn't enough.
Lock in distances starting from the nearest point and widen the frontier one point at a time, and by picking the best option at every moment, you find the shortest distance to every point.
Press the key points in order to sum up. (distances differ → counting steps fails → lock in the nearest first → widen the frontier)
When a map app finds the fastest route, this is what's happening inside: it locks in distances starting from the nearest places and widens the frontier little by little.
The simple rule of taking the best option in front of you brings back the fastest answer, even on complicated roads where every distance is different.