Skip to content

62 bipartite four-cycles - #161

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:62-bipartite-c4
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:62-bipartite-c4

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Add bipartite.robins_alexander_clustering and bipartite.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 masked plus_pair product 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_3 from Python ints, with L_3 = paths / 2 a float, as NetworkX 3.7 has sum(butterflies(G).values()) / L_3. NetworkX 3.6.1's (4.0 * C_4) / L_3, with C_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 by 1 / L_3 instead 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 butterflies after 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 with backend="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 in can_run.

  • butterflies returns a dict in node order, of Python ints. nodes follows NetworkX's nbunch_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 raises NetworkXNotImplemented, as in NetworkX. NetworkX 3.6.1 has no butterflies, so it is listed as added in 3.7; nxapi.bipartite.butterflies works 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 butterflies is 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:

    graph edges clustering, NetworkX with conversion cached butterflies, NetworkX with conversion cached
    random bipartite, 2e4 + 2e4 2e5 5.07 s 36.5x 166x 0.48 s 3.3x 16x
    power law 2.5, 2e4 + 2e4 5.7e4 6.15 s 114.7x 477x 0.15 s 2.4x 8x
    dense, 2000 + 2000 6.0e4 6.58 s 162.1x 469x 0.26 s 5.6x 11x
    random, not bipartite, 5e4 nodes 2.5e5 6.61 s 37.4x 271x 0.67 s 3.8x 17x
    random bipartite, 1e5 + 1e5 1e6 32.4 s 40.8x 386x 4.08 s 4.2x 18x
    power law 2.2, 1e5 + 1e5 4.8e5 6.76 s 8.3x 51x
    dense, 3000 + 3000 4.5e5 9.95 s 14.9x 15x

    NetworkX's clustering spends its time on the paths of three edges; its butterflies is far quicker, and on small graphs quicker than the conversion: with conversion, butterflies is 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 graphs butterflies breaks 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 no should_run hook.

  • 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); nodes as 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.py and the doctests of bipartite/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 28 butterflies calls in dev and 3.7.

  • Also: algorithms.bipartite exports only its functions, so its cluster module does not take the place of algorithms.cluster.

Stacked on #121.

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

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.09%. Comparing base (5b54ace) to head (b6fc318).

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