Dijkstra Notes
Dijkstra Notes
• At all times, and for every vertex v, if dist[v] is not ∞ then there is an all-black
path from s to v of length dist[v] (This is true at the beginning, and it is an
invariant that is maintained every time we update dist[.] of a vertex.
• For every vertex v, the value dist[v] can only be decreased or remain the same
from step to step, and it is never increased. (This can be verified by inspecting
the code.)
• If, at some point, for a certain vertex v, the value dist[v] equals the length
of the shortest path from s to v, then the value of dist[v] is never changed
in subsequent steps. (This is an immediate consequence of the previous two
observations.)
1. All the black nodes v have a value of dist[v] equal to the length of the shortest
path from s to v, or ∞ if no such path exists.
2. All the nodes v have a value of dist[v] equal to the length of the shortest all-black
path from s to v.
At the end of the execution of the algorithm, all nodes are black, and so either of the
two invariants implies that the values of dist[v] equal the length of the shortest path
from s to v, for all v.
1
Base Case of Induction. Consider what happens after the first iteration of the
while loop: the node s is taken out of the queue and is the only black node. We have
dist[s] = 0 and indeed the distance of s to itself is 0 (because the “empty path” is
a valid path of length 0, and there cannot be negative-length paths). This verifies
the first condition. For the second condition, dist[s] = 0 is also the length of the
shortest all-black path from s to itself; for vertices v 6= s, an all black path from s to
v must be a one-edge path s → v. If the edge (s, v) exists, then at the end of the first
iteration we indeed have dist[v] = `(s, v), which is the length of the all-black shortest
path from s to v. If the edge (s, v) does not exists, then we have dist[v] = ∞ which
is again correct because no all-black path exists from s to v.
Inductive Step Part (1). Now we argue that if both conditions are true after t
iterations, then the first condition must be true after t + 1 iterations. Let v be the
vertex that is removed from the queue during the (t + 1)-th iteration of the “while”
loop. We just need to argue that dist[v] is equal to the length of the shortest path
from s to v.
The operations that we perform during this iteration do not modify the value of
dist[v], which remains what it was after the t-th iteration. We want to argue that,
at time t, the two properties that we assume true after t iterations mean that v has
a value dist[v] equal to the actual shortest path length from s to v. Suppose, toward
a contradiction, that there is a path from s to v of length L < dist[v]. Because of
the second property, such path must use some white vertex as an intermediate step.
Let u be the first white vertex we encounter along such path. Then the length of this
path up to u is ≥ dist[u], because it is an all-black path from s to u and the second
property is telling us that dist[u] is the minimum length of such paths. Furthermore,
the length of this path up to u is ≤ L, so we conclude that
where all the value of dist[] refer at the end of the t-th iteration. But this is a
contradiction because v was chosen to be the white vertex of minimum dist[.], for the
values of dist[.] that we got at the end of the t-th iteration.
Inductive Step Part (2). Now we argue that if both conditions are true after t
iterations, then the second condition must be true after t + 1 iterations. As before,
let us call v the vertex that is taken out of the queue during iteration t + 1. We will
distinguish between the value distt [x] of a vertex x at the end of the t-th iteration,
and the value distt+1 [x] at the end of iteration t + 1.
If x is a vertex that was black at time t, then distt [x] was the length of the shortest
path from s to v, and we must have distt+1 [x] = distt [x]. Furthermore, by the second
property at time t we have that there is an all-black path from s to x of length distt [x],
2
so there is an all-black path from s to v of length distt+1 [x] at time t + 1 (and there
cannot be a shorter all-black path).
If we consider v, we argued above that distt+1 [v] = distt [v] and that distt+1 [v] is the
length of a shortest path from s to v. By property 2 applied at time t, we have that
there is an all-black path of length distt [v] at time t, and so there is an all-black path
from s to v of length distt+1 [v] at time t + 1 (and there cannot be a shorter all-black
path).
Finally consider a vertex y that is white at time t+1. Suppose toward a contradiction
that there is an all-black path from s to y of length L < distt+1 [y]. If the path
does not contain v, then the path was already all-black at time t, and it has length
L < distt+1 [y] ≤ distt [y] which contradicts property 2 at step t. If the path contains
v as last intermediate vertex, then the path has a s → v part of length L − `(v, y) plus
the edge `(v, y). Note that we must have distt [v] ≤ L − `(v, y), because distt [v] was
already the length of a shortest path from s to v. Note, however, that the updates
that we do at iteration t + 1 imply distt+1 [y] ≤ distt [v] + `(v, y), so we have