Repository navigation
59 bridges - #178
Open
eriknw wants to merge 1 commit into
Open
59 bridges#178eriknw wants to merge 1 commit into
eriknw wants to merge 1 commit into
Conversation
A tree edge of any spanning forest is a bridge iff no non-tree edge leaves the subtree below it. With each tree's nodes numbered in preorder, a subtree is a run of numbers, so the test is whether the least and greatest numbers its nodes reach by a non-tree edge lie in the run (Tarjan and Vishkin). The forest comes from Boruvka's rounds, each tree's Euler tour, ranked by pointer jumping, gives the numbers and the subtree sizes, and a sparse table gives each run's least and greatest reach. The passes grow with log n rather than with depth, so a path graph is no slower than a shallow graph, where a breadth-first forest would take a step per level. The edges are grouped by row here, as to_coo lists them in the order the matrix keeps them, so a matrix stored by column, hypersparse or as a bitmap gives the same answer as one stored by row. With root, a breadth-first walk from it finds its component. A component of under half the nodes that the walk finds within about 2 log2(n) levels is searched alone; otherwise the whole graph is, and the component's bridges are kept. Each bridge comes once, as (u, v) with u before v in node order, which is NetworkX's orientation without root, sorted by u and then v. A root not in the graph raises at the first next(), as NetworkX's generator does. A multigraph is declined, as everywhere in this backend; a direct call refuses it too, since the matrix would merge parallel edges. On the automatic path should_run leaves a NetworkX graph to NetworkX where converting it costs more than the search saves. bridges is served from 100000 nodes with 1.2 edges per node. has_bridges is served wherever bridges would serve the call NetworkX's has_bridges makes to it, which is where this backend is in bridges' priority, and elsewhere from 200000 nodes with 2 edges per node or 300000 with 1.2, so never a forest that must be converted. A call NetworkX will give a cached conversion, as has_reusable_conversion says, is served from 10000 nodes. With root, NetworkX searches the root's component alone, so a call that would convert is left to it, unless this backend serves node_connected_component, which NetworkX's bridges then calls, and the graph is not a view: NetworkX keeps a view's conversion when the graph under it changes, and its bridges takes only the component's nodes from that call. On this backend's graphs, or with backend="graphblas", both are always served. The dispatch table's generator now finds an nxapi module that a function of its own name hides, as bridges and reciprocity are, instead of keeping a list of their names.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 75-should-run-base #178 +/- ##
======================================================
+ Coverage 93.07% 93.50% +0.43%
======================================================
Files 184 187 +3
Lines 11568 12279 +711
Branches 1290 1351 +61
======================================================
+ Hits 10767 11482 +715
+ Misses 549 545 -4
Partials 252 252 🚀 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.
This adds
bridgesandhas_bridges. NetworkX finds the chains of a depth-first search in Python. Here a bridge is found as Tarjan and Vishkin find it: take any spanning forest and number each tree's nodes in preorder, so that every subtree is a run of numbers. The tree edge above a subtree is a bridge iff no non-tree edge leaves that run, that is iff the least and greatest numbers the subtree's nodes reach by a non-tree edge both lie inside it. Both names are served on this backend's graphs and withbackend="graphblas", at every size. On the automatic path,should_runleaves a NetworkX graph to NetworkX where converting it costs more than the search saves.bridgestakes 0.09 s. The edges come fromto_coo, which lists a matrix's entries in the order the matrix keeps them: by row, or by column for a matrix stored by column. The kernel groups them by row itself (a stable sort, made only when they are not grouped already), so a matrix stored by column, hypersparse or as a bitmap gives the answer a matrix stored by row does.root. NetworkX searches the root's component alone. Here a breadth-first walk from the root finds the component. If the walk ends within about 2 log2(n) levels and the component has under half the nodes, only the component's edges are searched; otherwise the whole graph is, and the component's bridges are kept. On this backend's graph of a 400 x 400 grid and a path of 5 nodes, with the root on the path,bridgestakes 0.49 ms, where searching the whole graph took 39 ms and NetworkX takes 0.09 ms. Where the component is the whole graph, such as a path or a grid of 1e6 nodes, the walk stops after 40 levels and the search takes 0.30 s and 0.32 s, where NetworkX takes 3.6 s and 6.3 s, as it first copies the component.(u, v)withubeforevin the graph's node order. Withoutrootthat is NetworkX's orientation too, sinceG.edges()gives each edge from its end that comes first in node order. The bridges come sorted byuand then byv, in node order, where NetworkX's follow each node's adjacency. Node order is the graph's own, also where a graph built by this backend'sunionnumbers its nodes in another order. Withroot, NetworkX walks a copy of the root's component whose nodes come in a set's order, so its orientation can differ there (47 of 996 bridges on shuffled string-labelled paths, withPYTHONHASHSEED=0; the count changes with string hashing). Self-loops are never bridges.has_bridgesreturns a Python bool.NetworkXNotImplementedat the call. Forbridges, arootnot in the graph raisesNodeNotFound("Root node 9 is not in graph")at the firstnext(), since NetworkX'sbridgesis a generator;has_bridgesraises it at the call. An unhashablerootgets the same error. These hold by name, with graphblas first in the priority, and for directnxapicalls on NetworkX and backend graphs, in NetworkX dev, 3.7 and 3.6.1.NotImplementedErrorwithbackend="graphblas". A directnxapicall refuses it at the call with the dispatcher's message.gb.ss.config["jit_c_control"] = "on", warmed first; NetworkX dev; benchslot heavy, one process per graph under a 6 GB watchdog, peak 3.5 GB; 3 interleaved rounds, each timing repeated to fill 0.2 s, medians; load 2.9 to 9.4). Againstbackend="networkx", with the output read in full, on paths, random trees, random graphs of 1 to 10 edges per node, grids, Watts-Strogatz and Barabasi-Albert graphs of 1e3 to 1e6 nodes (69 graphs). On this backend's graph,bridgesis 0.93x to 3.09x NetworkX's speed at 1e3 nodes, 1.31x to 7.13x at 1e4, 1.73x to 9.38x at 1e5 and 1.94x to 7.25x at 1e6;has_bridges0.67x to 2.84x, 1.03x to 8.35x, 1.29x to 9.16x and 1.35x to 7.49x. The least is a random tree each time; the most is a random graph of 5 or 10 edges per node, or at 1e6 a grid. A NetworkX graph NetworkX has already converted is served at the same speed, within noise. These were measured beforerootwas searched alone and the edges grouped by row; on five graphs of 1e5 to 1e6 nodes the calls withoutrootnow take 0.91 to 1.11 times as long as before, at loads of 3.7 to 7.4. Converting one costs about what NetworkX's whole search does, edge for edge: with the conversion counted, forests and graphs of one edge per node are 0.35x to 1.12x (bridges) and 0.26x to 0.84x (has_bridges) at every size, and most graphs under 1e5 nodes lose too.should_run, the automatic path. It answers four questions. The rival is NetworkX, where a decline goes; forhas_bridgesand for a call withroot, that can be a call NetworkX then makes to this backend, as below. The regime is the dispatched call with its conversion counted: NetworkX asks only where it would convert a NetworkX graph. The rule is per name, ashas_bridgesloses more: NetworkX'shas_bridgesbuilds every chain but stops its scan of the edges at the first bridge. The variable is two-dimensional: nodes, for the fixed cost, and edges per node, which keeps forests out. Sobridgesis served from 100,000 nodes with 1.2 edges per node, andhas_bridgesfrom 200,000 nodes with 2 edges per node or 300,000 with 1.2. Once NetworkX will give the call a cached conversion of the graph, as 75 should_run base #174'shas_reusable_conversionsays (never for a view, whose cache NetworkX keeps when the graph under it changes), both are served from 10,000 nodes: there the cached call was 1.29x to 9.22x forbridgesand 1.02x to 9.16x forhas_bridges, and at 1e3 nodes it still lost on paths and trees, and forhas_bridgeson a sparse random graph. Edges are counted from the adjacency lengths, and only past the least floor, asG.number_of_edges()took 20 to 24 times as long: 0.18 s on a graph of 1e6 nodes. Across the 138 measured (graph, name) pairs the rule never serves one that is slower with the conversion. It leaves 23 to NetworkX that would have won, by 1.00x to 1.83x: 13 on graphs of 1e4 to 7e4 nodes, 5has_bridgescalls on graphs of 1e5 to 2e5 nodes, 3 on grids just under a floor (99,856 and 199,809 nodes), and 2bridgescalls on random graphs of one edge per node, 3e5 and 1e6 nodes, where a tree of that size loses. The most it gives up is 0.29 s of NetworkX's 0.85 s, onhas_bridgesof a random graph of 1e5 nodes and 1e6 edges. A declined call says why: that NetworkX is the faster before the conversion, or even from the conversion it holds, or that it searches the root's component alone.has_bridgestakes the first of the dispatchedbridges, so wherever this backend is inbridges' priority a declinedhas_bridgescomes back as abridgescall, whichbridges' rule serves on a graph of its size, converting it. Sohas_bridgesis served whereverbridges' rule would serve that call, and has its own floors elsewhere. Likewise NetworkX'sbridgeswithrootfinds the component with the dispatchednode_connected_component, which this backend serves with no rule of its own: where this backend is in that name's priority, a declined call converts the graph anyway, so it is served. A test makes each of those declined calls through NetworkX and counts the conversion. A view is the exception: NetworkX keeps a view's conversion when the graph under it changes, and its ownbridgestakes only the component's nodes fromnode_connected_component, so a stale conversion would mislead this backend's search alone. A test converts a view, removes an edge from the graph under it, and checks that the call withrootis declined and answered as NetworkX answers it.rooton the automatic path. The rule cannot see the component's size without a walk, and NetworkX on a small component takes about 0.1 ms, so a call withrootthat would have to convert is left to NetworkX: with the root on the path of a 400 x 400 grid and a path of 5 nodes, the call that converts took 152 ms where NetworkX took 0.11 ms. On a cached conversion it is served from 10,000 nodes: 0.4 to 0.5 ms where the component is small (NetworkX 0.08 to 0.09 ms), and 13x to 27x NetworkX's speed where it is the whole graph (a path, a grid and a random graph of 1e6 nodes).convert_from_nxand counts no call.should_runreturns True there, and NetworkX does not ask it.Dispatcher.should_run, thenxapi._utils.should_rundecorator andhas_reusable_conversion. This change adds no copy of them.test_bridge_set.py; 55 addstest_bridges.py). 320 seeded random graphs: trees (every edge a bridge), forests with isolated nodes, cycles (none) with tails and chords, grids with tuple nodes, sparse random graphs, barbells, self-loops, string labels in shuffled node orders, linked rings and mixed graphs. Each is checked against NetworkX through the dispatcher: the same unordered pairs, each once, in node order, NetworkX's orientation withoutroot, the graph's own node objects, andhas_bridgesa bool equal to NetworkX's. A quarter of them are checked with roots too. There are also a path of 200,000 nodes, a star, a 20,000-node sparse graph, auniongraph whose ids do not follow its node order, every error above in each of the ways above, the multigraph decline, and the kernel's parts (its range min and max against slicing, its forest against NetworkX's components). The same answers come with the matrix stored by row and by column, each sparse, hypersparse and as a bitmap, on a barbell, a 4 x 4 grid, a ladder and every tenth random graph, with and without roots; stored by column, the first three had bridges that are not. A spy on the kernel shows which part of the graph a call withrootsearches: a small component alone, also one exactly as deep as the walk goes, and the whole graph for a component one level deeper or of half the nodes or more. For the automatic path: each floor and the space just under it, by name; a graph NetworkX has converted, and the same with the cache turned off, after the graph changes, where NetworkX recorded a failed conversion, and on a view; each decline's message;root, declined until the graph is converted and served after, served wherenode_connected_componentwould convert the graph, and declined on a view whose conversion is stale;has_bridgesserved wherebridgeswould be; and the declined calls, which give NetworkX's answer with no conversion. The package's tests pass in NetworkX dev, 3.7 and 3.6.1. 57 mutants of the kernel (the grouping by row and the walk fromrootamong them), the wrapper and theshould_runrules (the view exception among them) each fail them in all three; two of them, a forest with a cycle in it, never finish and are stopped after 120 s.test_bridges,test_chains,test_polynomials,test_edge_augmentation,test_edge_kcomponents), through the backend in NetworkX's test mode, with fallback to NetworkX on and off: the same tests pass before and after (75 in dev, 56 with one skipped in 3.7 and 3.6.1). 148bridgescalls (149 in dev) and 22has_bridgescalls are served in each. NetworkX's whole suite through the backend in dev, fallback on, gives every test the outcome it had before this change.nxapimodule that a function of its own name hides, asbridgesandreciprocityare, instead of keeping a list of their names. The kernel's function isbridge_ids, soalgorithms.bridgesstays the module.local_bridges). Both add the twobridges.pyfiles, so a merge conflicts there (add/add), and in the generated README tree and dispatch table. Keeping both sides of the two files and 59's side of the generated text, then runningmake all, resolves it: the table then reacheslocal_bridgesasnxapi.local_bridgeswith no edit by hand. A trial merge of the two branches (55 at 663d3c0) is lint-clean and passes both bridge test files and the repo's dispatcher tests in NetworkX dev, 3.7 and 3.6.1._nx_graphblasfiles and the list intests/_dispatched.py; inscripts/maketree.py, which 21 strongly connected #124 and 65 aperiodicity and flow hierarchy #163 also change; and with 55, as above. 33 girth regular #131, 50 spanning trees #151 and 67 moral graph and inter-community non-edges #165 conflict where 75 should_run base #174 does, and this change adds none there.Stacked on #174.