Negative-length shortest paths and all-pairs shortest paths
Overview
Kent Quanrud develops a dynamic-programming approach to shortest paths with negative edge lengths, first bounding walks by their number of edges and then using the resulting Bellman–Ford-style relaxation to identify negative-infinity distances. For all-pairs shortest paths, he compares a doubling-based recurrence running in O(n³ log n) with Floyd–Warshall’s O(n³) recurrence, and previews Johnson’s algorithm as a way to handle negative edges more efficiently.
Key takeaways
- Dijkstra’s greedy ordering fails with negative edges because a vertex that appears close can later receive a shorter route through an edge with negative length.
- If a source-to-destination distance is finite, a shortest walk is attained by a path; a cycle in a shortest walk either can be removed without increasing length or can be repeated to force negative infinity.
- The bounded-edge recurrence computes shortest walks in O(k(m+n)) time, and n−1 edge rounds suffice for finite distances because a shortest path has at most n−1 edges.
- Vertices improved on round n have negative-infinity distance; a graph search from that improvement set identifies every destination with negative-infinity distance.
- Doubling the permitted edge count yields an all-pairs algorithm in O(n³ log n), while Floyd–Warshall’s intermediate-vertex recurrence improves this to O(n³).
- Adding a zero-length super-source edge to every vertex lets the single-source relaxation detect negative cycles across the graph.
Chapters
- The opening grid example allows repeated walks and illustrates how negative edge lengths can produce a best total below zero.
- Dijkstra’s algorithm can finalize a vertex too early: a later route through a negative edge may lower its distance.
- The failure does not require a negative cycle; the greedy closest-to-furthest ordering itself is no longer reliable.
- A reachable negative cycle can be traversed repeatedly, making the infimum walk length from a source to a destination negative infinity.
- With negative edges, shortest walks and shortest paths need to be distinguished because a walk may repeat vertices.
- Algorithms must account for both finite shortest distances and destinations whose distances are negative infinity.
- Quanrud proves that, when s can reach t, a finite distance from s to t is attained by a path.
- Among shortest walks, choose one with the fewest edges; if it contains a nonnegative cycle, removing that cycle does not increase its length and reduces its edge count.
- If the walk contains a negative cycle, repeating the cycle produces a walk shorter than the supposed finite minimum, a contradiction.
- One tempting fix is to let a Dijkstra-like process revisit vertices whenever a better distance appears.
- A chain of triangles with edge lengths near 1 and -1 can reveal improvements one at a time, forcing repeated scans of the graph.
- This motivates an algorithm with a clear progress measure rather than relying on eventual self-correction.
- Define a subproblem as finding the shortest walk from a fixed source using at most k edges.
- For k = 0, the distance is 0 when the destination is the source and positive infinity otherwise.
- Increasing k by one gives a systematic recurrence even when edge lengths are negative.
- For each destination v, either retain its best distance using at most i−1 edges or enter v from an in-neighbor w.
- The recurrence takes the minimum of the previous value and the best distance to w using at most i−1 edges plus the length of edge (w,v).
- Considering all incoming edges handles the final step of every candidate walk without assuming distances are processed in order.
- With dynamic programming, each round processes all vertices and incoming edges, giving O(k(m+n)) time, conventionally written O(km) when m ≥ n.
- If there are no negative-infinity distances, a shortest walk can be represented by a path.
- A path on n vertices uses at most n−1 edges, so n−1 rounds suffice to obtain every finite shortest distance.
- Let A_k contain the vertices whose best distance improves when the edge limit increases from k−1 to k.
- An improvement at round n cannot be the final finite answer: a finite shortest path would already have been found within n−1 edges.
- Therefore, every vertex improved in round n has distance negative infinity.
- If a vertex improves in round k+1, at least one of its in-neighbors must have improved in round k.
- Otherwise, the recurrence would use unchanged in-neighbor values and could not produce a new improvement.
- Consequently, later-improving vertices are reachable from the round-n improvement set.
- Every vertex reachable from a vertex in A_n also has distance negative infinity: the improving source can be made arbitrarily cheap before taking the finite route onward.
- Combined with the improvement-propagation argument, this shows that the negative-infinity destinations are exactly the vertices reachable from A_n.
- Run DFS or BFS from all vertices in A_n to classify the full set.
- Assume the graph has no negative cycles, so all-pairs shortest-path distances are finite whenever the corresponding vertices are reachable.
- A negative cycle can be detected by adding a super-source with zero-length edges to every vertex and applying the preceding relaxation method.
- Running the single-source O(mn) method from each of n sources gives an O(mn²) all-pairs baseline.
- Make both source s and destination t parameters in the shortest-walk subproblem, alongside the edge limit i.
- Use the same recurrence: retain the best value for at most i−1 edges or add one incoming edge to t.
- Summing across n sources and n rounds gives O(mn²) time, with the edge scans accounting for m.
- Compute shortest walks with limits 1, 2, 4, 8, and so on, rather than processing every edge count up to n.
- For a 2^i-edge limit, test every midpoint u and combine the best s-to-u and u-to-t walks using at most 2^(i−1) edges.
- There are O(log n) limits, n² source-destination pairs, and n midpoint choices, for O(n³ log n) time.
- Number the vertices 1 through n and define a subproblem by the endpoints i,j and the largest allowed intermediate-vertex index k.
- At k = 0, the answer is 0 for i = j, the edge length for a direct edge from i to j, and positive infinity when neither option exists.
- This formulation builds toward allowing every vertex as an intermediate point, without bounding the number of edges in a walk.
- For each k, either exclude vertex k as an intermediate or route through it: D(i,j,k) = min(D(i,j,k−1), D(i,k,k−1) + D(k,j,k−1)).
- The recurrence takes constant time per subproblem and has n choices each for i, j, and k, yielding O(n³) time.
- Quanrud closes by previewing Johnson’s algorithm: reweight negative edges so Dijkstra can be run from each source, a technique explored in the homework.
Transcript excerpt
0:00 All right. Okay. So, another graph lecture uh negative edge lengths and and all paired shortest paths. So, we've been doing graphs uh for the last couple of lectures. So, a week ago we did and I guess you've now looked at it for your homework. We did kind of first search and strongly connected components, topological sort, things like this. Uh, one thing to note, you haven't had a chance to do it in PSO yet. So that hopefully will help because right now you're in slight no man's land with the homework because you haven't done the PSO yet. So hopefully that'll be helpful tomorrow. Um, but any case, so we did uh strongly connected component desk first search and last time we did kind of shortest path type problems. The simplest example is kind of unweighted
0:49 where you can just do breast first search and something implicitly similar also works for weighted graphs. You know, something like Dyer's algorithm isn't really constructing the layers by distance, but still working in terms of closest to furthest from S. And if you remember, that's how we actually prove it's correct. If you get the first five vertices right, then you can get the sixth one right, and then you can get the seventh one right. And that's kind of important for trying to clearly articulate why it's correct. Okay, so today, what are we doing today? Today we're dealing with uh with negative edges. Okay, so there are some negative numbers in this graph and and otherwise uh you're just trying to get from s to t and you want to add things up to be as small as possible which could be a negative number. Um and uh yeah, for
1:39 this graph I always forget the answer to
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Kent Quanrud.