A Bottom-Up Algorithm for Negative-Weight
SSSP with Integrated Negative Cycle Finding
Abstract
We present a simplified algorithm for solving the Negative-Weight Single-Source Shortest Paths (SSSP) problem, focusing on enhancing clarity and practicality over prior methods. Our algorithm uses graph diameter as a recursive parameter, offering greater robustness to the properties of the decomposed graph compared to earlier approaches. Additionally, we fully integrate negative-weight cycle finding into the algorithm by augmenting the Bellman-Ford/Dijkstra hybrid, eliminating the need for a separate cycle-finding procedure found in prior methods. Although the algorithm achieves no theoretical efficiency gains, it simplifies negative cycle finding and emphasizes design simplicity, making it more accessible for implementation and analysis. This work highlights the importance of robust parameterization and algorithmic simplicity in addressing the challenges of Negative-Weight SSSP.
1 Introduction
The Single-Source Shortest Paths (SSSP) problem with negative edge weights is a cornerstone of algorithmic graph theory, with applications spanning network optimization, logistics, and resource allocation. An important extension of this problem involves handling graphs that may contain negative-weight cycles, where the goal is to either return a shortest path tree or find a negative-weight cycle. Both outputs serve as verifiable certificates, ensuring correctness and reliability.
Scaling has been a foundational technique in shortest path algorithms since the 1990s [Gol95]. Initially, it was used to normalize edge weights, reducing the problem to one where all negative edges have weight exactly . More recently, scaling has been applied to transform cycle properties, ensuring that non-negative-weight cycles become significantly more positive. This transformation enables efficient computation of SSSP on the scaled graph. Algorithms such as those in the Bernstein-Nanongkai-Wulff-Nilsen [BNW22] framework have integrated scaling with advanced decomposition techniques, achieving near-linear time complexity for solving the Negative-Weight SSSP problem.
Building on this foundation, Bringmann, Cassis, and Fischer [BCF23] extended the existing framework, achieving a nearly 6-log-factor improvement over [BNW22]. Their work set a new standard for solving Negative-Weight SSSP, providing both theoretical advancements and practical insights.
In this paper, we build upon the techniques introduced by [BCF23], presenting a bottom-up algorithm inspired by [FHL+25] with the following contributions:
-
1.
Diameter-Based Decomposition Parameter: Our algorithm employs the diameter of the non-negative graph as a parameter in our recursive decomposition, similar to the bottom-up approach of [FHL+25]. Compared to [BNW22] and [BCF23]’s parameterization of the maximum number of negative-weight edges on any shortest path, this parameterization is more natural and, more importantly, avoids the issue of undetected failures in the presence of negative-weight cycles. In particular, we bypass the noisy binary search analysis of [BCF23], which they must perform to handle the case of undetected failures.
-
2.
Integrated Negative Cycle Finding: The diameter-based decomposition allows us to augment the Bellman-Ford/Dijkstra hybrid algorithm to look for negative-weight cycles on the fly. In other words, we fully integrate negative-weight cycle finding into our algorithm, instead of designing a separate procedure as in [BCF23].
Although our algorithm does not improve upon the theoretical efficiency of [BCF23], it emphasizes simplicity in design and implementation, making it an accessible and practical alternative for solving the Negative-Weight SSSP problem.
Theorem 1.
Consider a graph with integral edge weights that are at least . There is a Las Vegas algorithm that solves Negative-Weight SSSP in time with high probability.
1.1 Preliminaries
Let be a directed graph with integral weight function . A potential function is a function on the vertices used to reweight the graph: the reweighted graph satisfies for all edges . A result of Johnson [Joh77] reduces the Negative-Weight SSSP problem to computing a potential function such that all edge weights of are non-negative, after which Dijkstra’s algorithm can be run on to recover the single-source shortest paths in .
The iterative scaling technique of [BNW22] does not compute such a potential function in one go. Instead, if the graph has all edge weights at least , then the goal is to either compute a potential function such that has all edge weights at least , or output a negative-weight cycle; we call this problem Scale. [BNW22] show that Negative-Weight SSSP reduces to iterations of Scale when the original graph has all edge weights at least . Moreover, [BCF23] show that if each iteration of Scale runs in time in expectation, then Negative-Weight SSSP can be computed in time with high probability111With high probability means with probability at least for arbitrarily large constant . (and with Las Vegas guarantee) by restarting any runs of Scale that exceed time and applying a Chernoff bound. Hence, to obtain Theorem 1, we only focus on the task of solving Scale in expected time. It even suffices to solve Scale in this time with high probability, since the solution can be easily checked for correctness and Scale can be restarted if incorrect.
We need three main subroutines from [BNW22, BCF23]. The first is the Decompose subroutine introduced in [BNW22] and refined in [BCF23]. Define the in-ball/out-ball of radius centered at as the set of vertices reachable to/from by a path of weight at most , respectively. Given a graph , define the graph as the graph with all negative-weight edges reweighted to . We require the following guarantee from [BCF23]:
Lemma 2 (Lemma 8 of [BCF23]).
There is a randomized algorithm Decompose running in expected time that computes a set of positive-weight edges such that:
-
1.
Progress: With high probability, for any strongly connected component in , either (i) or (ii) for each vertex , both the in-ball and out-ball of radius centered at contain more than vertices.
-
2.
Sparse Hitting: For any (potentially non-simple) path of length at most in , the number of edges of inside (counting multiplicity) has expectation at most .
We remark that Lemma 8 of [BCF23] proves a different condition (ii) for progress that depends on a parameter that they introduce. However, our new condition (ii) also holds and is mentioned in the proof of their Lemma 22.
The second subroutine is a Bellman-Ford/Dijkstra hybrid algorithm for Negative-Weight SSSP, also introduced in [BNW22] and refined in [BCF23]. For convenience, we do not mention a source vertex in the input to BellmanFordDijkstra. Instead, the goal is to compute, for each vertex , the shortest path ending at (and starting from anywhere). Note that the shortest path has weight at most since the empty path at of weight is a possible choice. To frame this problem as SSSP, simply add a source vertex with zero-weight edges to each vertex in , and call SSSP on the new graph with source . For further convenience, we also integrate a potential function directly into BellmanFordDijkstra, which runs the Bellman-Ford/Dijkstra hybrid on instead (with the extension ), and quickly finds single-source shortest paths with few negative-weight edges in . These single-source shortest paths in correspond exactly to single-source shortest paths in , which in turn correspond to shortest paths in ending at each vertex.
In the version below, we also include the algorithm’s guarantees after each iteration to allow for early termination if a negative-weight cycle is detected. In addition, we also maintain the weights of our current shortest paths under a separate, auxiliary weight function . This can be implemented by recording the distance of each current path as a tuple , but only using the first coordinate for comparisons.
Lemma 3 (Lemmas 25 and 62 of [BCF23]).
Let be a directed graph and be a potential function. For each vertex , let be the minimum number of edges of negative weight in among all shortest paths in ending at . There is an algorithm BellmanFordDijkstra that computes a shortest path ending at each vertex in time .
The first iterations of the algorithm runs in time and computes, for each vertex , the minimum weight of a path from to among those that contains less than negative-weight edges. Moreover, given input auxiliary weights on the edges (independent of the weights that determine shortest paths), the algorithm can also compute the weight under of some such path .
The third subroutine is given a graph with its strongly connected components and a potential function, and fixes the DAG edges outside the SCCs while keeping the edge weights inside the SCCs non-negative.
Lemma 4 (Lemma 3.2 of [BNW22]).
Consider a graph with strongly connected components and a potential function , and suppose that all edges inside the SCCs have non-negative weight in . There is an time algorithm that adjusts so that all edge weights in are now non-negative.
2 The Scale Algorithm
Recall the specifications of the Scale problem: given an input graph with edge weights for all , either return a reweighted graph with weights at least or output a negative-weight cycle in . We present an algorithm that runs in expected time and succeeds with high probability, which is sufficient to prove Theorem 1.
2.1 Description
The algorithm operates in two main phases: (1) recursively decomposing a scaled version of the input graph, and (2) iteratively computing distances on the decomposition.
2.1.1 Phase 1: Recursive Decomposition
Let be the input graph. We define a scaled graph by increasing the weight of every edge by , i.e., for all edges .
The decomposition process uses the Decompose algorithm from [BCF23], which, given a graph and a diameter parameter , returns a set of edges whose removal ensures that the remaining strongly connected components (SCCs) are either small in size (condition (i) of Lemma 2) or have diameter at most (which we will deduce from condition (ii)). Starting with , we decompose recursively. For each subgraph with parameter , we compute the set of cut edges and remove these edges to obtain SCCs . For each SCC , the diameter parameter is set to if , or otherwise. If , we recursively decompose ; otherwise, becomes a leaf node.
Once the decomposition is complete, we check each leaf node for the presence of a negative edge . If such an edge exists, then we claim there must be a negative-weight cycle in . To find a negative-weight cycle, we run Dijkstra’s algorithm from in (where negative-weight edges in are replaced by edges of weight 0) to find a path to of weight at most in ; we will show that such a path must exist. Note that this path also has weight at most in , where edge weights are only smaller. The algorithm then outputs the cycle formed by concatenating this path with the edge , which is a negative-weight cycle since the edge has negative weight in and hence weight less than in .
At this point, if the algorithm does not terminate early with a negative-weight cycle, then all edges in leaf nodes are non-negative.
2.1.2 Phase 2: Iterative Distance Computation
In this phase, we iteratively compute distance estimates on the decomposition tree, starting from the leaves and moving up to the root. Initially, we set for all . For each node in the decomposition tree, we skip further computation if is a leaf, as all edges in are non-negative. For a non-leaf node , assume that we have already computed a such that all SCCs of have non-negative weights. Using Lemma 4, we adjust so that all edges of now have non-negative weights. We then run BellmanFordDijkstra on with potential function , and use as an auxiliary function to help detect a negative-weight cycle.
If BellmanFordDijkstra records a path with auxiliary weight more than , then we terminate BellmanFordDijkstra early and recover such a path with endpoints and . We then run Dijkstra’s algorithm from in to find a shortest path back to . We show that concatenating and produces a negative-weight cycle in .
After processing all nodes at the current level, we update and proceed to the next level. After processing all the levels, we guarantee that has non-negative weight edges everywhere.
2.2 Algorithm Pseudocode
2.3 Analysis
Our ultimate goal is to prove the following theorem.
Theorem 5.
There is an algorithm Scale such that, for any input graph with edge weights for all , Scale either returns a reweighted graph with edge weights at least , or outputs a negative-weight cycle in . The algorithm runs in expected time and succeeds with high probability.
To prove this theorem, we establish several intermediate results. Below, we say that a vertex subset has diameter at most in a graph if for any two vertices , there is a path in from to of weight at most . Throughout, we condition on the progress property of Lemma 2 succeeding for all decompositions, which occurs with high probability.
Claim 6.
Let be a non-leaf node in the decomposition tree, and let be a strongly connected component (SCC) of such that . Then, with high probability, has diameter at most in (and in ).
Proof.
Consider a SCC of with . Lemma 2 guarantees that, for every vertex , both the in-ball and out-ball of radius centered at in contain more than vertices. Let . Since both the out-ball of radius centered at and the in-ball of radius centered at each contain more than vertices, their intersection must be non-empty. Therefore, there exists such that there exists a path from to of length at most in , and a path from to of length at most in . By concatenating these two paths, we obtain a path from to of length at most in . Since , this result extends to . Therefore, the diameter of is at most in (and in ), as desired. ∎
Claim 7.
Let be any node in the decomposition tree with . Then, with high probability, has diameter at most in .
Proof.
Since is not the root, is a strongly connected component (SCC) of its parent . If , then by Claim 6, has diameter at most in , and hence in . Otherwise, its parent node has the same diameter parameter . By induction, has diameter at most in . Since is a subset of , it also has diameter at most in . Therefore, in both cases, has diameter at most in , as desired. ∎
Claim 8.
Let be any node in the decomposition tree with . Then, for all simple paths in with , we have .
Proof.
Let denote the total negative weight (i.e., sum of negative-weight edges) of in . For a simple path in with , each negative-weight edge in has weight at least , and contains at most edges. Therefore, . In , all negative-weight edges are replaced with edges of weight 0. Hence, the weight of in is given by . Substituting and , we find , as desired. ∎
Claim 9.
Let be a node in the decomposition tree with . With high probability, if there exists a path in such that but , then concatenating with a shortest path in from the end of back to its start produces a negative-weight cycle in . In particular, if has no negative-weight cycles, then for all (potentially non-simple) paths in with , we have .
Proof.
Suppose there exists a path in such that but . Let denote the total negative weight of in . The weight of in is given by . Since and , it follows that . For each negative-weight edge in , its weight in is , so the weight of in is . Substituting and , we find . By Claim 7, has diameter at most in , so the shortest path in from the end of back to its start satisfies . Since edge weights can only be smaller in , we also have . Combining and forms a cycle in with , so is a negative-weight cycle. ∎
Claim 10.
Let be a non-leaf node in the decomposition tree. Then, for any (potentially non-simple) path in with , the number of edges of inside (counting multiplicity) has expectation at most .
Proof.
Follows directly from Lemma 2. ∎
We now proceed to analyze the runtime and correctness of the Scale algorithm by considering whether contains a negative cycle. Below, let and denote the number of vertices and edges in .
Lemma 11.
Consider a node , and suppose does not contain a negative-weight cycle. Then, BellmanFordDijkstra runs in expected time .
Proof.
There are two cases to consider:
Case 1: For each vertex , there exists a shortest path to of weight at most in . In this case, the shortest path to with has an expected edges inside by Claim 10. Since has non-negative weight edges outside of , we have for the parameter defined in Lemma 3. By Lemma 3, BellmanFordDijkstra runs in expected time which has expectation since for all . Note that BellmanFordDijkstra can still terminate early and output a negative-weight cycle in , but that only speeds up the running time.
Case 2: For some vertex , every shortest path to has weight more than in . By Claim 9, this implies the existence of a negative-weight cycle in . Among all shortest paths to , let be one minimizing , which is still greater than . Take the longest prefix of with weight at most in . Since , the number of edges of inside has expectation by Claim 10. Let be the path concatenated with the next edge in , so that and the number of edges of inside still has expectation . Since is a prefix of shortest path , it is also a shortest path. Let be the endpoint of , and let be the number of iterations of BellmanFordDijkstra before , so that has expected value . After iterations, BellmanFordDijkstra finds a shortest path ending at with . Moreover, since otherwise, replacing the prefix of by results in a shortest path to of lower weight in , contradicting the assumption on . It follows that after an expected iterations of BellmanFordDijkstra, a path is found with and , and the algorithm terminates with a negative-weight cycle. The runtime is , which has expectation . ∎
Lemma 12.
Consider a node , and suppose contains a negative-weight cycle. Then, BellmanFordDijkstra runs in expected time .
Proof.
There are two cases to consider:
Case 1: contains a negative-weight cycle consisting entirely of non-positive-weight edges. Since Decompose only removes positive-weight edges, such a cycle remains intact throughout the decomposition and persists in one of the leaf nodes. A negative-weight edge from this cycle is found in this leaf node, and the algorithm computes a negative-weight cycle and terminates early. In particular, the node is never processed.
Case 2: All negative-weight cycles in include at least one positive-weight edge. This case is the most subtle and requires carefully defining a reference path .
Define . We first claim that any (potentially non-simple) path in of weight at most in must have weight exceeding in . Given such a path, we first remove any cycles on the path of non-negative weight in . The remaining path still has weight at most , so it must have at least edges, which means it can be decomposed into at least negative-weight cycles, each with at least one positive-weight edge in . It follows that the path has weight at least in , concluding the claim.
Among all paths of weight at most in , let be one with minimum possible weight in , which must be greater than . Take the longest prefix of with weight at most in . Since , the number of edges of inside has expectation by Claim 10. Let be the path concatenated with the next edge in , so that and the number of edges of inside still has expectation . Let be the endpoint of , and let be the number of iterations of BellmanFordDijkstra before , so that has expected value . After iterations, BellmanFordDijkstra finds a path ending at with . Moreover, since otherwise, replacing the prefix of by results in a path of lower weight in and of weight at most in , contradicting the assumption on . It follows that after an expected iterations of BellmanFordDijkstra, a path is found with and , and the algorithm terminates with a negative-weight cycle. The runtime is , which has expectation . ∎
Proof.
We first consider the decomposition phase. For each node , the decomposition takes expected time by Lemma 2. At each level of the decomposition tree, the total number of edges and vertices across all nodes is bounded by and , respectively, so the expected runtime per level is . With levels in the decomposition tree, the total expected runtime is .
For processing each node , Lemma 4 adjusts in time, and Lemmas 11 and 12 establish an expected runtime of for BellmanFordDijkstra. There may be an additional time to output a negative-weight cycle, but this only happens once. Again, summing over all nodes , the total expected runtime is .
To convert distances in into a reweighted graph with edge weights at least , we note that setting as the distances in makes non-negative. Since for all , it follows that , as desired. ∎
References
- [BCF23] Karl Bringmann, Alejandro Cassis, and Nick Fischer. Negative-weight single-source shortest paths in near-linear time: Now faster! In 2023 IEEE 64th Annual Symposium on Foundations of Computer Science (FOCS), pages 515–538. IEEE, 2023.
- [BNW22] Aaron Bernstein, Danupon Nanongkai, and Christian Wulff-Nilsen. Negative-weight single-source shortest paths in near-linear time. In 2022 IEEE 63rd annual symposium on foundations of computer science (FOCS), pages 600–611. IEEE, 2022.
- [FHL+25] Nick Fischer, Bernhard Haeupler, Rustam Latypov, Antti Roeyskoe, and Aurelio Sulser. A simple parallel algorithm with near-linear work for negative-weight single-source shortest paths. In Symposium on Simplicity in Algorithms (SOSA), 2025.
- [Gol95] Andrew V Goldberg. Scaling algorithms for the shortest paths problem. SIAM Journal on Computing, 24(3):494–504, 1995.
- [Joh77] Donald B Johnson. Efficient algorithms for shortest paths in sparse networks. Journal of the ACM (JACM), 24(1):1–13, 1977.