Skip to content

60 load centrality - #154

Open
eriknw wants to merge 1 commit into
python-graphblas:45-betweenness-kfrom
eriknw:60-load-centrality
Open

eriknw wants to merge 1 commit into
python-graphblas:45-betweenness-kfrom
eriknw:60-load-centrality

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add load_centrality (dispatched as newman_betweenness_centrality, the name NetworkX defines it under) and edge_load_centrality. NetworkX walks from each source in Python. Here a batch of sources walks the graph level by level, as betweenness does since #145, with each level holding its nodes' counts of predecessors; one multiply per level then passes the shares back up. Keys are NetworkX's exactly and values agree within a relative 1e-12. With the conversion counted, the backend took 1/16 to 1/150 of NetworkX's time on graphs where NetworkX took 0.9 to 62 s (Indicative).

  • Load. From each source, a node passes up 1 plus what it got from below, split equally among its predecessors (its neighbours, or in-neighbours, a level up), not by path counts as betweenness splits it. A node on level 1 passes nothing, as in NetworkX, which stops at the source. The sum over sources is scaled by 1 / ((n - 1) * (n - 2)) when normalized, on undirected graphs too, and not at all for two nodes or fewer, as NetworkX does.

  • v given. A float, 0.0 for a node that is not in the graph. A v that is not hashable raises NetworkX's TypeError, except on the null graph, where NetworkX never looks it up.

  • Edge load. The keys are (u, v) and (v, u) for every edge, on directed graphs too, and each self-loop once, as NetworkX's. From each source every key gets 1, and an edge from a node to its successor a level below gets 1 + U(w) / p(w) at both of its keys, where w is the lower end, p(w) counts its predecessors and U(w) sums the same over the edges below w. That is NetworkX's propagation written per level, so the values are symmetric. The pairs come by their first end in node order, then by their second, which may differ from NetworkX's order.

  • Cutoff, unweighted. nx.predecessor stops after the first level the cutoff is at most. So 2.5 walks 3 levels; any cutoff of 1 or less, negative ones and True included, walks one; and None, False, 0, NaN and inf walk them all. edge_load_centrality's default cutoff=False walks them all.

  • Weighted. NetworkX's Dijkstra predecessors follow its float sums and the order of its walk. They equal the levels', ties included, where every edge between two nodes weighs one positive, finite value, so that is what is served. All the weights must be of one type (int or NumPy int64, float or float64, or float32), each self-loop at least as heavy as an edge, and the lengths must keep rising to the cutoff. A length is the weight added once per level to the integer 0, as NetworkX adds it, and is kept when it is not past the cutoff. So with 0.7 on every edge a cutoff of 7 keeps ten levels (6.999999999999999). float32 weights add and compare as NumPy's float32 do. Left to NetworkX:

    • distinct weights, as in NetworkX's own test_weighted_load;
    • zero, negative, inf or NaN weights;
    • a lighter self-loop: at weight zero NetworkX lists the node among its own predecessors, and a negative one raises "Contradictory paths found";
    • weights of mixed types, such as an int beside a float or float32 beside float, whose sums then differ by path;
    • int32, float16, bool and Fraction weights, and ints past int64;
    • a weight that is not hashable or is a function;
    • a graph class whose adjacency is not _adj;
    • weights such as 1e308, whose sums reach inf and tie, unless the cutoff stops first.

    Int weights add as Python ints, which do not wrap past 2**63 where NetworkX's sums of NumPy int64s do; only a cutoff could tell.

  • Nodes that may not compare. NetworkX sorts each source's (length, node) pairs, so it compares nodes at one length. A graph of three or more nodes is served only where the nodes are all numbers, all strings, or tuples holding one of these kinds at each index. Others, such as 0 beside "a", are left to NetworkX, which raises TypeError for some and answers others. edge_load_centrality sorts by length alone and serves any nodes.

  • A cutoff that is not an int or float (Decimal, Fraction, str) is left to NetworkX.

  • Tolerance. Each value is a sum of positive terms. NetworkX adds them source by source in its walk's order, and this backend adds a batch at a time. NetworkX also adds 1 to each share a node passes up and takes it off at the end, which rounds off the last bits of a small share. The largest relative difference measured was:

    • 3.8e-16 on the 330 random test graphs;
    • 1.4e-14 on random graphs of 1000 to 3000 nodes;
    • 3.2e-13 on a 70 by 70 grid.

    The tests allow 1e-12.

  • Speed (Indicative, load 7 to 17; medians of three interleaved rounds). "cold" empties the conversion cache before each call. Every answer was checked against NetworkX's.

    graph name NetworkX cold cached
    gnm_random_graph(1000, 5000) node 0.93 s 0.027 s 0.023 s
    edge 3.0 s 0.032 s 0.030 s
    gnm_random_graph(3000, 15000) node 9.1 s 0.15 s 0.14 s
    edge 33 s 0.22 s 0.20 s
    directed gnm_random_graph(2000, 10000) node 3.6 s 0.078 s 0.074 s
    edge 12 s 0.13 s 0.12 s
    70 by 70 grid node 21 s 1.3 s 1.7 s
    edge 62 s 1.6 s 1.2 s

    So the backend took 1/16 to 1/150 of NetworkX's time with the conversion. The grid gains least: it has up to 138 levels below a source, a multiply each. A second run on the commit, at load 19 to 28, gave 1/14 to 1/110. A process computing the edge load of the grid and of the 3000-node graph peaked at 0.73 GB.

  • Names. NetworkX exports load_centrality, its second name for newman_betweenness_centrality, and so does nxapi. The generator (scripts/maketree.py) and the name check in test_match_nx.py now accept a public name whose function is registered under another name.

  • Tests.

    • 330 random graphs: directed and undirected; 165 in more than one piece; 215 with self-loops; a third with string labels; 48 of two nodes or fewer; and two-thirds weighted, with one weight per graph or none.
    • Every cutoff above, normalized both ways, and 949 calls with v.
    • The small graphs' scaling; each decline above, most also with NetworkX's answer under backend_priority; batches of 1, 2 and 5 sources; direct calls of the nxapi functions; and this backend's own graphs.
    • 38 mutants of the kernel, the wrapper and the generator each fail them, in NetworkX dev, 3.7 and 3.6.1.
  • NetworkX's test_load_centrality.py through the backend: the same 18 tests pass before and after in dev, 3.7 and 3.6.1, with 24 load calls served in each and one left to NetworkX (test_weighted_load, whose weights differ).

  • Not done.

    • A chunksize keyword, as betweenness has; the batch budget is betweenness's 256 MiB.
    • Weighted graphs with distinct weights, which would need Dijkstra's lengths and its walk's ties.
    • should_run, which is not on this branch (50-spanning-trees adds it). At the sizes measured the backend was faster with the conversion counted; smaller graphs were not timed.

Stacked on #145.

Serve load_centrality (newman_betweenness_centrality) and
edge_load_centrality, which NetworkX computes with a Python walk from
each source. Load splits what a node passes up equally among its
predecessors rather than by their path counts, so the batched walk of
betweenness carries each node's count of predecessors, and one
multiply per level passes the shares back up.

The cutoffs are NetworkX's: a count of levels as nx.predecessor stops,
and the lengths Dijkstra keeps when weighted. A weighted call is served
only where every edge weighs one positive value of one type, so that
Dijkstra's predecessors are the levels', ties included. Other weights,
nodes that may not compare when NetworkX sorts them, and a cutoff that
is not a number are left to NetworkX.

The name generator and the nxapi name check now accept a second name
for a function, as NetworkX exports load_centrality.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.73096% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.13%. Comparing base (026f2a9) to head (18bb9e4).

Additional details and impacted files
@@                 Coverage Diff                  @@
##           45-betweenness-k     #154      +/-   ##
====================================================
+ Coverage             92.94%   93.13%   +0.19%     
====================================================
  Files                   182      185       +3     
  Lines                 11398    11790     +392     
  Branches               1301     1354      +53     
====================================================
+ Hits                  10594    10981     +387     
- Misses                  549      552       +3     
- Partials                255      257       +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