Repository navigation
Conversation
Latapy's bipartite clustering averages, over the nodes two steps from a node, the neighbors each shares with it over the size of the union of their neighborhoods, the smaller degree or the larger. A masked plus_pair product counts the shared neighbors, taking the rows asked for in blocks of a bounded number of paths of two steps, and each term is one division of two integers, as in NetworkX. NetworkX adds a node's terms in the order of a Python set, which is not followed here, so a value can differ from NetworkX's in its last bits. A graph that is not bipartite raises NetworkXError, as in NetworkX: it is one where some node shares a component with its twin in the double cover. Node redundancy, the share of a node's pairs of neighbors that meet at another node too, counts from the same product the pairs that share two neighbors or more, and divides as NetworkX does, so it is NetworkX's to the bit. It does not ask for a bipartite graph. The nodes, the mode and the errors are NetworkX's, in its order. A directed graph is left to NetworkX, and for node_redundancy so is a graph with self-loops. The bipartite average_clustering is registered as bipartite_average_clustering, so the dispatch table's generator, and the tests that follow it, now know a function by where it lives before its name, which here also names nx.average_clustering.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 62-bipartite-c4 #164 +/- ##
===================================================
+ Coverage 93.09% 93.33% +0.23%
===================================================
Files 187 192 +5
Lines 11571 11953 +382
Branches 1305 1341 +36
===================================================
+ Hits 10772 11156 +384
Misses 546 546
+ Partials 253 251 -2 🚀 New features to boost your workflow:
|
eriknw
force-pushed
the
62-bipartite-c4
branch
from
December 3, 2026 05:21
69b393b to
b6fc318
Compare
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
bipartite.latapy_clustering(alsobipartite.clustering), the bipartiteaverage_clustering(registered asbipartite_average_clustering) andbipartite.node_redundancy, all dispatched in NetworkX 3.6.1, 3.7 and dev. Latapy's clustering of a node averages, over the nodes two steps away, the neighbors each shares with it over the size of the union of their neighborhoods (mode="dot"), the smaller degree ("min") or the larger ("max"). A maskedplus_pairproduct counts the shared neighbors of the rows asked for. Node redundancy, the share of a node's pairs of neighbors that meet at another node too, counts from the same product the pairs that share two neighbors or more. NetworkX builds neighbor sets for every pair in Python.Floats. Each term of the clustering is one division of two integers, as in NetworkX. NetworkX adds a node's terms in the order of a Python set, which this backend does not follow: it adds them in the order of the nodes' ids, and divides the sum by their number, as NetworkX does. So a value can differ from NetworkX's in its last bits. On 24817 values from 1200 random graphs, each mode, 94% have NetworkX's bits in dev, 3.7 and 3.6.1, and the largest relative difference is 6.5e-16 (7.1e-16 in an earlier run: the order of a set of strings changes from one process to the next). The average is NetworkX's own sum over
nodesof those values. Node redundancy is one division of two integers, as NetworkX divides, so it is NetworkX's to the bit.Not bipartite. The clustering and its average raise NetworkXError "Graph is not bipartite", before the mode is checked, as in NetworkX; a self-loop counts as a cycle of odd length, as in NetworkX's
is_bipartite. A graph is bipartite when no node shares a component with its twin in the double cover, found with the component labels the connectivity names use. Node redundancy does not ask, and serves any graph without self-loops;complete_graph(4)gives 1.0 for each node, as in NetworkX.nodesandmodeas NetworkX reads them.nodesis iterated, not read as an nbunch: an absent node raises KeyError, an unhashable one TypeError, a single node TypeError (not iterable), and a string is read by its characters. A repeated node is answered once, in the order first given. The average sums overnodesand divides bylen(nodes), so a repeated node counts twice, a generator raises TypeError, and no nodes raise ZeroDivisionError. Node redundancy readsnodestwice, so a generator gives{}, and a node of fewer than two neighbors raises NetworkX's NetworkXError, "Cannot compute redundancy coefficient for a node that has fewer than two neighbors." A mode other than"dot","min"or"max"raises NetworkXError "Mode for bipartite clustering must be: dot, min or max", and an unhashable one TypeError. Isolated nodes give 0.0. Each error is NetworkX's type and message, on each NetworkX version and Python version run here.Left to NetworkX: a directed graph, whose successors NetworkX reads as its neighbors; a multigraph, as everywhere here; and for node redundancy, a graph with self-loops, which NetworkX counts as neighbors (the four-cycle names' refusal, with its message). The self-loop decline comes before
nodesis read, so a generator reaches NetworkX whole.Memory. The rows go in blocks of about 2**22 paths of two steps (at least one row), so the product's size is bounded by the budget, not by the graph. Peak RSS of one process running all three names with conversion: 394 MB on the dense graph below (118 MB after building it) and 1012 MB on the random graph of 7e5 edges (301 MB after building it).
Speed (Indicative: dev, best of 3 interleaved in one process, conversion on every call unless cached; load 4 to 10). Graphs where NetworkX takes about 1 to 30 s:
The average follows the clustering: 31.6x on the first graph and 94.6x on the dense one. On small graphs the conversion costs more than NetworkX only at 30 edges (0.13x to 0.18x, under a millisecond either way); at 300 edges these are 2.1x to 3.3x.
Dispatch table. NetworkX registers the bipartite
average_clusteringasbipartite_average_clustering, whileaverage_clusteringandclusteringname NetworkX's top-level functions. The dispatch table's generator, the test ofnxapi's layout and the test that every wrapper converts through a helper now know a function by where it lives (its module and name, as NetworkX's), and fall back to its name; nothing generated before changes.nxapi.bipartitestill exports nothing tonxapi, sonxapi.clusteringandnxapi.average_clusteringstay the top-level ones. The call table gains a bipartite graph, the square without its diagonals.Tests. 305 seeded bipartite graphs (random, with hubs, complete bipartite, grids, hypercubes, even cycles, paths, trees, stars, relabeled graphs with isolated nodes) and 28 that are not bipartite: the clustering in each mode against NetworkX and against exact sums of fractions, the average, and node redundancy bit for bit against NetworkX and a count from neighbor sets;
is_bipartiteagainst NetworkX on 200 random graphs, a quarter with a self-loop; every argument case and error above three ways (NetworkX, graphblas by name, graphblas first in the priority); the rows in blocks of one row and a few; a graph whose node ids do not follow its node order; the names that must not collide. 28 mutants of the kernels and wrappers each fail them, in dev, 3.7 and 3.6.1.NetworkX's tests (
bipartite/tests/test_cluster.py,test_redundancy.py, and the doctests ofcluster.pyandredundancy.py) through the backend in dev, 3.7 and 3.6.1: the same tests pass before and after (42, 42 and 16), and after it the backend serves 10 calls oflatapy_clustering, 6 of the average and 7 ofnode_redundancyin each, each converted, where before it served none.Stacked on #161.