This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Algorithms Lecture 14: All-Pairs Shortest Paths The tree which fills the arms grew from the tiniest sprout; the tower of nine storeys rose from a (small) heap of earth; the journey of a thousand li commenced with a single step. — Lao-Tzu, Tao Te Ching , chapter 64 (6th century BC), translated by J. Legge (1891) And I would walk five hundred miles, And I would walk five hundred more, Just to be the man who walks a thousand miles To fall down at your door. — The Proclaimers, “Five Hundred Miles (I’m Gonna Be)”, Sunshine on Leith (2001) Almost there...Almost there... — Red Leader [Drewe Henley], Star Wars (1977) 14 All-Pairs Shortest Paths 14.1 The Problem In the previous lecture, we saw algorithms to find the shortest path from a source vertex s to a target vertex t in a directed graph. As it turns out, the best algorithms for this problem actually find the shortest path from s to every possible target (or from every possible source to t ) by constructing a shortest path tree. The shortest path tree specifies two pieces of information for each node v in the graph: • dist ( v ) is the length of the shortest path (if any) from s to v ; • pred ( v ) is the second-to-last vertex (if any) the shortest path (if any) from s to v . In this lecture, we want to generalize the shortest path problem even further. In the all pairs shortest path problem, we want to find the shortest path from every possible source to every possible destination. Specifically, for every pair of vertices u and v , we need to compute the following information: • dist ( u , v ) is the length of the shortest path (if any) from u to v ; • pred ( u , v ) is the second-to-last vertex (if any) on the shortest path (if any) from u to v . For example, for any vertex v , we have dist ( v , v ) = 0 and pred ( v , v ) = N ULL. If the shortest path from u to v is only one edge long, then dist ( u , v ) = w ( u → v ) and pred ( u , v ) = u . If there is no shortest path from u to v —either because there’s no path at all, or because there’s a negative cycle—then dist ( u , v ) = ∞ and pred ( v , v ) = N ULL. The output of our shortest path algorithms will be a pair of V × V arrays encoding all V 2 distances and predecessors. Many maps include a distance matrix—to find the distance from (say) Champaign to (say) Columbus, you would look in the row labeled ‘Champaign’ and the column labeled ‘Columbus’. In these notes, I’ll focus almost exclusively on computing the distance array. The predecessor array, from which you would compute the actual shortest paths, can be computed with only minor additions to the algorithms I’ll describe (hint, hint). 14.2 Lots of Single Sources The obvious solution to the all-pairs shortest path problem is just to run a single-source shortest path algorithm V times, once for every possible source vertex! Specifically, to fill in the one-dimensional subarray dist [ s , · ] , we invoke either Dijkstra’s or Shimbel’s algorithm starting at the source vertex s ....
View Full Document
This note was uploaded on 12/15/2009 for the course 942 cs taught by Professor A during the Spring '09 term at University of Illinois at Urbana–Champaign.
- Spring '09