1 / 57

All-Pairs Shortest Paths

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

kenna
Télécharger la présentation

All-Pairs Shortest Paths

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. All-Pairs Shortest Paths Jeff Chastine

  2. 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

  3. 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

  4. Where to begin… • Assume a path : • Goes from vertex to vertex • Contains at most edges • Decompose into • contains edges Jeff Chastine

  5. A recursive solution • Let be the min weight from to with edges. Then Jeff Chastine

  6. Key IdeaThis may be painful • recursively is • min of AND • min weight from to using edges looking at predecessors of Jeff Chastine

  7. Next • Compute a series of matrices: • then Jeff Chastine

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 2 to 3... Jeff Chastine

  27. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 3 to 4... Jeff Chastine

  28. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 3 to 5... Jeff Chastine

  29. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 4 to 2... Jeff Chastine

  30. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 4 to 5... Jeff Chastine

  31. 2 3 4 8 1 3 2 Fast-Forward! 1 -4 -5 7 5 4 6 From 5 to 1... Jeff Chastine

  32. 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

  33. 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

  34. 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

  35. 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

  36. 2 3 4 8 1 3 2 Do the samething 1 -4 -5 7 5 4 6 Construct from Jeff Chastine

  37. 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

  38. Floyd-Warshall • Runs in . • Again, no negative-weight cycles • Concept of intermediate vertex (IV): • In path , any vertex other than or Jeff Chastine

  39. 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

  40. Recursive Solution • Let be weight of shortest path from for which all IVs are in • implies no intermediate vertices Jeff Chastine

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related