Repository navigation
Conversation
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 Report❌ Patch coverage is 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:
|
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.
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)andlength(p) < length(c)). Whenvis not one of those for sources, every length fromsis the same withoutv: each node keeps a parent other thanv, and since a parent is strictly nearer, this holds all the way back tos. Float addition is monotone, so this holds exactly for float lengths too, not only within a tolerance. A row is searched again withoutvonly for such pairs, and only when the rest stays connected withoutv(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:
-inf. On a disconnected graph a node whose removal leaves the rest connected (an isolated node) getsinf, and the othersnan.wiener_indexis used as it is: a NumPy int gives NumPy floats, aDecimalgives Decimals on a digraph and NetworkX's TypeError on a graph.nodegives 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).
infis 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 awiener_index); an unhashablenode(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):
No size threshold: the smallest graph here is already faster than NetworkX.
Tests:
test_vitality.pycompares 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.pythrough 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 ofcloseness_vitalityserved 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.