Repository navigation
Conversation
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 Report❌ Patch coverage is 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:
|
eriknw
force-pushed
the
65-aperiodic-flow
branch
from
December 3, 2026 04:45
c55de1f to
be2dec2
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
is_aperiodic,flow_hierarchyandkosaraju_strongly_connected_components, on the strongly connected component labels of #124. In a strongly connected graph every walk from node 0 to a nodevhas the same length modulo the period, the gcd of the cycle lengths. So withlevel[v]the depth ofvin a breadth-first walk from node 0, the period is the gcd oflevel[u] + 1 - level[v]over the edges(u, v): onenp.gcd.reduceover 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, soflow_hierarchyis read off the labels.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.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'sNetworkXError("is_aperiodic not defined for undirected graphs"), since 3.6.1 hands one to the backend, while 3.7 and dev refuse it withNetworkXNotImplementedbefore dispatching.1 - (weight of the edges inside a component) / (all weight), a self-loop counting as inside, asG.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 ofm * 2**-53formedges), 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 givesnp.float64for NumPy weights.NetworkXError("flow_hierarchy not applicable to empty graphs"), looked for before the direction, then an undirected graphNetworkXError("G must be a digraph in flow_hierarchy").sourceyields the components as sets from the same labels asstrongly_connected_components, in an order that may differ from NetworkX's.flow_hierarchywhere 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_componentswith asource: NetworkX dev keeps its walk to the nodes that reachsourceand 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.flow_hierarchy2.3x to 2.5x, and 3.6x to 3.7x with float weights;kosaraju_strongly_connected_components1.2x to 1.3x;is_aperiodic1.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).kclasses with chords only from one class to the next (period a multiple ofk), 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 withbackend="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.test_dag,test_hierarchyandtest_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.Stacked on #124.