All episodes · From a List to Your Map App · Episode 19

How Maps Find the Fastest Route: Dijkstra's Algorithm

Pressing play loads the video from YouTube.

From A to G on a little map where every road shows its minutes: which way is fastest? Dijkstra's algorithm finds out without trying every route.
Start at zero, give every other junction infinity, then keep settling the closest junction and updating its neighbors. We watch the numbers drop (D from 10 to 9, G from 17 to 16) until G is settled at 16 minutes. We see why a settled number can be trusted when roads never subtract time, how a single negative road fools it, how A* settles far fewer junctions by heading toward the goal, and how Floyd's algorithm fills in a table for every pair.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

From A to G on this little map, which way is fastest? Each road shows the minutes it takes. Trying every route works for seven junctions, but a real map has millions. Dijkstra's algorithm finds the fastest route without trying them all. Today: shortest paths.

Start at A with zero. Every other junction gets infinity for now: no way found yet. The rule: settle the closest junction that isn't settled yet, then check its roads to see if anything gets closer. From A, C is two and B is four. C is closest, so it's settled, and through C, D would be ten. Next comes B, at four. Through B, D is only nine, so ten drops to nine. Then D at nine gives E twelve and F eleven. F comes first and offers G seventeen, but E then offers sixteen. Finally G is settled: sixteen minutes, through B, D and E.

Why trust a settled number? Every road adds zero minutes or more, so any other way there would start from somewhere already farther away. That's also why it breaks if a road could give time back: a negative road can beat a number we thought was final.

Map apps add a twist called A-star: it prefers junctions in the direction of the destination, so it settles far fewer junctions. And if you need the time between every pair of junctions, Floyd's algorithm fills in a whole table at once.

Settle the closest, update the neighbors, repeat. With no negative roads, every settled number is the true shortest time. Next time: connecting every house using the least cable in total. See you there.

Next episode: connecting every house using the least cable in total.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal