Skip to content

78 descendants at distance - #175

Open
eriknw wants to merge 1 commit into
python-graphblas:73-bfs-ridersfrom
eriknw:78-descendants-distance
Open

eriknw wants to merge 1 commit into
python-graphblas:73-bfs-ridersfrom
eriknw:78-descendants-distance

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Read descendants_at_distance's distance as NetworkX does. NetworkX walks the graph a level at a time and returns the level i for which i == distance, or set() where no level is equal to it. This backend read distance as a count of steps, which a negative number, a float and anything that is not an int do not fit.

  • Before, the same in NetworkX dev, 3.7 and 3.6.1:

    • -1 and -2 returned {source}, where NetworkX returns set().
    • Every float but 0.0 and -0.0 raised TypeError: 2.0, where NetworkX returns level 2, and 0.5, 1.5, -0.5, inf, -inf and NaN, where it returns set(). So did NumPy floats and arrays, fractions, Decimals, complex numbers, strings and None (np.array([1]) and 2+0j among them, which NetworkX reads as levels 1 and 2). Of 792 calls, from four sources (one absent, one None, one unhashable) with 33 distances on two graphs, three ways, 324 differed from NetworkX.
    • An unhashable source raised TypeError, where NetworkX raises NetworkXError "The node [0] is not in the graph.": its source not in G is False for a source it cannot hash.
  • What NetworkX does, in the same code in all three versions: for i, layer in enumerate(nx.bfs_layers(G, source)), return set(layer) once i == distance, else set(). So an int or a bool gives its level, and so does any number equal to one (2.0, Fraction(4, 2), Decimal(2), 2+0j). Anything else gives set(), after a walk to the last level. A NumPy array is read by the truth of i == distance: np.array([1]) gives level 1, and np.array([1, 2]) raises ValueError.

  • Now the walk makes NetworkX's comparison at each level.

    • A real number (an int, a bool, a float, a NumPy number or a fraction) below 0, and -inf and NaN, give set() at once, as depth_limit is read in the BFS walks.
    • The walk stops once its levels pass a real distance: for 1.5 it stops at level 2, where NetworkX's goes to the end.
    • Any other distance is compared at every level, as in NetworkX, and gives NetworkX's answer or error.
    • The source is checked first, as in NetworkX, and a source not in the graph, unhashable or not, raises NetworkX's error and message.
  • Speed. An int distance walks the same levels as before, with one comparison a level more. Not measured.

  • Tests, each against NetworkX three ways: on this backend's graph, with backend="graphblas", and with this backend first in backend_priority (a counter checks that each call reached this backend). They check:

    • every level and two past the last, from three sources, on 60 random graphs, directed and undirected, with self-loops and nodes added out of order;
    • 39 distances of the kinds above, ints, bools, floats on a level and between two, below 0, infinite and NaN, NumPy scalars and arrays, fractions, Decimals, complex numbers, a string, None, an empty tuple, 10**30 and 1e300, on a tree with an edge back to the source, directed and undirected, answers and errors alike;
    • a source not in the graph (unhashable, absent, None) with distances that would otherwise raise or give set();
    • the steps the walk takes on a path of 1000 nodes: none below 0 or for NaN, one for 0.5, two for 1.5, and every level only for a distance past the end or one that is not a real number.

    On the code before this change, 73 of them fail. 14 mutants of the walk and the wrapper each fail them, in NetworkX dev, 3.7 and 3.6.1. The repo's tests pass in all three.

  • NetworkX's tests. algorithms/traversal/tests/test_bfs.py and the doctests of breadth_first_search.py, with algorithms/tests/test_dag.py and the doctests of dag.py (whose transitive_closure_dag calls descendants_at_distance), through the backend in dev, 3.7 and 3.6.1. The same tests pass before and after (121, 121 and 109), with 95, 95 and 75 calls of descendants_at_distance served. One fails before and after in each: the bfs_layers doctest, which prints each layer in the order NetworkX met its nodes.

Stacked on #171.

NetworkX walks level by level and returns the level i for which
i == distance, or no node where no level is equal to it. This backend
read the distance as a count of steps: a negative distance returned
the source, and a float, a NumPy float, a fraction, a Decimal, a
complex number, a string or None raised TypeError.

The walk now makes NetworkX's comparison at each level. Below 0, -inf
and NaN give no node at once, and the walk stops once its levels pass
a real distance, so for 1.5 it stops at level 2, where NetworkX's goes
to the end. Any other distance is compared at every level, as NetworkX
compares it.

An unhashable source raised TypeError. It now raises NetworkXError,
"The node ... is not in the graph.", as NetworkX's `source not in G`
reads it.
@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.50%. Comparing base (94558a9) to head (e586dea).

Additional details and impacted files
@@                Coverage Diff                @@
##           73-bfs-riders     #175      +/-   ##
=================================================
+ Coverage          93.45%   93.50%   +0.04%     
=================================================
  Files                186      187       +1     
  Lines              12290    12381      +91     
  Branches            1393     1400       +7     
=================================================
+ Hits               11486    11577      +91     
  Misses               549      549              
  Partials             255      255              
🚀 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