What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to given city. It is the algorithm for the shortest path, which I designed in about twenty minutes. One morning I was shopping in Amsterdam with my young fiancee, and tired, we sat down on the cafe terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. In fact, it was published in '59, three years later. The publication is still readable, it is, in fact, quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one of the advantages of designing without pencil and paper is that you are almost forced to avoid all avoidable complexities. Eventually, that algorithm became to my great amazement, one of the cornerstones of my fame.Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001
DIKJSTRA(G, s)
- for each vertex v ∈ G.V
- v.d = ∞
- v.π = NIL
- Q.enqueue(v)
- source.d = 0
- while not Q.empty() do
- u = Q.ExtractMin()
- for each v ∈ G.Adj(u)
- if u.d + w(u,v) < v.d
- v.d = u.d + w(u,v)
- v.π = u