Search⌘ K

Introduction to All-Pairs Shortest Paths

Explore the fundamentals of the all-pairs shortest paths problem, including how to find shortest distances between every vertex pair in a graph. Understand the role of shortest path trees, negative cycles, and different algorithms like Dijkstra and Bellman-Ford. Learn reweighting techniques to handle negative edges, preparing you to implement these concepts effectively in Java.

Shortest path tree

Previously, we discussed several algorithms to find the shortest paths from a single source vertex ss to every other vertex of the graph by constructing the shortest path tree rooted at ss. The shortest path tree specifies two pieces of information for each node vv in the graph:

  • dist(v)dist(v) is the length of the shortest path from ss to vv.
  • pred(v)pred(v) is the second-to-last vertex in the shortest path from ss to vv.

Now, we consider the more general all-pairs shortest path problem, which asks for the shortest path from every possible source to every possible destination. For every pair of vertices uu and vv, we want to compute the following information:

  • dist(u,v)dist(u, v) is the length of the shortest path from uu to vv.

  • pred(u,v)pred(u, v) is the second-to-last vertex on the shortest path from uu to vv. These intuitive definitions exclude a few boundary cases, all of which we already saw previously.

  • If there is no path from uu to vv, then there is no shortest path from uu to vv; in this case, we define dist(u,v)=dist(u, v) = ∞ ...