Repository navigation
Conversation
Robins and Alexander's clustering is four times the four-cycles over the paths of three edges whose ends differ. With the nodes ranked by degree, a four-cycle has one node above its other three and one node opposite it, and the k paths of two edges from the top node down to the opposite one close C(k, 2) of its cycles, each counted once. A masked plus_pair product counts those paths, taking the rows in blocks of a bounded number of paths so that a dense graph's products stay small, and a node's own count, for butterflies, adds what it closes as top, opposite or middle node. The three-paths are a sum over the edges of (d_u - 1)(d_v - 1), less three for each triangle. Neither needs the bipartition, as in NetworkX, so a graph that is not bipartite gets NetworkX's numbers. The counts are added in int64 and divided once, as NetworkX divides, so the value is NetworkX's to the bit, and the integer 0 where NetworkX returns that. Where a bound on the counts, half the sum of the squares of each node's walks of two steps, reaches 2**62, the call raises OverflowError after the conversion. Graphs with self-loops, which NetworkX's loops read as neighbors, and directed graphs, which NetworkX 3.6.1 counts by successors and 3.7 refuses, are left to NetworkX. butterflies is NetworkX 3.7's, and its nodes follow NetworkX's nbunch rules.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 20-wiener-efficiency #161 +/- ##
========================================================
+ Coverage 92.88% 93.09% +0.21%
========================================================
Files 182 187 +5
Lines 11227 11571 +344
Branches 1274 1305 +31
========================================================
+ Hits 10428 10772 +344
Misses 546 546
Partials 253 253 🚀 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.robins_alexander_clusteringandbipartite.butterflies(NetworkX 3.7 and later). Robins and Alexander's clustering is four times the four-cycles over the paths of three edges whose ends differ. With the nodes ranked by degree, a four-cycle has one node above its other three and one node opposite it, and the k paths of two edges from the top node down to the opposite one close C(k, 2) of its cycles, each counted once. A maskedplus_pairproduct counts those paths;butterflies, each node's four-cycles, adds what a node closes as top, opposite or middle node. The three-paths are a sum over the edges of (d_u - 1)(d_v - 1), less three for each triangle. NetworkX walks every path of three edges in Python.NetworkX's value to the bit. Every count is an integer, and the value is one division, done as NetworkX does it:
4 * C4 / L_3from Python ints, withL_3 = paths / 2a float, as NetworkX 3.7 hassum(butterflies(G).values()) / L_3. NetworkX 3.6.1's(4.0 * C_4) / L_3, withC_4 = cycles / 4, is the same float, since a factor of 4 rounds exactly. On 2720 random graphs whose value is a float, every value has NetworkX's bits in dev, 3.7 and 3.6.1; multiplying by1 / L_3instead would change 555 of them.The integer 0 where NetworkX returns it: under four nodes or three edges, and where there is no path of three edges (a star). A graph with such paths and no four-cycle gives 0.0, as in NetworkX.
Not bipartite. NetworkX does not check, and counts every four-cycle and every path of three edges whose ends differ, so these do too: the triangles are taken out of the paths.
complete_graph(4)gives 1.0, as in NetworkX.Left to NetworkX: a graph with self-loops, which NetworkX's loops read as neighbors (four self-loops and one edge give 2.0 in 3.6.1 and 0.0 in 3.7); a directed graph, which NetworkX 3.6.1 counts by successors and 3.7 refuses from
butterfliesafter its early zeros; and a multigraph, as everywhere in this backend. Self-loops are seen after the conversion, so the wrapper raises NotImplementedError there: with graphblas in the priority, NetworkX answers, and withbackend="graphblas"NetworkX raises.Overflow. The counts are added in int64 once a bound on them stays below 262: a node's four-cycles are at most half the square of its paths of two edges, so the cycles at most half the sum of those squares, and the three-paths at most the sum over the nodes of their degree less one times their paths of two edges. Where the bound reaches 262 the call raises OverflowError, whose message says to call with
backend='networkx', as the other names here raise it where their integers could pass int64; NetworkX counts with Python ints. It is not a decline, so NetworkX's fallback does not take it. The bound needs each node's sum of its neighbors' degrees, a pass over every edge, so it is checked after the conversion; no part of it is checked incan_run.butterfliesreturns a dict in node order, of Python ints.nodesfollows NetworkX'snbunch_iter: one node, an iterable whose absent nodes are skipped, and NetworkX's errors for an unhashable member or a non-iterable. A directed graph raisesNetworkXNotImplemented, as in NetworkX. NetworkX 3.6.1 has nobutterflies, so it is listed as added in 3.7;nxapi.bipartite.butterfliesworks there too.Memory. The rows are taken in blocks of about 2**23 paths of two edges, so a product never holds a row for every node at once. On a dense graph (3000 + 3000 nodes, 450k edges), the peak RSS of
butterfliesis 1.0 GB, against 1.6 GB as one block, with 0.35 GB of each in use before the call.Speed (Indicative: dev, best of 3 interleaved in one process, conversion on every call unless cached; load 15 to 27). Graphs where NetworkX takes about 1 to 30 s:
NetworkX's clustering spends its time on the paths of three edges; its
butterfliesis far quicker, and on small graphs quicker than the conversion: with conversion,butterfliesis 0.06x at 30 edges, 0.41x at 300 and 1.2x at 2000 on those graphs (under a millisecond either way below 2000 edges), and the clustering 0.22x at 30 edges and 3.2x at 300. On sparser graphsbutterfliesbreaks even later: a review measured 0.5x at 2000 edges, 0.77x at 10000 and 1.0x near 30000 (nodes about edges^0.75), and about 1000 to 1500 edges on dense graphs. No size gate is added here; this branch has noshould_runhook.Tests. 309 seeded graphs (random bipartite, with hubs, complete bipartite, grids, hypercubes, cycles, trees, stars, complete graphs, random graphs that are mostly not bipartite, relabeled graphs with isolated nodes) against NetworkX and against a count from neighbor sets that is itself checked by listing every four-cycle; the rows in blocks of one row and a few; a graph whose node ids do not follow its node order; every case left to NetworkX, three ways (NetworkX, graphblas by name, graphblas first in the priority);
nodesas NetworkX reads it; the int64 bound. 30 mutants of the kernel and wrappers each fail them, in dev, 3.7 and 3.6.1.NetworkX's tests of these names (
bipartite/tests/test_cluster.pyand the doctests ofbipartite/cluster.py) through the backend in dev, 3.7 and 3.6.1: the same tests pass before and after (38, 38 and 12), with 7 clustering calls served in each, and 28butterfliescalls in dev and 3.7.Also:
algorithms.bipartiteexports only its functions, so itsclustermodule does not take the place ofalgorithms.cluster.Stacked on #121.