Lecture 23--Shortest path algorithms; transitive closure. 11/19/97.

======================================================================== 23.1 Shortest path algorithms. The procedure used in Prim's algorithm to find a minimum spanning tree can be modified to give an algorithm for finding the shortest path between two vertices or for finding the shortest paths from a fixed vertex v to any other vertex in the graph.

Let G = (V,E,W), where |V| = n, |E| = m, and W is a weight function defined on the edges of G with W(e) >= 0.

Dijkstra's Single Source Shortest Paths Algorithm:

Choose a "source" vertex v.

\\S will contain the vertices reachable so far. The array D will \\contain the "best" value for a shortest path from v \\to each vertex w in V which we have found so far. When we are \\finished, D will contain the weight of the shortest path from \\v to w for any w in V.

Initialize S to contain v.

Let l(x,y) be 0 if x = y, W((x,y)) if (x,y) is in E, and maxint otherwise.

Initialize D[w] to be l(v,w).

\\(Note: if G is not connected and v and w are in different components \\of G, then when the

\\algorithm finishes, D(w) will still be maxint, i.e., + oo.)

Let stuck be a boolean variable which becomes true if we can make no more progress (i.e., G is not connected). Initialize stuck to false.

While S <> V and not stuck do

choose w in V - S with D[w] a minimum.

add w to S

for each x in V - S do

D[x] = min ( D[x], D[w] + l(w,x) ).

Example. For the graph in Figure 6 we have

Iteration S            w   D[w] D[v1] D[v2] D[v3] D[v4]

initial {v0} - - 2 +oo +oo 10

1 {v0,v1} v1 2 2 5 +oo 9

2 {v0,v1,v2} v2 5 2 5 9 9

3 {v0,v1,v2,v3} v3 9 2 5 9 9

4 V v4 9 2 5 9 9

Exercise 23.1. Use Dijkstra's algorithm to find the shortest paths from vertex 5 to each other vertex in the graph given in figure 5.

Exercise 23.2. Give an example to show that Prim's MST algorithm cannot be used to find the shortest paths from a vertex v, i.e., show that the MST does not necessarily give the shortest paths.

Exercise 23.3. Give an example to show that Dijkstra's algorithm does not work if edge weights are allowed to be negative.

Correctness.

Space usage. Clearly this algorithm can be implemnted in space "capital theta" (m + n).

Time to run. The initialization can be completed in time "capital theta" (m + n). The loop will be executed at most n times. We can keep the w's in a priority queue. The queue will contain at most 2m + n values and so insertion can be done in time "capital theta"(log2(m+n)).

At most degree w nodes x till have their D value updated, so this step will be done at most 2m times over all executions of the loop.

So the total execution time will be O(mlog2(m+n)).

Correctness. We can prove correctness of this algorithm by induction. Specifically, we must prove that the shortest path from v to another vertex w lies completely in the set S and has length D(w).

Step 1. This is true for w = v.

Step 2. (inducting on the size of S). Suppose we are about to add w to S and the statement is true for all vertices added previously. If D(w) is not the length of a shortest path, there is a shorter path, which we will call P. And P must contain a vertex not in S. Call the first vertex on the path P from v by the name y. Then the distance from v to y is shorter than D(w) and the path from v to y (consisting of one edge) lies wholly in S. Thus by induction y should already have been put in S, since D(y) < D(w). Contradiction. So such a y cannot exist and the inductive step is verified.

Step 3. By 1 and 2 we have proved our claim,

23.2. Transitive closure of a graph or digraph.

If G is a graph or a digraph, G = (V,E), then the transitive closure of G is a graph G' = (V,E') such that there is an edge from v to w in G' if there is a path from v to w in G.

If we represent G by its adjacency matrix A, with A(x,y) = 1 if there is an edge from x to y and 0 otherwise, then we can compute the transitive closure G' by Warshall's Algorithm.

Warshall's Algorithm.

Assume G has n vertices.
Let R be the adjacency matrix of G' (to be computed).
Initialize R to be the adjacency matrix A.

For k = 1 to n for i = 1 to n for j = 1 to n, if R(i,k) = 1 and R(k,j) = 1, then set R(i,j) = 1.

We can prove this algorithm is correct by induction on the triples (i,j,k), lexicographically ordered (proof omitted).

Exercise 23.4. Find the ransitive closure of the graph in Figure 7 using Warshall's algorithm. Note 1. Clearly Warshall's algorithm runs in time "capital theta"(n3).

Note 2: Warshall's algorithm can be modified to find the shortest path from i to j by including the weights of the shortest path found so far instead of 1's or 0's. It can also be used to find the transitive closure of any binary relation, since a binary relation can be represented by a digraph.