Repository navigation
Conversation
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 Report❌ Patch coverage is 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:
|
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.
Store each undirected self-loop's own weight in a conversion. NetworkX's
to_scipy_sparse_arrayadds an undirected graph's edges in both directions and then each self-loop's weight negated, so a self-loop weighingwwas stored asw + w - w: NaN for inf or -inf, and inf wherew + woverflows, as for 1e308. NetworkX 3.6.1, 3.7 and dev all build the matrix this way, while NetworkX's own loops readw.to_undirectedviews:volume,conductanceandnormalized_cut_size; weightedclusteringandaverage_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_lengthandnegative_edge_cycle; and the edge data thatcompose,union,disjoint_unionandego_graphcarry.w + w - wis notwin the type NetworkX sums in (adjacency_matrix'sdtypeif given, else the weight's own):adjacency_matrix,laplacian_matrix,normalized_laplacian_matrix,modularity_matrix,pagerankandhits. NetworkX builds their answers fromto_scipy_sparse_arrayitself, so it answers them. A NaN self-loop stays NaN, and a directed graph's matrix is not summed, so both are still served.adjacency_matrixof 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.NetworkXUnbounded:bellman_ford_pathfrom a node with a -inf self-loop, and, on NetworkX 3.6.1,floyd_warshall_numpywith a -inf self-loop. 34 nan, infinite and negative weights #132 fixes both.single_source_bellman_ford_path_lengthandbellman_ford_path_lengthgive -inf lengths, and itsbellman_ford_patha path, where these raiseNetworkXUnbounded; a NaN self-loop ingoogle_matrix,modularity_matrixand, on ato_undirectedview, weightedclustering;eigenvector_centralitywith 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.