Algorithm. Dijkstra's Algorithm (basic version) [00NY]

Here’s the basic algorithm, which we can think of as a generalization of BFS to weighted graphs. Recall that BFS keeps track of:

  • which vertices have been visited so far,
  • a layer map, giving the layer of each visited vertex, and
  • a parent map recording the parent of each vertex in the BFS tree.
The visited vertices and parent map can be kept unchanged. The list of layers was really telling us the shortest distance to the nodes in each layer, since distance in an unweighted graph is just the number of edges. Therefore instead of recording the "layer" of each vertex we will just record the distance to each vertex from s.

d \gets empty dictionary
\mathit {parent} \gets empty dictionary
Mark s visited
d[s] \gets 0
\mathsf {while} not all vertices are visited:
Pick visited u and unvisited v such that d[u] + w_{uv} is as small as possible
d[v] \gets d[u] + w_{uv}
\mathit {parent}[v] \gets u
Mark v visited
\mathsf {return} d, \mathit {parent}