Skip to content

59 bridges - #178

Open
eriknw wants to merge 1 commit into
python-graphblas:75-should-run-basefrom
eriknw:59-bridges
Open

eriknw wants to merge 1 commit into
python-graphblas:75-should-run-basefrom
eriknw:59-bridges

Conversation

@eriknw

@eriknw eriknw commented Dec 4, 2026

Copy link
Copy Markdown
Member

This adds bridges and has_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 with backend="graphblas", at every size. On the automatic path, should_run leaves a NetworkX graph to NetworkX where converting it costs more than the search saves.

  • Kernel. The forest comes from Boruvka's rounds, with each edge's position as its weight. Each tree's Euler tour, ranked by pointer jumping, gives the preorder numbers and the subtree sizes. A sparse table, built a level at a time, gives each run's least and greatest reach. Every step is a numpy pass, and how many there are grows with log n, not with the depth of the trees. A breadth-first forest takes a step per level instead: about 26 us a level here, so 2.7 s on a path of 100,000 nodes, where NetworkX's bridges takes 0.09 s. The edges come from to_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, bridges takes 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.
  • The answer. The set of bridges is NetworkX's, as unordered pairs. Each bridge comes once, as (u, v) with u before v in the graph's node order. Without root that is NetworkX's orientation too, since G.edges() gives each edge from its end that comes first in node order. The bridges come sorted by u and then by v, 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's union numbers its nodes in another order. With root, 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, with PYTHONHASHSEED=0; the count changes with string hashing). Self-loops are never bridges. has_bridges returns a Python bool.
  • Errors, as in NetworkX. A directed graph raises NetworkXNotImplemented at the call. For bridges, a root not in the graph raises NodeNotFound("Root node 9 is not in graph") at the first next(), since NetworkX's bridges is a generator; has_bridges raises it at the call. An unhashable root gets the same error. These hold by name, with graphblas first in the priority, and for direct nxapi calls on NetworkX and backend graphs, in NetworkX dev, 3.7 and 3.6.1.
  • Multigraphs are declined, as everywhere in this backend. A parallel edge is never a bridge, and the matrix would merge it into one edge. Through the dispatcher a multigraph goes to NetworkX, or raises NotImplementedError with backend="graphblas". A direct nxapi call refuses it at the call with the dispatcher's message.
  • 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 3.5 GB; 3 interleaved rounds, each timing repeated to fill 0.2 s, medians; load 2.9 to 9.4). Against backend="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, bridges is 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_bridges 0.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 before root was searched alone and the edges grouped by row; on five graphs of 1e5 to 1e6 nodes the calls without root now 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; for has_bridges and for a call with root, 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, as has_bridges loses more: NetworkX's has_bridges builds 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. So bridges is served from 100,000 nodes with 1.2 edges per node, and has_bridges from 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's has_reusable_conversion says (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 for bridges and 1.02x to 9.16x for has_bridges, and at 1e3 nodes it still lost on paths and trees, and for has_bridges on a sparse random graph. Edges are counted from the adjacency lengths, and only past the least floor, as G.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, 5 has_bridges calls on graphs of 1e5 to 2e5 nodes, 3 on grids just under a floor (99,856 and 199,809 nodes), and 2 bridges calls 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, on has_bridges of 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.
  • Calls NetworkX makes back to this backend. NetworkX's has_bridges takes the first of the dispatched bridges, so wherever this backend is in bridges' priority a declined has_bridges comes back as a bridges call, which bridges' rule serves on a graph of its size, converting it. So has_bridges is served wherever bridges' rule would serve that call, and has its own floors elsewhere. Likewise NetworkX's bridges with root finds the component with the dispatched node_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 own bridges takes only the component's nodes from node_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 with root is declined and answered as NetworkX answers it.
  • root on 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 with root that 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).
  • 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. should_run returns True there, and NetworkX does not ask it.
  • From 75 should_run base #174: Dispatcher.should_run, the nxapi._utils.should_run decorator and has_reusable_conversion. This change adds no copy of them.
  • Tests, in a new file of their own (test_bridge_set.py; 55 adds test_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 without root, the graph's own node objects, and has_bridges a 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, a union graph 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 with root searches: 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 where node_connected_component would convert the graph, and declined on a view whose conversion is stale; has_bridges served where bridges would 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 from root among them), the wrapper and the should_run rules (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.
  • NetworkX's tests of these names and of the algorithms that call them (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). 148 bridges calls (149 in dev) and 22 has_bridges calls 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.
  • The generated dispatch table 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. The kernel's function is bridge_ids, so algorithms.bridges stays the module.
  • With 55 (local_bridges). Both add the two bridges.py files, 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 running make all, resolves it: the table then reaches local_bridges as nxapi.local_bridges with 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.
  • Merges. Against the branches of the other open pull requests, 48 of 74 merge without a conflict. Of the rest, this change adds conflicts only where other branches add names next to these, in the generated _nx_graphblas files and the list in tests/_dispatched.py; in scripts/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.

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

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.50%. Comparing base (7933efc) to head (a2409cf).

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

@eriknw eriknw mentioned this pull request Dec 4, 2026
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