Docsity

All Pair Shortest Path, Lecture Slides - Computer Science, Slides of Introduction to Computers

Partial preview of the text

Download All Pair Shortest Path, Lecture Slides - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!

COMS21102: All-Pairs Shortest Paths

Raphaël Clifford [email protected]

Bristol University, Department of Computer Science Bristol BS8 1UB, UK

23rd November 2006

Raphaël Clifford [email protected]

All Pairs Shortest Paths

What do we do if want the shortest path from every vertex to every other vertex? I (^) We could just run a single source shortest path algorithm for every vertex. I (^) Assuming we use Dijkstra’s shortest path algorithm, the total time would be O(V 2 log V + VE) using a Fibonacci heap. I (^) If negative weights are allowed we would use Bellman-Ford giving O(V 2 E) time. I (^) We can do better.

Raphaël Clifford [email protected]

Matrix Multiplication

The final shortest path distances δ(i, j) will be d (^) ij ( n −^1 ). How can we compute them efficiently? Let’s look at matrix multiplication and see how it can help us. I (^) Compute C = A · B, where C, A, and B are n × n matrices:

c ij =

∑^ n

k = 1

a ik b kj

The time required is O(n^3 ) using a standard algorithm. I (^) What if we map “+” to “min” and “×” to “+”?

c ij = min k {a ik + b kj }

Raphaël Clifford [email protected]

Matrix Multiplication

Therefore D( m )^ = D( m −^1 )^ “×” W

To initialise D^0 = (d ( 0 ) ij ) =

I (^) We have that D(^1 )^ = D(^0 )^ · W = W 1 I (^) D(^2 )^ = D(^1 )^ · W 1 = W 2 I (^) D(^3 )^ = D(^2 )^ · W 2 = W 3 I (^)... I (^) D( n −^1 )^ = D( n −^2 )^ · W n −^2 = W n −^1

Remember that the final result we are looking for is D( n −^1 ). The total time is Θ(n · n^3 ) = Θ(n^4 ). This is no better than running Bellman-Ford n times!

Raphaël Clifford [email protected]

The Floyd-Warshall algorithm

Can we do any better than O(V 3 log V )? Yes, by an even cleverer application of dynamic programming. I (^) The Floyd-Warshall algorithm takes O(V 3 ) time and also allows negative weight edges (although not negatively weighted cycles) I (^) Define c( ijk )=weight of shortest path from i to j with all intermediate vertices coming from the set { 1 ,... , k} I (^) Therefore δ(i, j) = c ij ( n )and c( ij^0 )= w ij

Raphaël Clifford [email protected]

The Floyd-Warshall recurrence

Consider all paths between vertices i and j whose intermediate vertices are in { 1 ,... , k}. Let p be a minimum weight path from among them. We observe that I (^) If k is not an intermediate vertex in p then all intermediate vertices of path p are in the set { 1 ,... , k − 1 }. Therefore, a shortest path from vertex i to j with all intermediate vertices in the set { 1 ,... , k − 1 } is also a shortest path from from i to j with all intermediate vertices in { 1 ,... , k}. I (^) If k is an intermediate vertex in p, then we consider the paths from i to k and k to j separately. Each separate path must be a shortest path with all intermediate vertices in { 1 ,... , k − 1 } By observing the relationship between paths all of whose vertices are in { 1 ,... , k − 1 } and paths all of whose vertices are in { 1 ,... , k} we can set up a recurrence that is different from the one given before.

Raphaël Clifford [email protected]

The Floyd-Warshall example

On blackboard...

Raphaël Clifford [email protected]

The Floyd-Warshall algorithm

In implementation you can leave off the superscripts I (^) Floyd-Warshall algorithm runs in O(n^3 ) time. I (^) It is simple to implement. I (^) Efficient in practice. I (^) Based on a clever observation that allowed dynamic programming to solve the all-pairs shortest path problem faster than was previously possible. How can we actually construct the shortest paths? We currently only know the length of the paths, not the actual paths themselves. I (^) We make the predecessor matrix Π I (^) Π = (π ij ) where π ij =NIL if either i = j or there is no path from i to j, and otherwise π ij is the predecessor of j in some shortest path from i to j I (^) From the predecessor matrix we can find the vertices for a given shortest path

Raphaël Clifford [email protected]

Johnson’s algorithm for sparse graphs

Johnson’s algorithm solves the all-pairs shortest path problem in O(V 2 log V + VE) time using a quite different approach to the previous algorithms we have discussed. For sparse graphs (i.e. where E is o(V 2 )) this is asymptotically the fastest method we have seen. I (^) The basic trick is to reweight the graph so that all the edge weights are nonnegative I (^) We can then run Dijkstra’s algorithm, once from each vertex I (^) Using the fastest implementation of Dijkstra’s algorithm, with the Fibonacci-heap priority queue, this gives us the time complexity of O(V 2 log V + VE)

Raphaël Clifford [email protected]

Johnson’s algorithm for sparse graphs

Any new weight function wˆ must satisfy the following criteria I (^) For all pairs of vertices u, v ∈ V , a path p is a shortest path from u to v using weight function w if and only if p is also a shortest path from u to v using weight function wˆ. I (^) All weights wˆ(u, v ) are non-negative

Raphaël Clifford [email protected]

All-Pairs Shortest Path in reweighted graph

Corollary

δ h (u, v ) = δ(u, v ) + h(u) − h(v )

I (^) Main idea: Find a function h : V → R such that w h (u, v ) ≥ 0 for all (u, v ) ∈ E. Then, run Dijkstra’s algorithm from each vertex on the reweighted graph. I (^) w h (u, v ) ≥ 0 if and only if h(v ) − h(u) ≤ w(u, v )

Raphaël Clifford [email protected]

Johnson’s algorithm

  1. Find a function h : V → R such that w h (u, v ) ≥ 0 for all (u, v ) ∈ E by using Bellman-Ford to solve the difference constraints h(v ) − h(u) ≤ w(u, v ), or determine that G has a negatively weighted cycle Time = O(VE)
  2. Run Dijkstra’s algorithm from each vertex u ∈ V using the weight function w h to compute δ h (u, v ) for all v ∈ V

Time = O(V 2 log V + E log V )

  1. For each (u, v ) ∈ V × V compute δ(u, v ) = δ h (u, v ) − h(u) + h(v )

Time = O(V 2 )

Total time is O(V 2 log V + VE)

Raphaël Clifford [email protected]

Further Reading

I (^) Introduction to Algorithms T.H. Cormen, C.E. Leiserson, R.L. Rivest and C. Stein. MIT Press/McGraw-Hill, ISBN: 0-262-03293-7. I (^) Chapter 25 – All-Pairs Shortest Paths I (^) Algorithms S. Dasgupta, C.H. Papadimitriou and U.V. Vazirani http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/ I (^) Chapter 6, Section 6.6 – Brief discussion of Floyd-Warshall

Raphaël Clifford [email protected]