Skip to content

79 self edges - #177

Open
eriknw wants to merge 1 commit into
python-graphblas:mainfrom
eriknw:79-self-edges
Open

eriknw wants to merge 1 commit into
python-graphblas:mainfrom
eriknw:79-self-edges

Conversation

@eriknw

@eriknw eriknw commented Dec 4, 2026

Copy link
Copy Markdown
Member

A DiGraph whose cache held total_degrees+ said it had no self-loops when it had one. Given that key alone, has_self_edges asked whether the total degrees sum to more than twice the number of edges, which they never do: a self-loop counts once in its row and once in its column, as any other edge does. The False then spread, as it is meant to when true: offdiag became A itself and each - property its +. On nx.DiGraph([(0, 1), (1, 2), (2, 0), (2, 2), (0, 3), (3, 0)]), nx.degree_centrality and then nx.reciprocity through the backend gave 0.5 where NetworkX gives 1/3 (in dev and 3.6.1; 1/3 now in dev, 3.7 and 3.6.1). It has been so since #8.

  • The fix. With total_degrees+ alone, the answer now comes from the diagonal, as it does when the cache holds nothing that tells. The cases for the row and column degrees looked under row_degrees+/- and column_degrees+/-, names get_property resolves to count_rowwise+/- and count_columnwise+/- before the cache is read, so they never ran; they now look under the names the cache keeps, and answer without reading the diagonal. Every other inference in the function holds, as does Graph's, which reads only L+/-, U+/-, offdiag and a negative diagonal, and so do those in the getters that settle has_self_edges on the way (L+/- and U+/-, the degree getters and the reductions).
  • Tests. On seven small graphs (a loop in a cycle, none, a node with only a loop, a loop alone, a negative loop, a loop on every node, no edges), with any one or two of 11 cached properties for a Graph and 19 for a DiGraph, each value from a graph of its own: has_self_edges and offdiag are right when asked directly, and after any of those properties is then asked, which must also be right. A DiGraph's row, column and total degrees answer without reading the diagonal. After degree_centrality, has_self_edges and offdiag are right on both classes, and reciprocity, overall and per node, is NetworkX's. Random orders of 24 properties and degree_centrality on a Graph, and of 35 properties and 5 algorithms on a DiGraph, each on 300 random graphs of up to 6 nodes, with and without loops, give a fresh graph's answer at every step. On main, 43 of the 79 cases fail in dev, 3.7 and 3.6.1, every one on a DiGraph.
  • Fill orders at scale. In dev, 3.7 and 3.6.1, 3000 random fill orders per class (about 16000 calls each, of properties, masked reductions and the same algorithms, leaving out the four failures below) find 312 answers on a DiGraph that differ from a fresh graph's on main and none after; on a Graph, none either way. In dev, on three 3000-node graphs with a loop in one, 3000 orders find 290 on a DiGraph on main and 61 after; the 61 are recip_degrees+ built from recip_degrees- and the diagonal as FP64 where a fresh graph has INT64, with the same values. Compared by value, 236 on main and none after.
  • Mutants. Of 42 mutants of the function, of Graph's, and of the getters that set has_self_edges, 40 fail these tests in dev, 3.7 and 3.6.1, among them the old total_degrees+ case and one that takes total_degrees+ alone for a loop, the row or column degrees alone under the old names, each degree case with its comparison loosened or turned around, and each getter's comparison loosened. The other two are equivalent: the row and the column pair looked up under the old names, where the single-key case below them gives the same answer, also without the diagonal.
  • Merges. A merge with 20 wiener efficiency #121 is clean, and these tests pass on it.
  • Not here. The fill orders show four failures that do not depend on self-loops, left out of the tests above: count_element+/- read from a cached count_rowwise+/- counts the rows, not the edges; iso_value raises KeyError after is_iso is cached as False; has_negative_diagonal takes a missing min_element- for 0, so a negative weight off the diagonal can make it, and with it has_self_edges, True; and a - scalar reduction read from cached L- and U- calls Matrix.reduce, which does not exist. Also, the clustering of a single node looks for degrees+/- in the cache, which keeps them as count_rowwise+/-, so it never reuses them; that costs time, not answers.

A DiGraph's has_self_edges, given only "total_degrees+" in the cache,
said there were self-loops if those degrees summed to more than twice
the number of edges. They never do: a self-loop counts once in its row
and once in its column, as any other edge does. So a DiGraph with a
self-loop that had been asked for its degree centrality then said it
had none, took the matrix itself as its off-diagonal part, and gave a
reciprocity of 0.5 where NetworkX gives 1/3. That case now reads the
diagonal.

The cases for the row and column degrees looked under "row_degrees"
and "column_degrees", which are only aliases: the cache keeps them as
"count_rowwise" and "count_columnwise", so those cases never ran. They
now read the keys the cache holds.
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.35%. Comparing base (35dbc90) to head (6ad0db0).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #177      +/-   ##
==========================================
+ Coverage   76.82%   81.35%   +4.53%     
==========================================
  Files         118      121       +3     
  Lines        4271     4553     +282     
  Branches      818      750      -68     
==========================================
+ Hits         3281     3704     +423     
+ Misses        751      657      -94     
+ Partials      239      192      -47     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This was referenced Dec 4, 2026
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