Skip to content

38 inf self-loops - #143

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:38-inf-self-loops
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:38-inf-self-loops

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026

Copy link
Copy Markdown
Member

Store each undirected self-loop's own weight in a conversion. NetworkX's to_scipy_sparse_array adds an undirected graph's edges in both directions and then each self-loop's weight negated, so a self-loop weighing w was stored as w + w - w: NaN for inf or -inf, and inf where w + w overflows, as for 1e308. NetworkX 3.6.1, 3.7 and dev all build the matrix this way, while NetworkX's own loops read w.

  • Now as NetworkX answers with such a self-loop, on graphs, subgraph views and to_undirected views: volume, conductance and normalized_cut_size; weighted clustering and average_clustering; google_matrix; with a -inf self-loop, eccentricity, center, periphery, diameter, radius, closeness_centrality, harmonic_centrality, harmonic_diameter, average_shortest_path_length, wiener_index, hyper_wiener_index, schultz_index, gutman_index, the three Floyd-Warshall names, all_pairs_bellman_ford_path_length and negative_edge_cycle; and the edge data that compose, union, disjoint_union and ego_graph carry.
  • Declined where w + w - w is not w in the type NetworkX sums in (adjacency_matrix's dtype if given, else the weight's own): adjacency_matrix, laplacian_matrix, normalized_laplacian_matrix, modularity_matrix, pagerank and hits. NetworkX builds their answers from to_scipy_sparse_array itself, so it answers them. A NaN self-loop stays NaN, and a directed graph's matrix is not summed, so both are still served.
  • A cached conversion holds each self-loop's own weight, so the check reads its diagonal and keeps the answer with it, and a repeat call costs what it did: a cached adjacency_matrix of a random graph of 200,000 nodes and 1,000,000 edges takes 2.9 ms, against 2.8 ms before. With no conversion yet, the graph's self-loops are read, 20 ms there (61 ms with a self-loop on every node), beside a conversion that takes far longer. A conversion that goes stale on a view goes with its answer.
  • The conversion of an undirected weighted graph reads its self-loops once more, one pass over the nodes.
  • With this branch alone, two calls that matched NetworkX only because the NaN diagonal was skipped now raise NetworkXUnbounded: bellman_ford_path from a node with a -inf self-loop, and, on NetworkX 3.6.1, floyd_warshall_numpy with a -inf self-loop. 34 nan, infinite and negative weights #132 fixes both.
  • Not changed here, since they come from the kernels or from NetworkX: with a -inf self-loop NetworkX's single_source_bellman_ford_path_length and bellman_ford_path_length give -inf lengths, and its bellman_ford_path a path, where these raise NetworkXUnbounded; a NaN self-loop in google_matrix, modularity_matrix and, on a to_undirected view, weighted clustering; eigenvector_centrality with a self-loop of 1e308; and NetworkX 3.6.1's Floyd-Warshall names, which read any self-loop otherwise than 3.7 does, on directed graphs too.

Stacked on #121.

NetworkX's to_scipy_sparse_array adds an undirected graph's edges in
both directions and then each self-loop's weight negated, so a
self-loop weighing w was stored as w + w - w: NaN for inf or -inf, and
inf where w + w overflows. NetworkX's own loops read w, so the names
that read a self-loop's weight answered otherwise: volume and the cut
sizes, weighted clustering, google_matrix, the distance names with a
-inf self-loop (eccentricity and its kin, closeness, the Wiener
indices, Floyd-Warshall, all-pairs Bellman-Ford, negative_edge_cycle),
and the edge data that compose, union, disjoint_union and ego_graph
carry. The conversion now writes each self-loop's weight back.

NetworkX answers adjacency_matrix, laplacian_matrix,
normalized_laplacian_matrix, modularity_matrix, pagerank and hits from
to_scipy_sparse_array itself, so where w + w - w is not w in the type
it sums in they are declined and NetworkX answers them. A cached
conversion answers the check from its diagonal and keeps the answer,
so a repeat call does not read the self-loops again.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.26168% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.97%. Comparing base (5b54ace) to head (c48f081).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #143      +/-   ##
========================================================
+ Coverage                 92.88%   92.97%   +0.08%     
========================================================
  Files                       182      183       +1     
  Lines                     11227    11441     +214     
  Branches                   1274     1305      +31     
========================================================
+ Hits                      10428    10637     +209     
- Misses                      546      549       +3     
- Partials                    253      255       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant