1 Shortest Paths & Dijkstras Algorithm
1.1 Problem Statement
Given a weighted graph G = (V, E), where w : E R, and a source vertex x such that
s V , we want to find the shortest paths from s to any other vertex in G. Note that
shortest really means lightest, i.e., paths that have the smallest total weight (the sum
of the weight for the edges on the path).
a
1 4
7
s b
3 8
Figure 1: A Directed and Weighted Graph
1.2 Dijkstras Algorithm
Idea: Suppose that we already have some subset S of vertices such that for every vertex v
in S, we know that shortest paths from s to v. Consider the vertices outside of S. In par-
ticular, consider the first vertices (say a, b, and c) outside of S. What would be the shortest
way to get to a from s? It might seem like simply adding a to S directly is the best thing
to do, but it does not quite work: for example, it might be a shorter path to a going through c.
What we do know is that the shortest path from s to a is no-longer than the path we have
found from vertices in S. This allows us to keep track of an upper bound on the length of
the shortest paths from s to every vertex outside of S, but we dont know for sure if that
upper bound is the exact value or not. So how do we get around this?
Is there at least one vertex for which we know for sure weve found a shortest path? If we
pick a vertex w outside of S that has the smallest upper bound, we must have in fact a
shortest path from s to w because if there was a shorter way, we would have picked it first.
This gives the following idea for an algorithm: for each vertex v in G, we will keep track of
d[v], an upper bound on the weight of a shortest path from s to v. Initially we set S = {},
d[s] = 0, and d[v] = for for every v 6= s. Then, we repeatedly pick a w outside of S such
that d[w] is smallest, add it to S, and update d[v] for every v that is adjacent to w, until all
the vertices are in S. If we also keep track of the predecessor for each vertex as we do this,
we can reconstruct the paths from s to every other vertex easily.
1
DIJKSTRA(G=(V,E), w: E -> R, s in V)
foreach v in V do
d[v] <- inf // shortest distance found to v
pred[v] <- nil // predecessor of v: endpoint of the last edge in a shortest
// path to v)
endfor
S ={} // set of vertices whose shortest path is known.
d[s] <- 0
while(v is not empty) do
for u in V such that d[u] is minimal
S <- S union {u}, V <- V - {u}
for each vertex v adjacent to u
if ( v in not in S and d[v] > d[u] + w(u,v))
d[u] <- d[u] + w(u,v)
pred[v] <- u
end if
end for
end while
END
1.3 Examples
1
7
a c
3 0
s 4 e
5 4
1
b d
Example 1. Tracing the algorithm on this graph produces the following values for S, d[ ],
and pred[ ] after each iteration of the while-loop (for every iteration, we indicate the values
of d[ ] and pred[ ] only for the vertices that remain outside of S).
2
d[ ] pred[ ]
S s a b c d e s a b c d e
{} 0 - - - - - -
{s} 3 5 s s - - -
{s, a} 5 10 s a - -
{s, a, b} 9 6 b b -
{s, a, b, d} 9 10 b d
{s, a, b, d, c} 9 c
{s, a, b, d, c, e}
which gives the following final values (from which we can easily find shortest paths and their
lengths).
d[ ] pred[ ]
S s a b c d e s a b c d e
{s, a, b, d, c, e} 0 3 5 9 6 9 - s s b b c