Skip to content

66 bipartite clustering and redundancy - #164

Open
eriknw wants to merge 1 commit into
python-graphblas:62-bipartite-c4from
eriknw:66-latapy
Open

eriknw wants to merge 1 commit into
python-graphblas:62-bipartite-c4from
eriknw:66-latapy

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add bipartite.latapy_clustering (also bipartite.clustering), the bipartite average_clustering (registered as bipartite_average_clustering) and bipartite.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 masked plus_pair product 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 nodes of 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.

  • nodes and mode as NetworkX reads them. nodes is 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 over nodes and divides by len(nodes), so a repeated node counts twice, a generator raises TypeError, and no nodes raise ZeroDivisionError. Node redundancy reads nodes twice, 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 nodes is 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:

    graph edges latapy, NetworkX with conversion cached redundancy, NetworkX with conversion cached
    random bipartite, 1e4 + 1e4 1e5 3.13 s 30.1x 75x 0.85 s 13.5x 51x
    random bipartite, 3e4 + 3e4 3e5 9.16 s 30.3x 68x 2.65 s 12.8x 49x
    random bipartite, 7e4 + 7e4 7e5 23.2 s 33.1x 74x 6.37 s 11.2x 53x
    power law 2.5, 2e4 + 2e4 5.7e4 7.11 s 43.5x 60x 2.56 s 32.5x 55x
    dense, 2000 + 2000 6.0e4 8.13 s 95.1x 136x 2.73 s 52.5x 91x
    random, not bipartite, 3e4 nodes 1.5e5 1.31 s 12.8x 47x

    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_clustering as bipartite_average_clustering, while average_clustering and clustering name NetworkX's top-level functions. The dispatch table's generator, the test of nxapi'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.bipartite still exports nothing to nxapi, so nxapi.clustering and nxapi.average_clustering stay 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_bipartite against 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 of cluster.py and redundancy.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 of latapy_clustering, 6 of the average and 7 of node_redundancy in each, each converted, where before it served none.

Stacked on #161.

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

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.33%. Comparing base (b6fc318) to head (3538d12).

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:
  • ❄️ 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