Repository navigation
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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:
|
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.
Read
descendants_at_distance'sdistanceas NetworkX does. NetworkX walks the graph a level at a time and returns the levelifor whichi == distance, orset()where no level is equal to it. This backend readdistanceas 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:
-1and-2returned{source}, where NetworkX returnsset().0.0and-0.0raised TypeError:2.0, where NetworkX returns level 2, and0.5,1.5,-0.5,inf,-infand NaN, where it returnsset(). So did NumPy floats and arrays, fractions, Decimals, complex numbers, strings and None (np.array([1])and2+0jamong 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.source not in Gis 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)), returnset(layer)oncei == distance, elseset(). 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 givesset(), after a walk to the last level. A NumPy array is read by the truth ofi == distance:np.array([1])gives level 1, andnp.array([1, 2])raises ValueError.Now the walk makes NetworkX's comparison at each level.
-infand NaN, giveset()at once, asdepth_limitis read in the BFS walks.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 inbackend_priority(a counter checks that each call reached this backend). They check:set();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.pyand the doctests ofbreadth_first_search.py, withalgorithms/tests/test_dag.pyand the doctests ofdag.py(whosetransitive_closure_dagcallsdescendants_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 ofdescendants_at_distanceserved. One fails before and after in each: thebfs_layersdoctest, which prints each layer in the order NetworkX met its nodes.Stacked on #171.