Repository navigation
Conversation
single_source_shortest_path, single_target_shortest_path, all_pairs_shortest_path, shortest_path without a weight, and predecessor walk the graph a level at a time, each node taking as its parent its neighbor on the level above that is first in node order, and build NetworkX's dicts and lists from the levels. Where there are several shortest paths the one given may not be NetworkX's, which follows the order the edges were added; predecessor gives every predecessor, as NetworkX does. Errors, cutoffs and when the all-pairs generator raises are NetworkX's. A path between two nodes of a NetworkX graph is left to NetworkX, whose search from both ends costs less than converting the graph.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 46-bfs-own-order #167 +/- ##
====================================================
+ Coverage 92.89% 93.22% +0.32%
====================================================
Files 182 183 +1
Lines 11305 11876 +571
Branches 1290 1366 +76
====================================================
+ Hits 10502 11071 +569
- Misses 548 549 +1
- Partials 255 256 +1 🚀 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.
Add
single_source_shortest_path,single_target_shortest_path,all_pairs_shortest_path,shortest_pathwithoutweight, andpredecessor. Each walks the graph a level at a time, asbfs_edgesdoes: the frontier times the adjacency matrix, each newly reached node taking as its parent its neighbor on the level above that is first in node order. NetworkX's dicts and lists are then built from the levels in Python, a level at a time: a level of 4096 nodes or more in an array with a row per path, a smaller one a path at a time, which costs less where the levels are many and short.predecessorgives each node's predecessors as NetworkX does, all of its neighbors one level nearer the source (its in-neighbors, on a directed graph), but in node order rather than in the order NetworkX's walk reaches them; the levels (return_seen) are NetworkX's. The dicts come by level, each level in node order. The source is kept as passed (1.0 for node 1) in the keys and the paths, as NetworkX keeps it.shortest_pathto a target "Source 9 not in G", since NetworkX walks out of the target on the reversed graph.predecessorof a target the walk does not reach, or that is not in the graph, is[](([], -1)withreturn_seen); an unhashable target raises NetworkX's TypeError, after the cutoff is read. An unknownmethodraises NetworkX's ValueError. Cutoffs: the paths go on whilecutoff > level, so 1.5 reaches level 2 and NaN, 0 or a negative cutoff only the source;predecessorstops after the first level at leastcutoff, so a cutoff below 1 still walks one level, and a falsy cutoff or NaN sets no bound. A cutoff that does not compare with an int raises NetworkX's TypeError, from the same first comparison.all_pairs_shortest_pathis a generator, as NetworkX's is: nothing is read until the first item is asked for, so a cutoff NetworkX cannot compare raises there, and on a graph without nodes never. The walks are made for a batch of sources at once, a row of a matrix each, so that a level costs one product for the batch; a batch holds at most about 2**20 reached ids (16 MiB), and a source's paths are built when it is yielded, so one source's dict is held at a time.shortest_pathwith neither end gives this generator.shortest_pathwithweight, and a path between two nodes of a NetworkX graph: NetworkX's search from both ends reaches a few nodes, where a conversion costs every edge (see the speed below). On a graph of this backend's own, a pair is walked from both ends, widening the smaller frontier, until one walk reaches a node the other has reached, which lies on a shortest path; where there is none, NetworkXNoPath with NetworkX's message.all_pairs_shortest_path, every item taken and dropped (the conversion is a small share): G(3000, 15000) 1.83 s where NetworkX takes 3.09 (1.68x), directed 1.89 where 2.84 (1.50x), G(5000, 25000) 5.65 where 9.46 (1.68x), G(2000, 40000) 0.45 where 0.92 (2.05x), a 60 by 60 grid 4.75 where 5.26 (1.11x). In a grid the paths are long and NetworkX's walk is cheap, so building the paths costs about as much as NetworkX's whole call. A review measured 1.24x to 1.4x on a sparser G(2000, 6000) (1.6x cached).single_source_shortest_path2.98 s where NetworkX takes 1.20 (0.40x),single_target_shortest_path0.43x,predecessor0.49x, and on the directed graph 0.41x to 0.43x; the conversion takes 1.6 to 2.5 s of each. On a cached conversion they are 2.35x, 2.54x and 2.10x (directed 1.37x to 1.69x). From a node beside a corner of a 300 by 300 grid NetworkX is faster on a cached conversion too: 0.91x, 0.94x and 0.81x.should_runis not on this base, so a single walk on a graph not yet converted is answered here, and is slower.predecessor's lists equal NetworkX's as sets and hold no node twice, and its levels are NetworkX's, as Python ints; a pair with no path raises NetworkX's NetworkXNoPath. The same with the walks in batches of 1, 7 and 30 ids, and with levels of 1, 2 or 5 nodes and more built in arrays. Each name's errors for absent, unhashable and None nodes, by type and message; each cutoff above and "a", [], [1], 7.0 and -inf; when the generator reads its cutoff, and that a batch is made only when its first node is asked for; the source as passed; the declines; the backend's own graph. 43 versions of the kernels, the wrappers and the dispatch table, in NetworkX dev, 3.7 and 3.6.1: the unchanged code and four other valid choices pass (each node's parent last in node order rather than first, the array and list builds swapped, a pair walked from one end only, from either end); 37 mutants fail the tests; one passes and is equivalent, meeting against the other walk's newest level only, since a node that walk had reached on an older level would give a path shorter than the levels already walked allow.test_unweighted.pypasses before and after (17 tests), with the five names served 16 times; the whole suite passes the same before and after (9569, 9430 and 6815 passed), with 265 calls of the five names served in each, most of thempredecessorfrom algorithms that call it. None needed an xfail: NetworkX's tests check a grid path withvalidate_grid_pathand a node's predecessors in either order. With each node's parent taken last in node order rather than first, the whole dev suite passes the same.Stacked on #146.