Skip to content

64 closeness vitality - #169

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:64-closeness-vitality
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:64-closeness-vitality

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add closeness_vitality. The closeness vitality of a node is the Wiener index of the graph less that of the graph without the node. NetworkX finds every shortest length again for each node, which costs all pairs once per node.

  • Shared work: one pass over all sources finds each source's lengths and, for each source, the nodes that are the one strictly nearer parent of some node on its shortest paths (length(p) + w == length(c) and length(p) < length(c)). When v is not one of those for source s, every length from s is the same without v: each node keeps a parent other than v, and since a parent is strictly nearer, this holds all the way back to s. Float addition is monotone, so this holds exactly for float lengths too, not only within a tolerance. A row is searched again without v only for such pairs, and only when the rest stays connected without v (strongly connected, on a digraph). Each node's index then follows from the sums of the first pass: the total, less the node's own row and column, plus what the rows searched again changed. The share of pairs searched again: none on a complete graph or a tree (where every node but a leaf disconnects the rest), 11% on a 15 x 15 grid, 21% on a Barabasi-Albert graph and 40% on a Watts-Strogatz graph of 300 nodes, 73% on a cycle of 12.

  • Rows: without a weight, or when every edge holds one finite value, rows are breadth-first searches in hops, and the rows searched again add up their lengths a level at a time without holding them. Otherwise rows are Bellman-Ford over lengths that changed. A zero or infinite weight can leave a node with no strictly nearer parent (a tie over a weight of 0, or a length of inf reached from inf). That source's row is then searched again for every node it reaches.

  • Values and types are NetworkX's:

    • the Wiener index is inf unless every node reaches every node, so a node whose removal disconnects the rest gets -inf. On a disconnected graph a node whose removal leaves the rest connected (an isolated node) gets inf, and the others nan.
    • a length of inf (over an infinite weight) makes the index inf, as in NetworkX, so removing a node can make the index inf or bring it back to a finite value.
    • an int on a directed graph of integer lengths, a float otherwise (NetworkX halves the undirected sum).
    • a given wiener_index is used as it is: a NumPy int gives NumPy floats, a Decimal gives Decimals on a digraph and NetworkX's TypeError on a graph.
    • a node gives one value; a node not in the graph gives 0.0, or nan where the index is inf.
  • Weights served: Python ints or floats (an int64 or float64 matrix), at least 0, and not NaN on an edge between two nodes. A NaN self-loop is served, as no self-loop changes a length (NetworkX's own conversion turns an undirected self-loop of inf into NaN). inf is added as NetworkX adds it. A missing weight is 1. Ints raise OverflowError where the number of nodes cubed times the largest weight could reach 2**63.

  • Left to NetworkX: negative weights, where NetworkX's Dijkstra raises "Contradictory paths found" (a self-loop's too); NaN between two nodes; bools and narrow NumPy types (NetworkX sums them in their own types); graphs of fewer than two nodes (NetworkX raises NetworkXPointlessConcept, or returns {} for the null graph with a wiener_index); an unhashable node (TypeError in NetworkX).

  • Ints mixed with floats: the matrix holds floats, so a value is a float where NetworkX's can be an int of the same value, on a digraph where no float weight is on a shortest path.

  • Memory: a batch holds as many rows as 1 GiB allows, at 64 bytes a row per node, plus 48 per edge in the first pass, which also compares lengths along every edge. A first-pass batch's rows are dropped before its pairs are searched again. Measured as macOS phys_footprint, whose lifetime maximum is the high-water mark: on random trees of 20000 nodes (every node but a leaf disconnects them, so only connectivity is searched again) it grew by 473 MiB, and 455 MiB with float weights. Under a batch budget of 64 MiB, on a connected Watts-Strogatz graph of 2000 nodes where 1.6 million rows (2.3 million with weights) are searched again, it grew by 39 and 42 MiB, and by 31 and 35 MiB on trees of 6000 nodes. RSS grows by more: macOS keeps freed pages resident for reuse (it ended 0.35 to 1.2 GiB above the start in these runs), and the footprint leaves them out.

  • Speed (indicative; median of three interleaved rounds on a shared machine at a one-minute load of 19 to 21, conversion included):

    graph NetworkX here
    G(n, m), 99 nodes, 300 edges 1.3 s 40 ms
    G(n, m), 200 nodes, 600 edges 11.4 s 65 ms
    G(n, m), 200 nodes, 2000 edges 8.5 s 39 ms
    15 x 15 grid 16.3 s 75 ms
    Watts-Strogatz, 300 nodes, k=4 35.6 s 147 ms
    Barabasi-Albert, 300 nodes, m=2 31.8 s 44 ms
    G(n, m), 200 nodes, 600 edges, float weights 30.7 s 103 ms
    G(n, m), 200 nodes, 600 edges, int weights 29.3 s 212 ms
    15 x 15 grid, float weights 33.5 s 247 ms
    directed G(n, p), 200 nodes, p=0.03 (not strongly connected) 17 ms 11 ms

    No size threshold: the smallest graph here is already faster than NetworkX.

  • Tests: test_vitality.py compares values, types and key order with NetworkX on random graphs (sparse and dense G(n, p), trees, barbells, graphs with cut vertices, digraphs, directed cycles) under eight kinds of weights (none, ints, ints with 0, floats, some inf, some missing, all 2, all 0.1), each with a node, an absent node, and a given index. It also covers the known values above, zero weights that tie, infinite lengths in rows searched again, batches of one to three rows, how many rows are searched again, every decline (explicit backend, graphblas first, and a direct call), NaN and inf self-loops, and overflow. A probe of 318 further random graphs (1590 calls, 11014 values, with self-loops, relabeled nodes and mixed weights) matches NetworkX in values and types in dev, 3.7 and 3.6.1. The largest float difference is 3.5e-16 of the Wiener index.

  • Mutants: 31 changes to the kernel and the wrapper, in NetworkX dev, 3.7 and 3.6.1. 27 make these tests fail (one by never finishing: without the overflow check, int lengths wrap and Bellman-Ford does not end). 4 leave them passing, as they change only speed or memory: searching rows again where the rest is disconnected, sizing first-pass batches without the edges, holding a batch's rows while rows are searched again, and holding hop lengths instead of adding them up a level at a time.

  • NetworkX's test_vitality.py through the backend passes in dev, 3.7 and 3.6.1, with fallback on and off (6 of 6, as before this change), with 5 calls of closeness_vitality served in each.

  • The package's tests pass in dev, 3.7 and 3.6.1, and NetworkX's whole suite through the backend (dev, fallback on) gives the same outcomes as before this change, test by test.

Stacked on #121.

The closeness vitality of a node is the Wiener index of the graph less
that of the graph without the node. Rather than find every length again
for each node, one pass over all sources finds, for each source, the
nodes that are the one strictly nearer parent of some node on its
shortest paths. Without any other node, every length from that source
is kept: each node keeps a parent other than the one removed, and the
parent order is well founded, so this holds for float lengths too. A
row is searched again without a node only for those pairs, and only
when the rest stays connected (strongly, on a digraph); each node's
index then follows from the sums of the first pass.

Without a weight, or when every edge holds one finite value, rows are
breadth-first searches in hops, and those searched again add up their
lengths a level at a time. Otherwise they are Bellman-Ford over lengths
that changed. A zero or infinite weight can leave a node with no
strictly nearer parent; that source's row is then searched again for
every node it reaches.

Values and types are NetworkX's: inf where the graph is not connected,
-inf for a node whose removal disconnects the rest, nan where both are
infinite, an int on a directed graph of integer lengths and a float
otherwise, and a given wiener_index used as it is. Weights are served
when they are Python ints or floats, at least 0 and not NaN between two
nodes; inf is added as NetworkX adds it. Ints raise OverflowError where
the number of nodes cubed times the largest could reach 2**63. Graphs
of fewer than two nodes and an unhashable node are left to NetworkX.

Batches of rows are sized to 1 GiB at the bytes a row takes per node
and per edge, measured, and a first-pass batch's rows are dropped
before rows are searched again.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.37500% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.14%. Comparing base (5b54ace) to head (5943fa6).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #169      +/-   ##
========================================================
+ Coverage                 92.88%   93.14%   +0.26%     
========================================================
  Files                       182      185       +3     
  Lines                     11227    11707     +480     
  Branches                   1274     1326      +52     
========================================================
+ Hits                      10428    10905     +477     
- Misses                      546      547       +1     
- 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