Repository navigation
Conversation
NetworkX adds integer weights as Python ints, exactly. Here they are added in 64 bits, which wrap past 2**63, and a matrix holds every weight in the one dtype numpy infers for them all, so an int past 2**53 beside a float, or an np.uint64 beside a signed int, is held as a float and rounds: 2**53 - 1 and np.uint64(4) add to 2**53 + 4. Where a sum could reach 2**63 the kernels raise IntegerOverflowError, an OverflowError that is also a NotImplementedError, so NetworkX's dispatcher tries the next backend: under backend_priority NetworkX answers for a NetworkX graph, and for one of this backend's graphs when fallback_to_nx is on. With backend="graphblas" NetworkX raises its own NotImplementedError from it, and a direct call gets an OverflowError. NetworkX answers with the arguments it was given, so a one-shot iterator of nodes, read before the sums are bounded, would reach it spent: cut_size, volume, average_degree_connectivity, degree_pearson_correlation_coefficient and eccentricity leave a weighted call with one to NetworkX. all_pairs_bellman_ford_path_length and node_degree_xy bound their sums when called, while NetworkX can still answer, rather than when the first item is read. A conversion that holds ints as floats is kept and marked. The names that add the weights as exact ints, compare them, or give them back decline at their call, on that conversion or one NetworkX serves from its cache, and NetworkX answers; a name NetworkX declares as reading weights is one of them unless it is listed with a float answer. Those names, such as pagerank, NetworkX computes in floats too, or from exact sums that a float sum of weights of one sign matches to a few parts in 2**53. They are served unless the weights can cancel in a float sum: ints held as floats of both signs, or ints of both signs large enough that their sums pass 2**53, which they add as floats too. The conversion records which, so a call reads one attribute. An int below 2**53 beside a float is served, as NetworkX adds it in floats too. The additional_docs notes say which weights are left to NetworkX.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 22-assortativity #126 +/- ##
====================================================
+ Coverage 93.24% 93.33% +0.08%
====================================================
Files 192 193 +1
Lines 12022 12196 +174
Branches 1395 1413 +18
====================================================
+ Hits 11210 11383 +173
- Misses 553 554 +1
Partials 259 259 🚀 New features to boost your workflow:
|
This was referenced Dec 2, 2026
Open
Open
Closed
eriknw
force-pushed
the
22-assortativity
branch
from
December 2, 2026 20:52
4f35ea8 to
b2fd013
Compare
eriknw
force-pushed
the
26-overflow-fallback
branch
from
December 3, 2026 00:04
9e518bb to
4dca4dc
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.
Let NetworkX answer the calls with integer weights that this backend would answer inexactly: where a 64-bit sum could reach 2**63, and where the conversion holds integer weights as floats and the answer is an integer sum, a comparison of weights or the weights themselves. NetworkX's dispatcher tries the next backend only after
NotImplementedError, so the overflow checks now raiseIntegerOverflowError, anOverflowErrorthat is also aNotImplementedError, and the names that need such ints exact decline withNotImplementedError.backend_priority, NetworkX answers exactly, with Python integers, for a NetworkX graph whateverfallback_to_nxsays, and for this backend's graphs whenfallback_to_nxis on (off, NetworkX raises its ownNotImplementedError)backend="graphblas", NetworkX raises its ownNotImplementedErrorfrom theIntegerOverflowError, where it raisedOverflowErrorOverflowErrorcut_size,volume,average_degree_connectivity,degree_pearson_correlation_coefficientoreccentricityis left to NetworkX, which would otherwise get it already readall_pairs_bellman_ford_path_lengthandnode_degree_xycheck their bounds when called rather than at the first item, so NetworkX can still answer, and so can NetworkX's own degree mixing names, which readnode_degree_xynp.uint64beside a signed int, or 263 and up beside a smaller int), or an int past 253 beside a float. The conversion is kept and marked. The names that add the weights as exact ints, compare them, or give them back decline at their call, before any argument is read, on that conversion or one NetworkX serves from its cache, and NetworkX answers exactly: the path lengths,cut_size,volume,mutual_weight, the Wiener indices, betweenness,edge_boundarywithdata,degree_mixing_matrix,average_degree_connectivityand the rest. Before this change, a call that only read the weights was served and answered a rounded float.pagerank,hits, eigenvector and Katz centrality,clustering, closeness and harmonic centrality,average_shortest_path_length,conductanceand the expansions,modularity, the degree assortativity coefficients,average_neighbor_degreeand the matrix names. NetworkX computes these in floats too, or from exact sums; a float sum of weights of one sign is within a few parts in 253 of the exact one. The tests call each of the 27 on such weights of either sign: 26 are served and within 1e-9 of NetworkX's largest value (hitsiterates to a tolerance; the others agree far closer), andpagerank,closeness_centralityandconductanceare compared on smaller cases at a relative 1e-12.group_closeness_centralitystill declines weights like these, as it is unweighted here. Where the int degrees pass 231, NetworkX'sdegree_assortativity_coefficientsquares them in int64, which wraps, so there it is compared with Pearson's r of the pairs, as NetworkX'sdegree_pearson_correlation_coefficientgives it. Floats are within tolerance, not NetworkX's bits.edge_expansionwould be 0.0 for 4/3, andconductanceandnormalized_cut_sizewould divide by 0. The float-answer names decline weights whose float sums can cancel, and NetworkX answers: ints held as floats of both signs, and ints held as ints of both signs whose absolute values sum past 252, which these names add as floats too. Declined,degree_pearson_correlation_coefficientis answered by NetworkX from this backend'snode_degree_xy, the same exact pairs in another order, so it may differ from NetworkX's alone in its last bits. A sum of degrees counts a weight at most twice, so within that bound every float sum of the weights is exact; past it, one int past 253 or many smaller ones can round (252 + 1 added three times as floats is 3 * 252 + 4). Ints held as ints were answered wrongly this way before this change: with weights near +-260, or near +-253 and each within it,edge_expansion,mixing_expansion,conductance,normalized_cut_size,modularity,overlapping_modularity, closeness and harmonic centrality,harmonic_diameterandhitsanswered off,conductanceandnormalized_cut_sizedivided by 0, andpagerankfailed to converge where NetworkX did not; with every weight within 2**52 + 1,normalized_cut_sizesummed a volume of 9 as 8. Ints of one sign, of any size, and ints of both signs within the bound stay served. The conversion records this from the matrix's least and greatest values and, for ints of both signs, their absolute sum, so a call only reads it.average_shortest_path_length, closeness and harmonic centrality,harmonic_diameter) on positive ints past 2**53 beside floats, where NetworkX's Bellman-Ford and Dijkstra, comparing ints with floats, can raiseNetworkXUnboundedor "Contradictory paths found"; and annp.uint64beside negative ints, where NetworkX raises numpy'sOverflowError("Python integer -2 out of bounds for uint64"), now only when everynp.uint64there is 0, since otherwise the weights are of both signs and declined.np.uint64(2**63)beside an int, is still left to NetworkX, before conversion.pagerank9.0 ms against NetworkX's 15.3 ms,average_neighbor_degree9.0 against 16.7,clustering9.9 against 74.6,closeness_centralityof one node 10.3 against 14.8. Medians of five interleaved rounds. Indicative, load 6 to 10. Reading the bounds of int weights at conversion, and their absolute sum where they have both signs, costs nothing measurable:pagerankandlaplacian_matrixon a 20000-node, 100000-edge graph with int weights of one sign and of both, conversion included, within 1% of before (Indicative, load 4 to 7).Stacked on #122.