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.