next up previous
Next: About this document Up: My Home Page

lec20-7.au Give two more shortest path trees for the following graph:

sp-dp-graph Run through Dijkstra's algorithm, and see where there are ties which can be arbitrarily selected.  

There are two choices for how to get to the third vertex x, both of which cost 5.

There are two choices for how to get to vertex v, both of which cost 9.

lec20-8.au

All-Pairs Shortest Path

Notice that finding the shortest path between a pair of vertices (s,t) in worst case requires first finding the shortest path from s to all other vertices in the graph.  

Many applications, such as finding the center or diameter of a graph, require finding the shortest path between all pairs of vertices.

We can run Dijkstra's algorithm n times (once from each possible start vertex) to solve all-pairs shortest path problem in tex2html_wrap_inline93 . Can we do better?

Improving the complexity is an open question but there is a super-slick dynamic programming algorithm which also runs in tex2html_wrap_inline95 . lec20-9.au Dynamic Programming and Shortest Paths

The four-step approach to dynamic programming is:

  1. Characterize the structure of an optimal solution.
  2. Recursively define the value of an optimal solution.
  3. Compute this recurrence in a bottom-up fashion.
  4. Extract the optimal solution from computed information.

From the adjacency matrix, we can construct the following matrix:

tex2html_wrap_inline97 , \>\>\> if tex2html_wrap_inline99 and tex2html_wrap_inline101 is not in E
D[i,j] = w(i,j), \>\>\> if tex2html_wrap_inline107
D[i,j] = 0, \>\>\> if i=j

This tells us the shortest path going through no intermediate nodes.

There are several ways to characterize the shortest path between two nodes in a graph. Note that the shortest path from i to j, tex2html_wrap_inline117 , using at most M edges consists of the shortest path from i to k using at most M-1 edges + W(k, j) for some k.

lec20-10.au This suggests that we can compute all-pair shortest path with an induction based on the number of edges in the optimal path.

Let tex2html_wrap_inline131 be the length of the shortest path from i to j using at most m edges.

What is tex2html_wrap_inline139 ?

What if we know tex2html_wrap_inline141 for all i,j?

since w[k, k]=0

This gives us a recurrence, which we can evaluate in a bottom up fashion:

for i=1 to n
\> for j=1 to n
\> tex2html_wrap_inline155
\> for k=1 to n
\> \> tex2html_wrap_inline161 =Min( tex2html_wrap_inline163 , tex2html_wrap_inline165 )

This is an tex2html_wrap_inline167 algorithm just like matrix multiplication, but it only goes from m to m+1 edges. lec20-11.au

Since the shortest path between any two nodes must use at most n edges (unless we have negative cost cycles), we must repeat that procedure n times (m=1 to n) for an tex2html_wrap_inline181 algorithm.

We can improve this to tex2html_wrap_inline183 with the observation that any path using at most 2m edges is the function of paths using at most m edges each. This is just like computing tex2html_wrap_inline189 . So a logarithmic number of multiplications suffice for exponentiation.

Although this is slick, observe that even tex2html_wrap_inline191 is slower than running Dijkstra's algorithm starting from each vertex! lec20-12.au The Floyd-Warshall Algorithm  

An alternate recurrence yields a more efficient dynamic programming formulation. Number the vertices from 1 to n.

Let tex2html_wrap_inline197 be the shortest path from i to j using only vertices from 1, 2,..., k as possible intermediate vertices.

What is tex2html_wrap_inline205 ? With no intermediate vertices, any path consists of at most one edge, so tex2html_wrap_inline207 .

In general, adding a new vertex k+1 helps iff a path goes through it, so

Although this looks similar to the previous recurrence, it isn't. The following algorithm implements it:

tex2html_wrap_inline211
for k=1 to n
\> for i=1 to n
\> \> for j=1 to n
\> \> tex2html_wrap_inline225

This obviously runs in tex2html_wrap_inline227 time, which asymptotically is no better than a calls to Dijkstra's algorithm. However, the loops are so tight and it is so short and simple that it runs better in practice by a constant factor.




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Sep 15 17:03:44 EDT 1998