Skip to content

65 aperiodicity and flow hierarchy - #163

Open
eriknw wants to merge 1 commit into
python-graphblas:21-strongly-connectedfrom
eriknw:65-aperiodic-flow
Open

eriknw wants to merge 1 commit into
python-graphblas:21-strongly-connectedfrom
eriknw:65-aperiodic-flow

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add is_aperiodic, flow_hierarchy and kosaraju_strongly_connected_components, on the strongly connected component labels of #124. In a strongly connected graph every walk from node 0 to a node v has the same length modulo the period, the gcd of the cycle lengths. So with level[v] the depth of v in a breadth-first walk from node 0, the period is the gcd of level[u] + 1 - level[v] over the edges (u, v): one np.gcd.reduce over the edge list, where a tree edge gives 0 and a self-loop 1. An edge lies on a cycle exactly when its ends share a component, so flow_hierarchy is read off the labels.

  • is_aperiodic. The walk takes a GraphBLAS round a level; a long thin walk, as around a cycle, goes to a queue in Python, priced as is_strongly_connected's walks are. A single node is aperiodic only with a self-loop (without one it has no edge, gcd 0), as in NetworkX. NetworkX 3.6.1 takes the gcd over breadth-first levels too, and dev and 3.7 over depth-first levels with an early exit; all three answer whether the period is 1, and this agrees with each on every graph tested.
  • is_aperiodic's errors as in NetworkX: NetworkXPointlessConcept("Graph has no nodes."); NetworkXError("Graph is not strongly connected."), which all three versions raise for any graph that is not (none looks at one component only); and for an undirected graph NetworkX 3.6.1's NetworkXError("is_aperiodic not defined for undirected graphs"), since 3.6.1 hands one to the backend, while 3.7 and dev refuse it with NetworkXNotImplemented before dispatching.
  • flow_hierarchy is 1 - (weight of the edges inside a component) / (all weight), a self-loop counting as inside, as G.subgraph(c).size() counts it, a single node's included. Without a weight NetworkX divides edge counts, ints, and so does this: the same float. With integer or bool weights both sums here are exact Python ints, so the value is NetworkX's while its float sums stay below 2**53. Float weights of one sign agree within the rounding of the sums: over 600 random graphs of 5 to 5000 nodes, weights uniform, lognormal or spread over 1e-12 to 1e12, all positive or all negative, the largest difference was 3.3e-16 (0.4 of m * 2**-53 for m edges), and 461 were bitwise equal. A NaN weight gives NaN, and an infinite one NaN or 1.0, as in NetworkX. The answer is a Python float, where NetworkX gives np.float64 for NumPy weights.
  • flow_hierarchy's errors as in NetworkX: a graph without edges raises NetworkXError("flow_hierarchy not applicable to empty graphs"), looked for before the direction, then an undirected graph NetworkXError("G must be a digraph in flow_hierarchy").
  • kosaraju_strongly_connected_components without a source yields the components as sets from the same labels as strongly_connected_components, in an order that may differ from NetworkX's.
  • Declined, so NetworkX answers: flow_hierarchy where the weights sum to 0, since NetworkX raises ZeroDivisionError for Python numbers and gives NaN for NumPy ones, which a conversion does not tell apart; float weights of both signs, whose sums can cancel to what NetworkX's order of summation decides; float weights whose absolute sum passes 2**1023, where NetworkX's sum of degrees (each edge twice) overflows; complex weights, refused by dtype as 49 complex weights #149 does (base 21 does not have 49 complex weights #149, so the wrapper checks). kosaraju_strongly_connected_components with a source: NetworkX dev keeps its walk to the nodes that reach source and yields their components, while 3.7 and 3.6.1 step past them, so the sets differ between versions (on one 7-node graph, from source 0, dev gives [{0, 1}, {4}] and 3.7 [{0, 1, 2}]). Multigraphs, as everywhere in this backend.
  • Speed (Indicative: interleaved with NetworkX dev, best of 3, one process a row, conversion included; load 3 to 4.6). On random digraphs of 300000 and 1000000 nodes (1.2 to 5 million edges), where NetworkX takes 1.4 to 14 s: flow_hierarchy 2.3x to 2.5x, and 3.6x to 3.7x with float weights; kosaraju_strongly_connected_components 1.2x to 1.3x; is_aperiodic 1.9x to 2.2x on graphs of period 3, but 0.8x on an aperiodic one of 1000000 nodes (NetworkX 2.5 s), where NetworkX's walk stops once its gcd reaches 1 and this backend still converts the whole graph. Conversion is most of this backend's time: at 1000000 nodes it takes about 3 s, the kernels 0.01 to 0.14 s. On a conversion NetworkX has cached, the same calls take 0.02 to 0.09 s, 22x to 159x faster than NetworkX. A directed cycle of 1000000 nodes, which goes through the queue in Python, ties NetworkX's 0.7 s (2.1x on a cached conversion).
  • Tests. 330 random digraphs through each name: random ones, a Hamiltonian cycle with chords, a cycle through k classes with chords only from one class to the next (period a multiple of k), sometimes with one chord more, and small cycles joined one way; self-loops on some, isolated nodes, int, string and tuple labels; weights none, ints from 0, ints of both signs, positive floats, floats of both signs (declined), bools. Among the strongly connected ones without a self-loop, 53 are aperiodic and 59 not. Each call is made by name with backend="graphblas" and with this backend first in NetworkX's priority and fallback off, so a decline must reach NetworkX's own answer. Also every error and decline above, NaN and infinite weights, int weights whose sum passes 2**63, cycles of 500 and 3000 nodes through the queue, and larger graphs (a torus, a scale-free graph, a complete bipartite one). 27 mutants of the kernels and wrappers each fail them, in NetworkX dev, 3.7 and 3.6.1.
  • NetworkX's tests of these names (test_dag, test_hierarchy and test_strongly_connected), through the backend in dev, 3.7 and 3.6.1: the same tests pass before and after (98, 97 and 86, with the one xfail each had before), with 12, 12 and 14 more conversions to the backend after. With fallback off, those of these names pass with no xfail (29, 29 and 22), and so does dev's test of kosaraju with a source, which this backend declines and NetworkX answers. The repo's tests pass in all three.
  • Also: entries in the dispatched-call table and notes for the two names that decline, with the plugin info regenerated.

Stacked on #124.

is_aperiodic: in a strongly connected graph every walk from node 0 to a
node v has the same length modulo the period, the gcd of the cycle
lengths. With level[v] the depth of v in a breadth-first walk from node
0, the period is the gcd of level[u] + 1 - level[v] over the edges
(u, v): one NumPy reduction over the edge list, where a self-loop gives
1. The walk takes a GraphBLAS round a level; a long thin one, as around
a cycle, goes to a queue in Python, priced as is_strongly_connected's
walks are. The errors are NetworkX's: NetworkXPointlessConcept for a
graph without nodes, NetworkXError for one that is not strongly
connected, and NetworkX 3.6.1's NetworkXError for an undirected graph
(later versions refuse one before dispatching).

flow_hierarchy: an edge lies on a cycle exactly when its ends share a
strongly connected component, as a self-loop's do, so the answer is 1
less the weight of those edges over all the weight, read off the
component labels. Without weights, or with integer ones, both sums are
exact, and the value is NetworkX's while its sums stay below 2**53.
Float weights of one sign agree within the rounding of the sums, and a
NaN weight gives NaN, as in NetworkX. Left to NetworkX: weights that
sum to 0, where NetworkX raises ZeroDivisionError for Python numbers
and gives NaN for NumPy ones, which a conversion does not tell apart;
float weights of both signs, whose sums can cancel to what the order of
summation decides; float weights whose sum passes half the float range,
where NetworkX's sum of degrees overflows; and complex weights.

kosaraju_strongly_connected_components yields the components from the
same labels as strongly_connected_components, in an order that may
differ from NetworkX's. A source is left to NetworkX: NetworkX dev
keeps its walk to the nodes that reach the source and yields their
components, while 3.7 and earlier step past them, so the sets differ.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.49622% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.57%. Comparing base (a102f8c) to head (be2dec2).

Additional details and impacted files
@@                    Coverage Diff                    @@
##           21-strongly-connected     #163      +/-   ##
=========================================================
+ Coverage                  93.38%   93.57%   +0.19%     
=========================================================
  Files                        189      192       +3     
  Lines                      12034    12426     +392     
  Branches                    1335     1393      +58     
=========================================================
+ Hits                       11238    11628     +390     
  Misses                       541      541              
- Partials                     255      257       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw
eriknw force-pushed the 65-aperiodic-flow branch from c55de1f to be2dec2 Compare December 3, 2026 04:45
This was referenced Dec 4, 2026
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