Skip to content

53 rich club - #157

Open
eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:53-rich-club
Open

eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:53-rich-club

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add rich_club_coefficient, answered for normalized=False. The coefficient at degree k is 2 * E_k / (N_k * (N_k - 1)), where N_k is the number of nodes of degree above k and E_k the number of edges among them. NetworkX's default, normalized=True, divides each value by the coefficient of a copy rewired with random double-edge swaps drawn from seed. That is left to NetworkX, and so is any call with a seed, so only calls that pass normalized=False explicitly, without a seed, are answered here. A declined call goes to NetworkX under backend priority.

  • Keys: 0, 1, 2, ... for as long as N_k is at least 2, as NetworkX gives them. They include degrees that no node has, and stop before the first k with fewer than two nodes above it. A graph with no edges, or no nodes, gives {}.
  • Kernel: an edge stays among the nodes of degree above k while k is below the lower degree of its two ends. A histogram of the degrees and one of each edge's lower end degree then give every N_k and E_k by suffix sums, in time linear in the edges, with no n^2 step. Edge weights are not read.
  • Values: 2 * E_k and N_k * (N_k - 1) are integers, held exactly in float64 for graphs under 94 million nodes, so each quotient is the correctly rounded one. NetworkX divides Python ints, which is correctly rounded too, so the values are the same floats, not only close. On 510 random graphs (13903 keys) in NetworkX 3.6.1, 3.7 and the development branch, keys and values were all equal.
  • Any false normalized, such as 0 or None, is answered, since NetworkX tests it with if normalized. Q and 3.7's n_samples change nothing without normalization, as in NetworkX, where n_samples=0 is refused only with normalized=True.
  • A seed changes nothing without normalization either, but NetworkX 3.7 and later check it all the same: seed="junk", 1.5 or [1] raise ValueError there, and 3.6 does not check it. A call with a seed is declined, so that each NetworkX checks its own.
  • n_samples, which NetworkX added in 3.7, joins the newer keywords of 61, which are declined where the installed NetworkX lacks them. On 3.6 a call with it was answered here, where NetworkX's own function raises TypeError for it, and now NetworkX raises that error. Its row is added to that rule's test.
  • Errors are NetworkX's: directed graphs and multigraphs raise NetworkXNotImplemented in NetworkX before dispatch, and a directed graph of this backend raises the same. A self-loop raises NetworkX's plain Exception, "rich_club_coefficient is not implemented for graphs with self loops.".
  • Speed (indicative; median of nine interleaved rounds on a shared machine): at 1e5 edges NetworkX took 96 ms on G(n, m) and 102 ms on a Barabasi-Albert graph, and this took 52 and 50 ms including conversion, about 1 ms with the conversion cached. At 1e6 edges: 1.57 s and 1.58 s against 0.68 s and 0.72 s with conversion, 8.5 ms cached. At 1e4 edges it was 6.5 ms against 4.9 ms with conversion. It wins with conversion at each size, so no hook holds small graphs back.

Stacked on #156.

The coefficient at degree k is 2 * E_k / (N_k * (N_k - 1)), where N_k
is the number of nodes of degree above k and E_k the number of edges
among them, for each k from 0 while N_k is at least 2, as NetworkX
gives it. An edge counts while k is below the lower degree of its two
ends, so histograms of the degrees and of each edge's lower end degree
give every N_k and E_k by suffix sums, in time linear in the edges.
The default, normalized=True, divides by the coefficients of graphs
rewired with random double-edge swaps and is left to NetworkX, so only
calls with normalized=False are answered here. A self-loop raises
NetworkX's own exception.

A seed is left to NetworkX too. It changes nothing without
normalization, but NetworkX 3.7 and later check it all the same and
raise ValueError for one that cannot seed a generator, where 3.6 does
not. On a NetworkX before 3.7, which has no n_samples, a call with it
is left to NetworkX, whose own function raises TypeError for it, as
for the other keywords newer than 3.6.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.41270% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.99%. Comparing base (83d8c57) to head (cb262b6).

Additional details and impacted files
@@                   Coverage Diff                   @@
##           61-generic-declines     #157      +/-   ##
=======================================================
+ Coverage                92.90%   92.99%   +0.09%     
=======================================================
  Files                      182      185       +3     
  Lines                    11294    11483     +189     
  Branches                  1281     1290       +9     
=======================================================
+ Hits                     10493    10679     +186     
- Misses                     547      549       +2     
- Partials                   254      255       +1     
🚀 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