Skip to content

71 number of walks - #170

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:71-number-of-walks
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:71-number-of-walks

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Add number_of_walks. NetworkX (3.6.1, 3.7 and dev alike) takes its int64 adjacency matrix to the power walk_length with SciPy's sparse matrix_power, then reads every pair out of the sparse result, one at a time, into a dict of dicts. Here GraphBLAS takes the power, and the dict is built from the rows of a dense array.

  • The power. Each edge counts once whatever its value, as NetworkX passes weight=None, and an undirected self-loop once, as in NetworkX's adjacency matrix. The power is built through the leading bits of walk_length: from one power to the next by a squaring (and a step) or by single-edge steps, whichever needs fewer multiplications, as counted from the entries in each row and column. Steps win once the counts fill in, as on a random graph; squarings where they stay sparse, as on a cycle or a path (a walk length of 10000 on a directed cycle of 1000 nodes: 2.4 ms against 250 ms by steps, Indicative). Both ways are tested against exact counts.
  • Exact counts. The power is taken in floats first, which are exact while every count on the way stays below 253, and the counts come back as ints. Past that, it is taken again in uint64, whose arithmetic wraps modulo 264 even where a count on the way does not fit, so every count below 264 comes out exact. The floats tell which those are: each count is within a factor (1 + n * 2-53)walk_length of its float. OverflowError is raised where a count could reach 264.
  • Where NetworkX's int64 overflows. From 263 NetworkX's counts wrap to negative ones: on the complete graph of 10 nodes, the walks of length 21 from a node back to itself number 10941898913151235920, and NetworkX gives -7504845160558315696. Here they are exact up to 264, with OverflowError past it rather than a wrapped count, and also just below it where the floats cannot rule it out (a review found 3 such of about 120 counts between 263 and 264). Where the floats bound the counts too loosely to tell (counts past 253 at walk lengths of about 251 / n or more, where that factor passes 4/3), the call raises OverflowError even when the counts are below 263. Only counts that grow polynomially with the length get there, as from one cycle to another in a directed graph: 2 nodes with self-loops and an edge between them have 260 walks of length 2**60 from one to the other, which NetworkX counts right and this refuses.
  • Types and order. Python ints, in node order, as in NetworkX. On a graph without edges, floats: NetworkX's adjacency matrix of such a graph is float64, so it answers 0.0, and 1.0 at length 0.
  • Errors as in NetworkX: a negative walk_length raises its ValueError, and the null graph its NetworkXError "Graph has no nodes or edges". Ints of every kind are served, bools and NumPy ints too. A walk_length that is not an int (2.0, a Fraction, a 0-d array, a string) is left to NetworkX, whose comparison with 0 or SciPy's matrix power answers or raises. Multigraphs are declined, as everywhere in this backend (NetworkX counts parallel edges as walks of their own).
  • An edge attribute named None. NetworkX builds its matrix with G.edges(data=None, default=1), which reads an attribute named None as the edge's value: with 5 there, a path of 3 nodes has 25 walks of length 2 from an end back to itself, where there is one. This counts the edge once. So does this backend's adjacency_matrix, which NetworkX's number_of_walks calls, so with this backend first NetworkX's own answer counts it once too, and declining here would not bring NetworkX's back. The matrix names with weight=None are a follow-up.
  • Speed (Indicative: interleaved with NetworkX dev, best of 3, one process a call with both backends imported first, conversion included; load 4 to 6). Where NetworkX takes 4 to 26 s: 130x to 188x, on random graphs of 1000 and 2000 nodes with four times as many edges at walk lengths 3 and 10, a random digraph of 1000 nodes at length 10, a directed cycle of 1000 nodes at length 1000, and a random graph of 1500 nodes and 15000 edges at length 8. At 300 nodes (NetworkX 0.4 s), 56x to 75x. The dict of every pair is most of the time on both sides. NetworkX reads each pair from its sparse result one at a time: at 2000 nodes its dict takes 17.2 s and its power 0.01 s at length 3, and 19.2 s and 7.0 s at length 10. Here, at 2000 nodes, the dict takes 0.08 to 0.10 s, the power 0.01 to 0.04 s and the conversion 3 ms.
  • Tests. 330 random graphs, directed and undirected, with self-loops, isolated nodes, weights of several kinds (not read), and int, string and tuple labels, at walk lengths 0 to 64 and 240: 282 with counts below 253, 10 between 253 and 263, 38 reaching 264 (OverflowError), 7 without edges. Each is compared with exact counts from powers of an object matrix capped at 264 and, below 263, with NetworkX's dict (values, types and key order), by name with backend="graphblas" and with this backend first in NetworkX's priority and fallback off; both ways of taking the power are checked too. Also the edges of the bound: n(k - 1) walks on complete graphs with self-loops (1287, 2557, 2558 and 264 itself), and NetworkX's wrapped counts on the complete graph of 10 nodes; counts past 253 on the way and few at the end (layers of 64 nodes, 254 walks across 11 layers and none longer, beside a cycle); walk lengths of 270 and 10400; the bound itself; every error and decline above; and the kernel on a weighted backend graph. 26 mutants of the kernel and the wrapper each fail them, in NetworkX dev, 3.7 and 3.6.1.
  • NetworkX's tests of this name (test_walks), through the backend in dev, 3.7 and 3.6.1: the same tests pass before and after (19, 19 and 6), with 6 conversions to the backend after against 5 before, when NetworkX's number_of_walks handed its adjacency_matrix to this backend. The repo's tests pass in all three.
  • Also: an entry in the dispatched-call table and a note for the name, with the plugin info regenerated.

Stacked on #121.

The counts are a power of the adjacency matrix, each edge counted once
whatever its value. The power is built through the leading bits of the
walk length, from one power to the next by a squaring or by single-edge
steps, whichever needs fewer multiplications: steps once the counts
fill in, squarings where they stay sparse, as on a cycle or a path.

The power is taken in floats first, which are exact while every count
on the way stays below 2**53. Past that it is taken again in uint64,
which wraps modulo 2**64 and so is exact below 2**64, where the floats
bound every count; OverflowError is raised where one could reach 2**64.
From 2**63 NetworkX's int64 counts wrap to negative ones, and these
stay exact.

The counts come back as a dict of dicts of ints in node order, as in
NetworkX, and as floats on a graph without edges, whose adjacency
matrix in NetworkX is float64. A walk_length that is not an int is left
to NetworkX.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.71591% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.09%. Comparing base (5b54ace) to head (74f5985).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #170      +/-   ##
========================================================
+ Coverage                 92.88%   93.09%   +0.20%     
========================================================
  Files                       182      185       +3     
  Lines                     11227    11579     +352     
  Branches                   1274     1322      +48     
========================================================
+ Hits                      10428    10779     +351     
- Misses                      546      547       +1     
  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