Dynamic Programming
Explore dynamic programming approaches to solve the all-pairs shortest path problem in C++. Understand how to apply recurrences, use the Shimbel and Bellman-Ford algorithms, and efficiently compute shortest paths in weighted graphs without negative cycles.
We'll cover the following...
Dynamic approach for the all-pairs shortest path
We can also solve the all-pairs shortest path problem directly using dynamic programming instead of invoking a single-source algorithm. For dense graphs, where , the dynamic programming approach eventually yields an algorithm that is both simpler and (slightly) faster than Johnson’s algorithm. For the rest of this course, we’ll assume that the input graph contains no negative cycles.
As usual, for dynamic programming algorithms, we first need a recurrence. Just as in the single-source setting, the obvious recursive definition
only works when the input graph is a dag; any directed cycles drive the recurrence into an infinite loop.
We can break this infinite loop by introducing an additional parameter, exactly as we did for Bellman-Ford; let denote the length of the shortest path from to that uses at most edges. The shortest path between any two vertices traverses at most edges, so the true shortest-path distance is ...