We are given a directed graph G = (V, E) and a weight function w : E → R (may have negative weights also). The natural versions of the shortest path problem are as follows
distance between a pair Given vertices x, y ∈ V , find the least weighted path starting at x and ending at y.
Single source shortest path (SSSP) Given a vertex s∈ V , find the least weighted path from s to all vertices in V − {s}.
All pairs shortest paths (APSP) For every pair of vertices x, y ∈ V , find least weighted paths from x to y.
Although the first problem often arises in practice, there is no specialized algo-rithm for it. The first problem easily reduces to the SSSP problem. Intuitively, to find the shortest path from x to y, it is difficult to avoid any vertex z since there may be a shorter path from z to y. Indeed, one of the most basic operations used by shortest path algorithms is the relaxation step. It is defined as follows
-Relax(u, v) : (u, v)∈ E,
if ∆(v) > ∆(u) + w(u, v) then ∆(v) = ∆(v) + w(u, v)
For any vertex v, ∆(v) is an upperbound on the shortest path. Initially it is set to
∞ but gradually its value decreases till it becomes equal to δ(v) which is the actual shortest path distance (from a designated source vertex).
The other property that is exploited by all algorithms is Observation 9.6 subpath optimality
Let s = v0, v1, v2. . . vi. . . vj. . . vℓ be a shortest path from v0tovℓ. Then for any inter-mediate vertices, vi, vj, the subpath vi, vi+2. . . vj is also a shortest path between vi and vj.
This follows by a simple argument by contradiction, that otherwise the original path is not the shortest path.
9.2.1 Bellman Ford SSSP Algorithm
The Bellman Ford algorithm is essentially based on the following recurrence δ(v) = min
u∈In(v){δ(u) + w(u, v)}
where In(v) denotes the set of vertices u ∈ V such that (u, v) ∈ E. The shortest path to v must have one of the incoming edges into v as the last edge. The algorithm actually maintains upperbounds ∆(v) on the distance from the source vertex s -initially ∆(v) =∞ for all v ∈ V − {s} and ∆(s) = 0 = δ(s). The previous recurrence is recast in terms of ∆
∆(v) = min
u∈In(v){∆(u) + w(u, v)}
that follows from a similar reasoning. Note that if D(u) = δ(u) for any u ∈ In(v), then after applying relax(u, v), ∆(v) = δ(v). The underlying technique is dynamic programming as many vertices may have common predecessors in the shortest path recurrence.
Bellman Ford SSSP Initialize ∆(s) = 0, ∆(v) = ∞ v ∈ V − {s}.
Repeat n− 1 times
relax(e) for all e∈ E
Output δ(v) = ∆(v) for all v ∈ V .
The correctness of the algorithm follows from the previous discussion and the following critical observation.
Observation 9.7 After i iterations, all vertices whose shortest paths consist of i edges, have ∆(v) = δ(v).
It follows from a straightforward induction on the number of edges in the path with the base case δ(s) = 0 and the definition of relax step.
So, the algorithm finds all shortest paths consisting of at most n− 1 edges with n− 1 iterations. However, if there is a negative cycle in the graph, then you may require more iterations and in fact, the problem is not well defined any more. We can specify that we will output simple paths (without repeated vertices) but this version is not easy to handle. 2
Exercise 9.9 Describe an efficient algorithm to detect negative cycle in graph.
Since each iteration involves O(|E|) relax operations - one for every edge, the total running time is bounded by O(|V | · |E|).
To actually compute the shortest path, we keep track of the predecessor of a vertex which is determined by the relaxation step. The shortest path can be constructed by following the predecessor links.
Exercise 9.10 If there is no negative cycle, show that the predecessors form a tree (which is called the shortest-path tree).
9.2.2 Dijkstra’s SSSP algorithm
If the graph doesn’t have negative weight edges, then we can exploit this feature to design a faster algorithm. When we have only non-negative weights, we can actually determine which vertex has the its ∆(v) = δ(v). In the case of Bellman Ford algo-rithm, at every iteration, at least one vertex had its shortest path computed but we
2This is equivalent to the longest path problem which is known to be intractable
couldn’t identify them. We maintain a partition U and V − U such s ∈ U where U is the set of vertices v ∈ V for which ∆(v) = δ(v). One the other hand, for non-negative weights, we can make the following claim.
Observation 9.8 The vertices v ∈ V − U for which ∆(v) is minimum, satisfies the property that ∆(v) = δ(v).
Suppose for some vertex v that has the minimum label after some iteration, ∆(v) >
δ(v). Consider a shortest path s ❀ x → y ❀ v, where y /∈ U and all the earlier vertices in the path s ❀ x are in U. Since x ∈ U, ∆(y) ≤ δ(x) + w(x, y) = δ(y).
Since all edge weights are non-negative, δ(y)≤ δ(v) < ∆(v) and therefore ∆(y) = δ(y) is strictly less than ∆(v) which contradicts the minimality of ∆(v).
A crucial property exploited by Dijkstra’s algorithm is that along any shortest path s ❀ u, the shortest path-lengths are non-decreasing because of non-negative edge weights. Along similar lines, we can also assert the following
Observation 9.9 Starting with s, the vertices are inserted into U is non-decreasing order of their shortest path lengths.
We can prove it by induction starting with s - δ(s) = 0. Suppose it is true upto iteration i, i.e., all vertices v ∈ U are such that δ(v) ≤ δ(x), x ∈ V − U. Let
∆(u) ∈ V − U be minimum, then we claim that ∆(u) = δ(u) (from the previous observation) and ∆(u) ≤ ∆(x), x ∈ V − U. Suppose δ(x) < δ(u), then by an extension of the previous argument , let y be the earliest vertex in s ❀ x that is not in U. Then ∆(y) = δ(y)≤ δ(x) < δ(u) ≤ ∆(u), thereby violating the minimality of
∆(u).
To implement this algorithm efficiently, we maintain a priority queue on the values of ∆(v) for v ∈ V −U, so that we can choose the one with the smallest value in O(log n) steps. Each edge is relaxed exactly once since only the edges incident on vertices in U are relaxed - however because of relax operation the ∆() of some vertices may change.
This yields a running time of ((|V | + |E|) log |V |).
9.2.3 Floyd-Warshall APSP algorithm
Consider the adjacency matrix AG of the given graph G = (V, E) where the entry AG(u, v) contains the weight w(u, v). Let us define the matrix product of AG· AG in a way where the multiplication and addition operators are replaced with addition and max operator.
Claim 9.2 Define the k-th iterate of AG, namely AkGfor n−1 ≥ k ≥ 2 as min{Ak−1G , Ak−1G · AG} where the min is the entrywise minimum. Then AkG(u, v) contains the shortest path distance between u and v consisting of at most k edges.
We can prove it by induction on k. Note that a path between u and v of at most k edges consists of a path of at most k− 1 edges to one neighbours of v followed by the last edge.