630 likes | 872 Vues
All-Pairs Shortest Paths. All-Pairs Shortest Paths. Could just run SSSP times Bad runtimes With negative weights and dense graph, Output: Matrix of min distance Input a Adjacency Matrix |. All-Pairs Shortest Paths. Create a predecessor matrix is N IL if or no path
E N D
All-Pairs Shortest Paths Jeff Chastine
All-Pairs Shortest Paths • Could just run SSSP times • Bad runtimes • With negative weights and dense graph, • Output: Matrix of min distance • Input a Adjacency Matrix | Jeff Chastine
All-Pairs Shortest Paths • Create a predecessor matrix • is NIL if or no path • is the predecessor of on some path to • th row is shortest-paths tree with root • Predecessor subgraph Jeff Chastine
Where to begin… • Assume a path : • Goes from vertex to vertex • Contains at most edges • Decompose into • contains edges Jeff Chastine
A recursive solution • Let be the min weight from to with edges. Then Jeff Chastine
Key IdeaThis may be painful • recursively is • min of AND • min weight from to using edges looking at predecessors of Jeff Chastine
Next • Compute a series of matrices: • then Jeff Chastine
The Algorithm EXTEND-SHORTEST-PATHS () 1 • Let be an matrix 3 forto 4 do for to 5 do 6 forto 7 do 8 return Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 What’s the minimum weight using only 1 edge? Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 We need to update the best way to get from vertex 1 passing through all others Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Currently, best way from 1 to 2 is found in . Can we do better? Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 We could try going through vertex 3, but that would cost 12! Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Not connected to 4. We could try passing through vertex 5, but it doesn’t connect to 2 Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 So, the best way from 1 to 2 was found in afterall. Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Currently, best way from 1 to 3 is found in . Can we do better? Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 We could try to go through 2, but it’s not connected to 3. Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Again, skip 4, and vertex 5 isn’t connected to 3. Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 So, the best way from 1 to 3 was in afterall. Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Going from 1 to 4 isn’t possible in . Can we do better? Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Going from 1 to 4 through 2 gives makes it possible using (2) edges! Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 We could try going through vertex 3, but it’s not connected to 4 Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Aha! Going through 5 gives us a better result of 2! Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 There’s no change for going to 5, so we won’t walk through it. Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 What about going from vertex 2 to vertex 1? It’s currently not possible in . Jeff Chastine
2 3 4 8 1 3 2 Painfully ExplicitExample 1 -4 -5 7 5 4 6 Vertex 2 isn’t connected to 1 or 3, but we can go through 4! Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 2 to 3... Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 3 to 4... Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 3 to 5... Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 4 to 2... Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 4 to 5... Jeff Chastine
2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 5 to 1... Jeff Chastine
2 3 4 8 1 3 2 What’s next? 1 -4 -5 7 5 4 6 What about 5 to 2? Does it get updated? Jeff Chastine
2 3 4 8 1 3 2 What’s next? 1 -4 -5 7 5 4 6 Nope. What about 5 to 3? Does it get updated? Jeff Chastine
2 3 4 8 1 3 2 What’s next? 1 -4 -5 7 5 4 6 Nope. What about 5 to 3? Does it get updated? Jeff Chastine
2 3 4 8 1 3 2 Important! 1 -4 -5 7 5 4 6 shows us the min weight using 2 edges! What about 3 edges? Jeff Chastine
2 3 4 8 1 3 2 Do the samething 1 -4 -5 7 5 4 6 Construct from Jeff Chastine
Notes • Runs in • EXTEND-SHORTEST-PATHS runs in • Called ( times to construct • Can be improved to • Repeated squaring (in book) • Similar to matrix multiplication Jeff Chastine
Floyd-Warshall • Runs in . • Again, no negative-weight cycles • Concept of intermediate vertex (IV): • In path , any vertex other than or Jeff Chastine
Floyd-Warshall • Considers only a subset of vertices • In any path , consider intermediate vertices • If is not an IV, then IVs consist of • Else, is an IV, so • Break path into • Both and are shortest paths • is not an intermediate vertex for and • All IVs of both and must exist in Jeff Chastine
Recursive Solution • Let be weight of shortest path from for which all IVs are in • implies no intermediate vertices Jeff Chastine
2 3 4 Predecessor subgraph: predecessor on way to another vertex. Realize that these are parts of paths! 8 1 3 2 1 -4 -5 7 5 4 6 Original state. Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 Example: This entry says 2’s predecessor is 1 when coming from 1. Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 Example: This entry says 3’s predecessor is 1 when coming from 1. Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 Example: This entry says 2’s predecessor is 3 when coming from 2. Jeff Chastine
2 3 Main Idea 4 8 1 3 2 When k=1, consider updating each entry by running paths through vertex 1. This will create D(1) and Π(1) 1 -4 -5 7 5 4 6 Original state. Jeff Chastine
2 3 4 8 1 3 We’re attempting to update all of 4’s entries by going through vertex 1. I skippedvertices 2 and 3 for thisexample. 2 1 -4 -5 7 5 4 6 What can we update 4’s paths going through vertex 1? Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 On a path going from 4 to 2, 2’s predecessor is 1 Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 On a path going from 4 to 5, 5’s predecessor is 1 Jeff Chastine
2 3 4 8 1 3 2 1 -4 -5 7 5 4 6 On a path going from 4 to 5, 5’s predecessor is 1 Jeff Chastine
2 3 4 8 1 3 We’ve created D(1) and Π(1). We can make D(2)and Π(2) byrepeating the process, running all paths through vertex 2. 2 1 -4 -5 7 5 4 6 Here we go again… Jeff Chastine