Repository navigation
73 edge bfs - #171
Open
eriknw wants to merge 1 commit into
Open
73 edge bfs#171eriknw wants to merge 1 commit into
eriknw wants to merge 1 commit into
Conversation
edge_bfs yields every edge that a breadth-first walk from the given nodes reaches, each once. The walk goes a level at a time with any_pair products over the out-edges, over the in-edges for orientation="reverse", or over both for "ignore". Each node reached then gives its row of the adjacency, or of its transpose, gathered in NumPy. NetworkX walks each node's edges in Python and, on an undirected graph, builds a frozenset for each edge to see it once. The edges are NetworkX's, and so are their orientations wherever the walk fixes them: an undirected edge, or with "ignore" a directed one, comes from the end the walk meets first, labelled "forward" from its tail and "reverse" from its head. The order is breadth-first but this backend's own: the nodes of source first, as given, then each level in node order, each node's edges in the node order of their other ends, out-edges before in-edges. So between two nodes at one depth the direction or label may differ from NetworkX's. source is read as NetworkX reads an nbunch, with its errors. An orientation other than None, "original", "reverse" or "ignore" raises NetworkXError where NetworkX raises it: on a directed graph, once source names a node. Errors come when the first edge is asked for, as from NetworkX's generator. All three walk as bfs_edges does: each node's parent is its neighbor on the level above that comes first in node order, so the tree may differ from NetworkX's. For this backend's graph, bfs_tree returns this backend's DiGraph, whose nodes are the source, as given, then each level in node order. For a NetworkX graph it returns NetworkX's DiGraph, built from those edges as NetworkX builds it, so a NetworkX caller gets the graph it would have had. bfs_predecessors yields the tree's parents, with NetworkX's DeprecationWarning where NetworkX gives one. bfs_labeled_edges labels each edge by the depths of its ends, and walks only as far as the edges read need, a part at a time, as NetworkX's generator does. Several sources are walked in the order given, so a node one step from two of them is the child of the first, and the first edge into each node is its tree edge. Each conversion now records, weakly, the NetworkX graph it was made from; bfs_tree reads that record to tell a conversion from this backend's own graph. A conversion copied along with its graph (deepcopy or pickle) has no record, so bfs_tree by name on such a copy returns this backend's DiGraph. A graph of this backend's is served without a conversion, as is a call by name. On the automatic path a NetworkX graph goes to NetworkX where converting it costs more than the walk. bfs_predecessors is served where the graph is converted either way: a conversion of it is cached, or this backend serves bfs_edges, which NetworkX's own version calls. bfs_tree is left to NetworkX, whose own version builds the same DiGraph from the bfs_edges its priority names; building it costs the same here, and a deep graph's walk costs more. bfs_labeled_edges is left to NetworkX too, whose girth reads a few edges from every node. Dispatcher.should_run takes each wrapper's rule, as can_run does. A depth limit of -inf or NaN now walks no level, as in NetworkX, where bfs_edges and bfs_successors raised OverflowError or TypeError. edge_bfs cached the node keys under the name the components and core names use for a pair of arrays, so calling one after the other on the same graph failed. It now reads that pair.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 46-bfs-own-order #171 +/- ##
====================================================
+ Coverage 92.89% 93.45% +0.56%
====================================================
Files 182 186 +4
Lines 11305 12290 +985
Branches 1290 1393 +103
====================================================
+ Hits 10502 11486 +984
- Misses 548 549 +1
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.
Add
edge_bfs, with every orientation: None,"original","reverse"and"ignore". It yields each edge that a breadth-first walk fromsourcereaches, once each. The walk goes a level at a time withany_pairproducts: over the out-edges; over the in-edges for"reverse"; over both for"ignore". Each node reached then gives its row of the adjacency, or of its transpose, and those rows are gathered in NumPy and deduplicated there. NetworkX walks each node's edges in Python, and on an undirected graph it builds a frozenset for every edge to yield it once.What is exact. The edges are NetworkX's, each once, and so are their orientations wherever the walk fixes them.
"forward", or against itself for"reverse"."ignore", a directed edge is labelled"forward"when the walk meets it at its tail and"reverse"when it meets it at its head.The order is breadth-first but this backend's own: first the nodes of
source, as given, then each level in node order. Each node's edges come in the node order of their other ends, out-edges before in-edges. Any breadth-first order is a valid answer. NetworkX's order follows the order in which each node's edges were added, which a matrix does not keep.As NetworkX reads its arguments:
sourceis read as an nbunch: None is every node in node order, a node is itself, and any other iterable gives its members that are in the graph. A repeated member counts once, and a string that is not a node is read by its characters.sourcenames one of its nodes. On an undirected graph every edge is"forward", whatever the orientation.Left to NetworkX: multigraphs, as everywhere in this backend.
Speed. Indicative: dev, best of 2 interleaved in one process, conversion on every call unless cached, load 8.3 to 12.6. The loop column is NetworkX's 5 calls from random sources against one conversion and 5 calls.
"ignore""reverse""original"transitive_closureof a graph with cycles callsedge_bfsfrom every node. With graphblas first in the priority, those calls are served from one conversion: 2.30x on 1000 nodes (5.53 s against 2.40 s) and 2.48x on 2000 nodes (17.8 s against 7.16 s). The rest of that time is NetworkX building the closure.Memory. Peak RSS of one process on the undirected random graph of 3e6 edges is 2280 MB after
edge_bfswith conversion and its edges listed, up from 1594 MB after building the graph. With"ignore"on the directed one it is 2123 MB, up from 1535 MB.Also added:
bfs_tree,bfs_predecessorsandbfs_labeled_edges. They walk asbfs_edgesdoes, a level at a time: each node's parent is its neighbor on the level above that comes first in node order (amin_firstproduct). So the tree may differ from NetworkX's, which follows the order each node's edges were added.bfs_treegives a DiGraph with an edge from each parent to its child and no edge data. Its nodes are the source, as given, then each level's nodes in node order, which is the orderbfs_edgesyields them.reverseanddepth_limitare NetworkX's.bfs_treebuilds it: an empty DiGraph, the source added, then the edges. (On the automatic path NetworkX builds it itself, below.) NetworkX hands a returned graph to the caller as it is (only its test mode converts one back), and this backend's DiGraph lacks much of NetworkX's graph interface (edges,nodes,successors,degreeand more), so a NetworkX caller gets the graph it would have had. Making this backend's graph classes complete is left to its own change.remember_sourceandsource_ofinclasses/_utils.pyand two lines ofconvert_from_nx, carried identically from 50 spanning trees #151. A graph built withfrom_networkxhas no record, so it is this backend's own._AntiGraph's is) is not recorded, as in 50 spanning trees #151, so its tree is this backend's DiGraph; and a conversion made by callingconvert_from_nxby hand is recorded, so its tree is NetworkX's. A conversion copied along with its NetworkX graph (copy.deepcopyorpickle, which copy the graph's cache) has no record either, sobfs_tree(G, s, backend="graphblas")on such a copy returns this backend's DiGraph; the automatic path is unaffected, since it leaves a NetworkX graph'sbfs_treeto NetworkX.bfs_predecessorsyields(child, parent)for that tree, inbfs_edges's order, with the source as given. Where NetworkX deprecates it (3.7 and later), it gives NetworkX's DeprecationWarning, with its message, when the first pair is asked for; in 3.6.1 it gives none. An earlier decision not to registerbfs_predecessors, as serving it would lose that warning, is superseded: the warning is now NetworkX's.bfs_labeled_edgesyields NetworkX's edges with NetworkX's labels."level"and"reverse"are fixed by the depths of the two ends, so they are exact; which edges are"tree"and which"forward"follows the tree. An undirected graph gives no reverse edges and each level edge once, from the end reached first. A self-loop is a level edge.girthcompares by identity.Their errors, as NetworkX raises them.
bfs_treeandbfs_predecessors. None raises ValueError "None cannot be a node" forbfs_tree, and an unhashable source raises TypeError.bfs_labeled_edgesreadssourcesas NetworkX does: a node, or else an iterable of nodes, each counted once. A non-iterable that is not a node raises TypeError. A source that is not a node raises KeyError, after the edges of the sources before it have been yielded.sort_neighborsis declined, as inbfs_edges: a NetworkX graph on the automatic path goes to NetworkX, and this backend's graph, or a call withbackend="graphblas", raises NotImplementedError.depth_limitof 0 or less walks no level, and so do -inf and NaN, as in NetworkX:bfs_treegives the source alone andbfs_predecessorsnothing. Before, -inf raised OverflowError and NaN TypeError inbfs_edgesandbfs_successorstoo, which share the walk.Which calls are served.
convert_from_nxcalls through each name, and through a call on the treebfs_treereturned, and finds none. A declined argument raises NotImplementedError there, as NetworkX falls back only for its own graphs.backend="graphblas": every call.should_run. The rival is NetworkX, and the measure is end to end, the conversion included, for each name.bfs_predecessorsis served where the graph is converted either way: a conversion of it is cached, or this backend is inbfs_edges's priority. NetworkX's own version callsbfs_edges, which would convert the graph and be served here. Served then, it is 0.96x to 1.18x as fast as NetworkX's own walk on thebfs_edgesserved here, about the same. Elsewhere it is left to NetworkX.bfs_treeis left to NetworkX, even with a conversion cached or this backend inbfs_edges's priority. NetworkX's ownbfs_treethen walks with thebfs_edgesits priority names, this backend's included, and builds the same DiGraph. Serving it here would be 0.85x to 1.12x as fast as that, about the same.bfs_treewhere the graph is converted either way, asbfs_predecessorsis. Building NetworkX's DiGraph costs here what it costs NetworkX, so from a cached conversionbfs_treeis 0.95x to 2.07x of NetworkX alone, against 0.04x on a path, whose walk pays this backend's cost per level once a node. (NetworkX would ask a backend forbfs_tree, which returns a graph, bybackend_priority.generatorsorbfs_tree's own priority, not byalgos.)bfs_labeled_edgesis left to NetworkX, even with a conversion cached. With the conversion it is 0.64x at best. Cached it is 1.4x to 5.0x on graphs of 1e5 nodes or more that are not a path or a grid, but NetworkX'sgirth, the one caller of it in NetworkX, reads a few edges from every node. Served from a cached conversion,girthis 0.012x to 0.019x of NetworkX's on graphs of 1600 to 20000 nodes: each call takes 157 to 206 microseconds here, against 2.1 to 3.8 in NetworkX.bfs_treeis at best 1.12x: 0.60x to 1.12x on graphs of 1e5 nodes or more with at most 10 entries per node, other than a path, since it builds NetworkX's DiGraph as NetworkX does.Their speed. Indicative: dev (NetworkX 3.7.1rc0.dev0, Python 3.14), JIT on and warmed, one process per graph. Each time is the mean over at least 0.2 s, and three interleaved rounds give the median. Load 3.1 to 4.6. The results are consumed: the generators are listed, and
bfs_tree's graph is taken as returned.bfs_predecessorsandbfs_labeled_edges, "cached" is within 5% of this backend's own graph, except from the source with no edges, where a call takes 0.03 to 0.22 ms.bfs_tree's columns come from a later run, at load 3.1 to 3.8, of code whose calls by name are this change's, where a NetworkX graph's tree is NetworkX's DiGraph. Building it costs what it costs NetworkX, so cached is 0.95x to 2.07x, except on a path (0.04x) and from the source with no edges (0.13x). On this backend's own graph, whose tree is this backend's DiGraph, it is 2.6x to 68x, again except on a path (0.04x to 0.05x) and from the source with no edges (0.09x).bfs_edgesandbfs_successorsshare that walk. Taking narrow levels another way would help all of them.bfs_treeandbfs_predecessorswith this backend inbfs_edges's priority, the conversion counted, are at best 1.13x and 0.74x of NetworkX alone.Dispatcher.should_run, with a hook per wrapper (nxapi._utils.should_run, ascan_runhas), carried identically from 50 spanning trees #151.A fix to
edge_bfs. It cached the node keys under the name the components and core names use for a pair of arrays. So on one graph,k_corethenedge_bfsraised TypeError, andedge_bfsthenk_coreraised ValueError. It now reads that pair. A test calls them in both orders, and withconnected_components.Tests. 320 seeded graphs, directed and undirected. Their node order is not their label order, their edges were added in a random order, a quarter have self-loops, and some have isolated nodes or string labels. Each is run with every orientation and with
sourceas None, one node, a list with an absent node and a repeat, or an empty list. The tests check:source, a multigraph left to NetworkX, graphs of this backend (one whose ids are not in node order), and a walk 3000 nodes deep.27 mutants of the kernel and wrapper each fail them, in dev, 3.7 and 3.6.1.
Tests of the three added names. 80 random graphs, directed and undirected, with self-loops, isolated nodes and nodes added out of order; tuple nodes; a graph of this backend's whose ids are not in node order; a path 1500 deep. The tests check:
bfs_tree: NetworkX's nodes, each but the source the child of one parent a level up joined to it in the walk's direction, with everyreverseanddepth_limit;bfs_edges's order and edges; for a NetworkX graph, NetworkX's DiGraph, equal node for node and edge for edge to the one NetworkX builds from those edges, by name and from the wrapper itself; for this backend's graph, this backend's DiGraph. On the automatic path a NetworkX graph's tree is not served (a counter on the served calls), with this backend inbfs_tree's priority, ingenerators, inbfs_edges's, and with a conversion cached, and NetworkX's own tree walks with thebfs_edgesserved here. Each conversion's record of its NetworkX graph, and that it goes with the graph.bfs_tree,bfs_predecessors,bfs_edgesandbfs_successors, and 0.5 walks one, as NetworkX's do.bfs_predecessors:bfs_edges's tree as pairs, NetworkX's children, fractional depth limits, and the source as given, its type included.bfs_labeled_edges: NetworkX's edges and labels as multisets, from one source, several (not in node order too), and a repeated one; each level edge within a level, each reverse edge back up, each tree and forward edge one level down, one tree edge into each node reached; the walk replayed edge by edge, so that an edge into a node not yet reached is its tree edge and no other edge is, before a KeyError too; the sources walked in the order given; the walk's order; the edges written out for two small graphs; that the walk goes only as far as the edges read; and the labels being NetworkX's objects, throughgirth.convert_from_nx); each routing rule, with and without a cached conversion, with this backend inbfs_edges's priority, and with NetworkX's cache turned off; that NetworkX's own versions convert the graph forbfs_edgesthere; that a cached value that is not this backend's graph counts as no conversion.edge_bfs, these names,k_coreandconnected_components, called in both orders.47 mutants of the kernel, the wrappers, the routing, the hook, the conversion's record and the shared keys each fail them. 45 fail them in dev, 3.7 and 3.6.1 alike. The other two change only whether the DeprecationWarning is given, and fail them where that differs: always giving it in 3.6.1, never giving it in dev and 3.7. The repo's tests pass in dev, 3.7 and 3.6.1.
NetworkX's tests.
algorithms/traversal/tests/test_edgebfs.py, the doctests ofalgorithms/traversal/edgebfs.py, andalgorithms/tests/test_dag.py, whosetransitive_closurecallsedge_bfs, were run through the backend in dev, 3.7 and 3.6.1. The same tests pass before and after (94, 94 and 84).test_edgebfs.pycome out in this order on its graphs.NetworkX's tests of the three added names.
algorithms/traversal/tests/test_bfs.pyand the doctests ofbreadth_first_search.py, withtest_cycles.pyand the doctests ofcycles.py(whosegirthcallsbfs_labeled_edges),test_edgebfs.py, the doctests ofedgebfs.py, andtest_dag.py, through the backend in dev, 3.7 and 3.6.1. The same tests pass before and after (189, 189 and 179).bfs_layersdoctest, which prints each layer in the order NetworkX met its nodes.bfs_labeled_edges, 7 ofbfs_predecessorsand 5 ofbfs_treein each environment. NetworkX's test mode does not askshould_run, so it servesgirth's calls andbfs_tree's too.bfs_edgesfall from 18 to 11, asbfs_treeandbfs_predecessorsno longer call it.Stacked on #146.