18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

All-Pairs Shortest Path<br />

Next: War Story: Nothing but Up: Shortest Paths Previous: Dijkstra's <strong>Algorithm</strong><br />

All-Pairs Shortest Path<br />

If we want to find the length of the shortest path between all pairs of vertices, we could run Dijkstra's algorithm n<br />

times, once from each possible starting vertex. This yields a cubic time algorithm for all-pairs shortest path, since<br />

.<br />

Can we do better? Significantly improving the complexity is still an open question, but there is a superslick dynamic<br />

programming algorithm that also runs in .<br />

<strong>The</strong>re are several ways to characterize the shortest path between two nodes in a graph. <strong>The</strong> Floyd-Warshall algorithm<br />

starts by numbering the vertices of the graph from 1 to n. We use these numbers here not to label the vertices, but to<br />

order them. Define to be the length of the shortest path from i to j using only vertices numbered from 1, 2,..., k<br />

as possible intermediate vertices.<br />

What does this mean? When k = 0, we are allowed no intermediate vertices, so that every path consists of at most one<br />

edge. Thus . In general, adding a new vertex k+1 as a possible intermediary helps only if there is a short<br />

path that goes through it, so<br />

This recurrence performs only a constant amount of work per cell. <strong>The</strong> following dynamic programming algorithm<br />

implements the recurrence:<br />

Floyd(G)<br />

Let , the weight matrix of G<br />

for k=1 to n<br />

file:///E|/BOOK/BOOK2/NODE78.HTM (1 of 2) [19/1/2003 1:29:18]<br />

for i=1 to n<br />

for j=1 to n

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!