Skip to content

74 articulation points - #179

Open
eriknw wants to merge 1 commit into
python-graphblas:59-bridgesfrom
eriknw:74-articulation
Open

eriknw wants to merge 1 commit into
python-graphblas:59-bridgesfrom
eriknw:74-articulation

Conversation

@eriknw

@eriknw eriknw commented Dec 4, 2026

Copy link
Copy Markdown
Member

This adds articulation_points, biconnected_components, biconnected_component_edges and is_biconnected. NetworkX finds the blocks (biconnected components) with a depth-first search in Python. Here they come from the spanning forest that 59's bridges numbers in preorder, by Tarjan and Vishkin's method. All four are served on this backend's graphs and with backend="graphblas", at every size. On the automatic path, should_run leaves 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.

  • Kernel. 59 numbers a spanning forest in preorder, so that every subtree is a run of numbers, and finds the least and greatest numbers each subtree reaches by a non-tree edge. The blocks are the connected components of a graph on the forest's edges, each edge named by its lower end. That graph joins the edges above both ends of each non-tree edge whose ends are not one the other's ancestor. It also joins the edges above a node v that is not a root and above its child w, when w's subtree reaches outside v's. 59's _spanning_forest finds 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, where np.unique took 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_ids reads 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_biconnected without a search. NetworkX's is_biconnected stops 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. Now is_biconnected reads 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.
  • The answer is compared with NetworkX's as NetworkX's own tests compare it: as sets. articulation_points yields 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's union numbers its nodes in another order. biconnected_components yields sets of nodes and biconnected_component_edges lists 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_biconnected returns a Python bool, as NetworkX defines it: False under two nodes, True for a single edge.
  • Errors and laziness, as in NetworkX. A directed graph raises NetworkXNotImplemented at the call. The three generators read the graph at the first next(), so an edge added between the call and then is seen, as with NetworkX's generators. 59's bridges reads the graph at the call instead, which differs only for a direct nxapi call. 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 raises NotImplementedError with backend="graphblas", and a direct nxapi call refuses it at the call. These hold by name, with graphblas first in the priority, and for direct nxapi calls.
  • Speed (Indicative: JIT on, 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). Against backend="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 during biconnected_component_edges, so it lacks that name and is_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 the should_run base 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.
    • On this backend's graph, articulation_points is 1.25x to 1.50x NetworkX's speed at 1e3 nodes and 1.53x to 4.72x on the 41 larger graphs. biconnected_components is 1.11x to 1.50x and 1.71x to 6.34x. biconnected_component_edges is 1.14x to 1.36x and 1.72x to 4.04x (40 larger graphs).
    • is_biconnected is 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.
    • A graph NetworkX has already converted is served at about the speed of this backend's own graph: 0.84 to 1.08 times its time, on calls over 1 ms.
    • Converting costs about what NetworkX's whole search does. With the conversion counted, articulation_points is 0.38x to 1.08x, biconnected_components 0.47x to 1.40x and biconnected_component_edges 0.46x to 1.25x. is_biconnected is 0.00x to 1.64x, 0.00x where NetworkX's search ends at once.
  • Memory. Measured with a memory guard that samples the process's resident set and physical footprint (the larger) every 20 ms, under an explicit cap, one process per call: 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.
    • The process's peak, graph included, per edge: 1,061 bytes here against NetworkX's 848 at 1e5 nodes, 713 against 655 at 3e5, and 434 against 632 at 1e6. At 1e5 nodes and mean degree 16, 768 against 684.
    • What the call adds to what the process held before it: 612, 480 and 275 bytes per edge here at 1e5, 3e5 and 1e6 nodes, against NetworkX's 270, 192 and 202 (488 against 226 at mean degree 16). The cost per edge falls as the graph grows, so it is O(m).
    • Of this backend's, the output is 72 bytes per edge (a tuple per edge and the block lists, as NetworkX's). The kernel's numpy arrays peak at 160 bytes per edge (3e5 nodes, traced), in the numbering of the spanning forest. The rest is memory that the forest's rounds free and the allocator keeps. At 3e5 nodes, after the call the process holds 533 MB more than before it, of which the numpy arrays and Python objects still live (traced) come to 100 MB, mostly the output; with the output dropped, it still holds 459 MB more.
  • 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.
    • With the conversion counted, the call won on 37 of the 182 (graph, name) pairs, by 1.01x to 1.64x: 17 biconnected_components, 13 is_biconnected, 6 biconnected_component_edges and 1 articulation_points call. 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 but articulation_points (1.01x to 1.42x). The one floor that held only wins, biconnected_components from 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).
    • Leaving the 37 to NetworkX gives up at most 1.64 s of NetworkX's 5.91 s (biconnected_components on the random graph of 1e6 nodes and 4e6 edges). The best ratio given up is 1.64x (is_biconnected on a path of 1e6 nodes).
    • Once NetworkX will give the call a cached conversion of the graph, as 75 should_run base #174's has_reusable_conversion says (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 are is_biconnected on 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_edges also 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's remembered, so it runs once per version of the graph.
  • Native graphs first. A call on this backend's graph, through NetworkX's dispatcher or directly, is served with no conversion: a test spies on convert_from_nx and counts no call, for each name. should_run returns True there, and NetworkX does not ask it.
  • Tests, in 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; and is_biconnected the same bool.
    • There are also edge cases (no node, one node, a lone self-loop, two components, a wheel, a complete bipartite graph), a path and a cycle of 200,000 nodes, is_biconnected's early answers, and a union graph. 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.
    • 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 of 59's random graphs; stored by column, the grid and the ladder were wrong on all four names before the forest's edges were grouped by row.
    • For the automatic path: the declines on small graphs, which give NetworkX's answer with no conversion; a graph of 300,000 nodes and 2 edges per node, left; the self-loop rule; a graph NetworkX has converted, served from 10,000 nodes, its self-loop fact kept for the graph's version and dropped when the graph changes; a view's conversion, not reused; and each decline's message.
    • The package's tests pass in NetworkX dev, 3.7 and 3.6.1. 42 mutants of the kernel (among them one that drops the forest's grouping by row), the wrappers and the should_run rules each fail them in all three. Three more pass, as expected: two drop an early answer of is_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.
  • NetworkX's tests of these names and of the algorithms that call them (test_biconnected, test_cycles, both test_kcomponents, test_cuts, test_connectivity, test_kcutsets and test_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 14 articulation_points, 28 is_biconnected, 3 biconnected_component_edges and 1,841 biconnected_components calls (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.
  • Also changed in 59's files. _faster_from takes no floors, and then serves a call that must convert never, and an unless rule: a reason to leave a graph to NetworkX all the same. 59's _bridge_ids reads _forest_numbers.
  • Merges. Against the branches of the other open pull requests, 46 of 74 merge without a conflict, and 18 more conflict only where 59 does. This change adds conflicts with 10: in the generated _nx_graphblas files and the list in tests/_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 two components/__init__.py, interface.py and the README tree. With 55 (local_bridges) it conflicts where 59 does, in both bridges.py files; 74 changes them further, and that merge was not tried.

Stacked on #178.

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.
@eriknw eriknw changed the title 74 articulation 74 articulation points Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.70%. Comparing base (a2409cf) to head (54e518a).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant