Repository navigation
Conversation
NetworkX's Bellman-Ford relaxes an edge only where the length through it is less than the length its head has, which through a NaN or +inf weight it never is, so such an edge reaches nothing. Here it was summed, and every node past it got a NaN or inf length. The Bellman-Ford names now drop those edges. They, and the average length by Bellman-Ford, still serve negative weights. NetworkX's Dijkstra, behind eccentricity and its kin, closeness, harmonic, the Wiener indices, ego_graph with a distance and the average length by Dijkstra, meets a NaN in the order it pops its heap, and it takes no weight to be negative: given one, whether it raises "Contradictory paths found", and the lengths it gives when it does not, follow that order too. Those names gave Bellman-Ford's lengths for a negative weight, so a call that fails in NetworkX was answered. They now leave NaN and negative weights to NetworkX, by facts cached on the conversion, and the handlers that gave a negative cycle NetworkX's Dijkstra error are removed, as nothing reaches them. Floyd-Warshall keeps NaN entries, so its calls with a NaN go to NetworkX too, as does any call here with a -inf weight, where the check for a negative cycle misses cycles NetworkX finds. On an undirected graph the conversion adds a self-loop's weight in twice and takes it out once, so a loop of -inf or +inf holds NaN, as a NaN loop does. Such a NaN on the diagonal counts as -inf there, and the call goes to NetworkX. Float weights so heavy that a sum may pass the largest float go to NetworkX too: at -inf the lengths stop falling before the check for a negative cycle, and the walk back along the parents could run without end; at inf a node NetworkX leaves unreached got a length. The walk now stops after as many steps as there are nodes. A negative weight off the diagonal no longer counts as a negative self-loop, as it did once the sign of the weights was cached. The Floyd-Warshall names take the same bound, and where a weight is negative, the Bellman-Ford and Floyd-Warshall names also go to NetworkX when that bound reaches 2**53 times the lightest nonzero weight, as a sum that large can lose a weight and with it a negative cycle that NetworkX's rounding hides.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 32-float-path-lengths #132 +/- ##
=========================================================
+ Coverage 92.93% 93.28% +0.35%
=========================================================
Files 182 183 +1
Lines 11276 11795 +519
Branches 1275 1339 +64
=========================================================
+ Hits 10479 11003 +524
+ Misses 545 544 -1
+ Partials 252 248 -4 🚀 New features to boost your workflow:
|
eriknw
force-pushed
the
32-float-path-lengths
branch
from
December 2, 2026 20:40
faf0d71 to
28445c9
Compare
eriknw
force-pushed
the
34-nan-weights
branch
from
December 2, 2026 20:40
2dd7fee to
30e477d
Compare
eriknw
force-pushed
the
34-nan-weights
branch
from
December 2, 2026 23:20
30e477d to
87bc219
Compare
eriknw
force-pushed
the
34-nan-weights
branch
3 times, most recently
from
December 3, 2026 08:59
f4faf1b to
f9c1c17
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes the shortest-path names give NetworkX's answers when edge weights are NaN or infinite, or negative on the names NetworkX backs with Dijkstra, or leave the call to NetworkX. NetworkX's Bellman-Ford relaxes an edge only where the length through it is less than the length its head has, or less than inf for a head not yet reached. Through a NaN or +inf weight that is never true, so such an edge reaches nothing. On the directed path 0-1-2-3-4 with weights 1, NaN, 2 and 1.5, plus an edge 0-4 of weight 10, NetworkX's
single_source_bellman_ford_path_lengthfrom 0 gives{0: 0, 1: 1.0, 4: 10.0}. The backend added the NaN like any other weight and also gave nodes 2 and 3 a NaN length.single_source_bellman_ford_path_length,bellman_ford_path_length,bellman_ford_path,all_pairs_bellman_ford_path_lengthandnegative_edge_cycle. This is exact, because NetworkX never relaxes those edges, so the reached nodes, lengths and paths are now NetworkX's. The graph without them is cached on the conversion. A graph with no such weight is walked as before, and the check reads its values once per conversion.eccentricity,diameter,radius,center,periphery,harmonic_diameter,closeness_centrality,harmonic_centrality, the Wiener, Schultz, Gutman and hyper-Wiener indices,average_shortest_path_length(method None or"dijkstra"), andego_graphwith adistance. There a NaN length compares false with every other length, so the answer follows the order NetworkX pops its heap. On the path 0-1-2-3 with weights 1, NaN and 1, plus an edge 0-3 of weight 10, NetworkX gives node 1 an eccentricity of 11.0. With the same edges added in another order it gives 1.0. The backend gave 11.0 to every node. A +inf weight is still served, since Dijkstra adds it like any other weight and the answers match.wiener_indexandeccentricityraise that error, where the backend answered 30 and{0: 2, 1: 3, 2: 8, 3: 7}. The check reads two facts cached on the conversion, so a repeated call costs nothing more. The Bellman-Ford names, andaverage_shortest_path_lengthwithmethod="bellman-ford", still serve negative weights.n - 1rounds, misses cycles that NetworkX finds.negative_edge_cyclegave False where NetworkX gives True, andbellman_ford_pathcould run without end: the parents of two nodes pointed at each other, and the walk back from the target grew its list until memory ran out. At inf, NetworkX relaxes no edge and leaves the node unreached, where the backend gave it a length: on the directed path 0-1-2 with weights 1e308 and 1e308, NetworkX'sbellman_ford_path(G, 0, 2)raises NetworkXNoPath and the backend returned[0, 1, 2]. These names, andaverage_shortest_path_lengthwithmethod="bellman-ford", now go to NetworkX where n * n times the heaviest finite weight, doubled, reaches the largest float. (n - 1) times the heaviest weight bounds only a simple path. Where there is a negative cycle, NetworkX keeps walking around it until its heuristic or its count stops it, and it queues each node fewer than n times, so a length it adds has at most n * n - n + 1 edges, or n * n + n + 1 innegative_edge_cycle, which adds a node with an edge of weight 1 to every other. On 2254 random graphs of 3 to 12 nodes with a negative cycle, NetworkX added walks of up to 7.6 n edges (91 on 12 nodes) with the heuristic off and 4.5 n with it on, past n - 1 in 817 of 18786 single-source calls, and never past n * n. If such a sum reaches -inf before NetworkX finds the cycle, whether it then raises or gives -inf lengths follows the order it walks, so the bound has to hold for NetworkX's longest walk, not only for this backend's n edges. Doubling covers rounding, and the added node ofnegative_edge_cycle. The walk back along the parents now also stops, with RuntimeError, once it is longer than the nodes it can visit. The check reads the weights once per conversion.single_source_bellman_ford_path_lengthfrom 0 never shortens the length of 1 by the loop and gives{0: 0, 1: 1e300}, where the backend read the loop as a negative cycle and raised NetworkXUnbounded. These names,average_shortest_path_lengthwithmethod="bellman-ford"and the Floyd-Warshall names now go to NetworkX where the bound above, n * n times the heaviest finite weight, doubled, reaches 253 times the lightest nonzero one: no sum below the bound loses a weight that size. The ratio of the weights alone is not enough: after three edges of 252 a loop of -1 is lost too, as 3 * 252 - 1 rounds back to 3 * 252, though no weight is 253 times another. Without a negative weight a lost one moves a length only by rounding and makes no cycle negative, so such weights are still served: random weights spread from 1e-3 to 1e6 were never declined, on 10 to 10000 nodes; with a tenth of them negative they were declined from 3000 nodes (the bound passes 253 times 1e-3 at about 2100 nodes) and served up to 2000. A review found the same rule declines weights in [-1, 1] with some negative from about 1e5 nodes, and weights whose largest is 1e9 times the lightest from about 1e4. The lightest weight is read once per conversion, and only where a weight is negative. On 541 small graphs built to lose a weight (paths of 2**50 to 1e300 ending in a negative loop or cycle, and random mixes of such weights), the backend's answers differed from NetworkX's in 153 of 4869 calls before, all Bellman-Ford's, and in none after, in NetworkX dev and 3.7. In 3.6.1 the 111 left are Floyd-Warshall on a negative cycle, which 3.6.1 answers and 3.7 raises for, as the backend does.negative_edge_cyclesaid True for a graph with a self-loop of 2.0, a -1.0 edge and no negative cycle, on NetworkX's cached conversion. This was so before this change too. This fix has been needed since 2023; it is in this change rather than a separate one so the stack carries one rule for the cached signs.floyd_warshall,floyd_warshall_predecessor_and_distance,floyd_warshall_numpy) go to NetworkX for a NaN or infinite weight. NetworkX keeps NaN entries, and it gives a predecessor to an edge of weight inf but to no walk through one. Its NumPy version adds -inf to the inf of a missing edge and so fills NaN. They also go to NetworkX by the bound above for sums that may pass the largest float: on the path 0-1-2 with weights 1e308 and 1e308, NetworkX'sfloyd_warshall_predecessor_and_distancegives the pair (0, 2) no predecessor, as its sum is inf, where the backend gave one. On 208 graphs with such weights, that name differed from NetworkX's in 5 before and none after, in NetworkX dev and 3.7.average_shortest_path_lengthwithmethod="bellman-ford"goes to NetworkX for a NaN or infinite weight. NetworkX's sum skips the pairs those edges leave unconnected, but its check for a graph in pieces still counts the edges.all_pairs_bellman_ford_path_lengthnow checks its weights when it is called, not at the first item, so a declined call goes to NetworkX while NetworkX's dispatcher is still handling the call.all_pairs_dijkstra_path_length(30 dijkstra #133) leaves NaN and negative weights to NetworkX in the same way.clustering,average_clustering,modularity_matrix,directed_modularity_matrix,google_matrix,normalized_laplacian_matrix, andpagerankwith an infinite weight.Stacked on #129.