Repository navigation
Conversation
articulation_points, biconnected_components, biconnected_component_edges and is_biconnected, by Tarjan and Vishkin's method on the spanning forest that bridges numbers in preorder. The blocks are the connected components of a graph on the forest's edges that joins the edges above both ends of each non-tree edge whose ends are not one the other's ancestor, and the edges above a node and its child where the child's subtree reaches outside the node's by a non-tree edge. A non-tree edge lies in the block of its end later in preorder, and a node is a cut node iff its forest edges lie in two blocks or more. As for bridges, the passes grow with log n rather than with depth. Building a GraphBLAS matrix groups each block's nodes and edges, faster than a numpy sort. The output matches NetworkX's as sets: each cut node once, in node order, where NetworkX's follow its search, and the blocks and their edges in an order of their own. A self-loop lies in one block of its node, where NetworkX puts it in the block its search meets it by, or in none at a node its search starts from. The generators read the graph at the first next() and raise for a directed graph at the call, as NetworkX's do. is_biconnected answers without a search where a node has no edge to another, or one among three nodes or more, or the graph is in pieces. On the automatic path should_run leaves to NetworkX every NetworkX graph that would have to be converted: with the conversion counted the wins were small, and no floor of nodes and edges per node kept out the losses on more than one kind of graph. A call NetworkX will give a cached conversion is served from 10000 nodes, except that biconnected_component_edges leaves a graph with a self-loop to NetworkX, a scan kept once per version of the graph. On this backend's graphs, or with backend="graphblas", all four are always served. The forest numbering in bridges is now a function of its own that returns what the blocks need, with each root's subtree size, and _faster_from takes no floors and a reason to leave a graph all the same.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 59-bridges #179 +/- ##
==============================================
+ Coverage 93.50% 93.70% +0.19%
==============================================
Files 187 190 +3
Lines 12279 12673 +394
Branches 1351 1376 +25
==============================================
+ Hits 11482 11875 +393
Misses 545 545
- Partials 252 253 +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.
This adds
articulation_points,biconnected_components,biconnected_component_edgesandis_biconnected. NetworkX finds the blocks (biconnected components) with a depth-first search in Python. Here they come from the spanning forest that 59'sbridgesnumbers in preorder, by Tarjan and Vishkin's method. All four are served on this backend's graphs and withbackend="graphblas", at every size. On the automatic path,should_runleaves to NetworkX every NetworkX graph that would have to be converted. A call NetworkX will give a cached conversion is served from 10,000 nodes.vthat is not a root and above its childw, whenw's subtree reaches outsidev's. 59's_spanning_forestfinds the components. A non-tree edge lies in the block of its end later in preorder, and a node is a cut node iff its forest edges lie in two blocks or more. Every step is a numpy pass, and their number grows with log n, not with the depth of the trees. Each block's nodes and edges are grouped by building a GraphBLAS matrix of (block, member) pairs: 0.018 s on a random tree of 1e6 nodes, wherenp.uniquetook 0.393 s. 59's numbering is now a function of its own,_forest_numbers, which also returns the non-tree edges and each root's subtree size._bridge_idsreads it. The forest's edges come grouped by row however the matrix is stored, as 59 groups them, and the blocks are grouped from the matrix's rows (to_csr), which do not depend on its storage either.is_biconnectedwithout a search. NetworkX'sis_biconnectedstops at the second block its search finds, which on a tree, or a sparse random graph with an isolated node, comes at once. A first version of this kernel computed every block there: 0.37 to 0.85 s on a random tree of 1e6 nodes and a random graph of 5e5 nodes, where NetworkX took under a millisecond. Nowis_biconnectedreads the degrees first. A node with no edge to another node answers False, and so does a node with one among three nodes or more. So does a forest of fewer than n - 1 edges, a graph in pieces. Only then are the blocks computed. On trees and random graphs of 1.2 edges per node the answer takes 0.05 to 0.28 ms, against NetworkX's 0.03 to 0.10 ms (0.22x to 1.05x). Past those checks, NetworkX's search can run long before its second block: on two barbells (two cliques joined by a long path) this backend was 4.9x and 5.2x as fast.articulation_pointsyields each cut node once, in the graph's node order, where NetworkX's follow its search. Node order is the graph's own, also where a graph built by this backend'sunionnumbers its nodes in another order.biconnected_componentsyields sets of nodes andbiconnected_component_edgeslists of pairs. The blocks come in an order of their own, and each block's edges in an order and an orientation of their own. A self-loop at a node with other edges lies in one block of that node: the block of the node's forest edge up or, at a root, of one of its forest edges down. NetworkX puts it in the block of the edge its search meets it by, or in none at a node its search starts from. A self-loop at a node with no other edge is in no block, as in NetworkX.is_biconnectedreturns a Python bool, as NetworkX defines it: False under two nodes, True for a single edge.NetworkXNotImplementedat the call. The three generators read the graph at the firstnext(), so an edge added between the call and then is seen, as with NetworkX's generators. 59'sbridgesreads the graph at the call instead, which differs only for a directnxapicall. Multigraphs are declined, as everywhere in this backend, since the matrix would merge parallel edges, which make a block of their own. Through the dispatcher a multigraph goes to NetworkX, or raisesNotImplementedErrorwithbackend="graphblas", and a directnxapicall refuses it at the call. These hold by name, with graphblas first in the priority, and for directnxapicalls.gb.ss.config["jit_c_control"] = "on", warmed first; NetworkX dev; benchslot heavy, one process per graph under a 6 GB watchdog, peak 4.7 GB; 3 interleaved rounds, each timing repeated to fill 0.2 s, or 2 s of wall time, medians; load 2.9 to 10.0). Againstbackend="networkx", with the output read in full and checked as sets. 46 graphs: paths, random trees, cycles, grids, random graphs of 1.2 to 10 edges per node, Watts-Strogatz and Barabasi-Albert graphs of 1e3 to 1e6 nodes, two barbells of 1e4 and 1e5 nodes, and a random graph of 3,000 nodes and 450 edges per node. One, a random graph of 1e6 nodes and 4e6 edges, passed the 6 GB cap duringbiconnected_component_edges, so it lacks that name andis_biconnected: that process also held two NetworkX graphs of it, one with its conversion (see Memory). The kernel benched is the committed one, on 59 before 59 moved onto theshould_runbase and grouped the forest's edges by row; on a matrix stored by row, as every converted graph's is, that grouping is one pass that finds them grouped already.articulation_pointsis 1.25x to 1.50x NetworkX's speed at 1e3 nodes and 1.53x to 4.72x on the 41 larger graphs.biconnected_componentsis 1.11x to 1.50x and 1.71x to 6.34x.biconnected_component_edgesis 1.14x to 1.36x and 1.72x to 4.04x (40 larger graphs).is_biconnectedis 1.43x to 1.84x at 1e3 nodes, and 2.45x to 6.37x on 21 larger graphs, where every node has two neighbors or more. On paths it is 5.3x to 2842x, as it answers from the degrees and NetworkX searches the whole path. Where a node has one neighbor or none, it is 0.22x to 1.05x on trees and random graphs of 1.2 edges per node, as above. On random graphs of 2 and 4 edges per node, which also have such nodes, it is 0.92x to 30.3x, where NetworkX's search took 0.06 to 2.6 ms.articulation_pointsis 0.38x to 1.08x,biconnected_components0.47x to 1.40x andbiconnected_component_edges0.46x to 1.25x.is_biconnectedis 0.00x to 1.64x, 0.00x where NetworkX's search ends at once.list(biconnected_component_edges(G)), the output held, on random graphs of mean degree 8 (gnm_random_graph(n, 4n)), this backend's graph built from a matrix and NetworkX's from the same edges.should_run, the automatic path. It answers four questions. The rival is NetworkX, where a decline goes. 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, and all four come out the same. The variable would be nodes and edges per node, as for 59, but no floor of the two kept out the losses on more than one kind of graph, so none is set.biconnected_components, 13is_biconnected, 6biconnected_component_edgesand 1articulation_pointscall. Watts-Strogatz graphs of 3e5 and 1e6 nodes and 2 edges per node lost on every name (0.53x to 0.87x), where grids of their size and edges per node won on all butarticulation_points(1.01x to 1.42x). The one floor that held only wins,biconnected_componentsfrom 3e5 nodes and 4 edges per node, has only random graphs above it (1.16x to 1.40x), while a barbell of 1e5 nodes and 10.8 edges per node lost (0.82x).biconnected_componentson the random graph of 1e6 nodes and 4e6 edges). The best ratio given up is 1.64x (is_biconnectedon a path of 1e6 nodes).has_reusable_conversionsays (never for a view, whose cache NetworkX keeps when the graph under it changes), all four are served from 10,000 nodes, the floor 59's names use. Of the 158 pairs served that way, 150 are faster (1.21x to 2890x). The other 8 areis_biconnectedon trees and random graphs of 1.2 edges per node, slower by at most 0.23 ms (0.30 ms against NetworkX's 0.06 ms, on a tree of 1e6 nodes). Telling those graphs apart takes a scan for a node of fewer than two neighbors: 34 to 40 ms on a graph of 1e6 nodes without one. So the rule does not look. Under 10,000 nodes a cached call is left to NetworkX, though it was faster on 22 of the 24 pairs measured at 1,000 to 3,000 nodes. A declined call says why: that NetworkX is the faster before the conversion, or even from the conversion it holds.biconnected_component_edgesalso leaves a graph with a self-loop to NetworkX, so that the automatic path gives NetworkX's placing of it. That scan takes 22 to 79 ms on graphs of 1e6 nodes. It is kept in the graph's__networkx_cache__by the base'sremembered, so it runs once per version of the graph.convert_from_nxand counts no call, for each name.should_runreturns True there, and NetworkX does not ask it.test_biconnected.py. The graphs: the 320 seeded random graphs of 59's tests, the 1,253 graphs of NetworkX's atlas (every graph of up to 7 nodes, up to isomorphism), and 60 graphs joined from those at a node or by an edge. Each is checked against NetworkX through the dispatcher: the same cut nodes, each once, in node order, as the graph's own node objects; the same blocks as sets; the same edges, each once, a self-loop in a block of its node; andis_biconnectedthe same bool.is_biconnected's early answers, and auniongraph. One test gives the kernel a forest by hand, whose root has non-tree edges into two of its subtrees. The forest Boruvka's rounds build kept every edge at a root on each graph tried, so this one checks the kernel's use of a root's subtree size.should_runrules each fail them in all three. Three more pass, as expected: two drop an early answer ofis_biconnected, which changes only its speed, and one applies the second rule at a root too, where no reach can leave the root's tree.test_biconnected,test_cycles, bothtest_kcomponents,test_cuts,test_connectivity,test_kcutsetsandtest_edge_kcomponents) run through the backend in NetworkX's test mode, with fallback to NetworkX on and off. The same tests pass before and after: 350 with 3 skipped in dev and 3.7, 198 with 14 skipped in 3.6.1. In each, the backend serves 14articulation_points, 28is_biconnected, 3biconnected_component_edgesand 1,841biconnected_componentscalls (1,809 in 3.6.1). NetworkX's whole suite through the backend in dev, fallback on, gives every test the outcome it had at 59's tip._faster_fromtakes no floors, and then serves a call that must convert never, and anunlessrule: a reason to leave a graph to NetworkX all the same. 59's_bridge_idsreads_forest_numbers._nx_graphblasfiles and the list intests/_dispatched.py, where other branches add names next to these, and with 21 strongly connected #124 and 65 aperiodicity and flow hierarchy #163, which add modules to the components packages too, also in the twocomponents/__init__.py,interface.pyand the README tree. With 55 (local_bridges) it conflicts where 59 does, in bothbridges.pyfiles; 74 changes them further, and that merge was not tried.Stacked on #178.