Skip to content

55 local bridges - #158

Open
eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:55-local-bridges
Open

eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:55-local-bridges

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add local_bridges. A local bridge is an edge whose two ends share no neighbor. Its span is the length of the shortest path between its ends once the edge is taken away, or inf where there is none.

  • Which edges: those of the strict upper triangle where a masked A @ A has no entry. A keeps its self-loops, so no edge at a node with a self-loop is a local bridge, and no self-loop is one. NetworkX intersects the two ends' neighbor sets, and a self-loop puts a node in its own.

  • What comes out: (u, v, span) when with_span is True, and (u, v) otherwise. As in NetworkX, with_span=1 or np.True_ gives pairs.

  • Order: the first end of each pair is the one that comes first in the graph's node order, as G.edges gives it. Pairs come by their first end and then by their second, both in node order. NetworkX goes by each node's adjacency order instead, so the order can differ. On 3600 random graphs it differed in 2430, while the pairs and spans were the same in all of them.

  • It returns a generator, as NetworkX does. A call that is declined is declined at the call, where NetworkX's dispatcher can still hand it to NetworkX. The graph is read at the call, and the spans are computed for a batch of edges at a time as the generator is read. NetworkX reads the graph while its generator runs, so a change made to the graph in the meantime shows in NetworkX's answer, or raises RuntimeError there, but not here.

  • Spans without a weight: a breadth-first search per edge, one row per edge in a batch. Each row starts from the first end's other neighbors, with the first end already taken as reached, so the edge itself is never walked. A row stops when it reaches the second end.

  • Spans with a weight: Bellman-Ford rows, started the same way. A row walks on only from lengths that changed and are still below its second end's length so far. NetworkX's Dijkstra and this both give the least sum over paths, added from the first end. Float addition is monotone, so the float spans are the same floats, not only close.

  • Memory: a batch holds as many rows as 1 GiB allows, at the bytes a row may take per node of the graph at a batch's peak: 32 for breadth-first rows and 96 for Bellman-Ford rows. These are twice or more what was measured at 20000 nodes, on a random tree, where a row searches a whole side, and on random bipartite graphs, whose frontiers are wide: up to 8.5 and 45.3 bytes, with rows reaching about half the nodes. The rows filled so far are held as a bitmap once more than 1% full. Held sparse, they were copied at each update: on the 20000-node random tree the spans peaked at 1.7 GiB without weights and 3.3 GiB with int weights. They now peak at 251 MiB, and at 294 MiB with weights.

  • Weights served: Python ints or floats, which convert to an int64 or float64 matrix, that are at least 0 and finite. Ints are served while the number of nodes times the largest weight stays below 2**62. A missing weight is 1, as in NetworkX, and when every weight is the int 1 the spans are hop counts.

  • Weights left to NetworkX:

    • negative weights, where NetworkX's Dijkstra can raise "Contradictory paths found";
    • NaN and infinities;
    • bools and narrow NumPy types;
    • weight functions, which the backend declines for every name;
    • values a matrix cannot hold, such as strings: the conversion is declined, at the call, with a self-loop too, where NetworkX's conversion raises TypeError (declined since 61). NetworkX then answers without spans, as it reads no weight, and with spans raises as its generator is read.
  • Ints mixed with floats: the matrix holds floats, so a span over ints only is 3.0 here and 3 in NetworkX. The values are equal. In 137 of 600 random graphs with such a mix, at least one span differed in type this way.

  • Errors: directed graphs and multigraphs raise NetworkXNotImplemented in NetworkX before dispatch, and a directed backend graph raises the same here.

  • Speed (indicative; median of five interleaved rounds on a shared machine at a one-minute load of 15 to 19, conversion included):

    graph NetworkX here
    G(n, m), 2000 nodes, 6000 edges 6.7 s 86 ms
    Watts-Strogatz, 5000 nodes, k=6 5.3 s 85 ms
    Barabasi-Albert, 4000 nodes, m=2 21.9 s 246 ms
    random tree, 3000 nodes 2.4 s 142 ms
    random tree, 6000 nodes 11.9 s 592 ms
    300 x 300 grid 3.6 s 366 ms
    G(n, m), 2000 nodes, 6000 edges, float weights 9.2 s 889 ms
    300 x 300 grid, float weights 4.2 s 2.88 s
    G(n, m), 1e6 edges, with_span=False 2.17 s 1.51 s

    In a tree every span is inf, and each row searches the whole of its first end's side. On the weighted grid the spans are short, and the time goes to the number of batches: at 90000 nodes the memory bound holds a batch to 124 rows, about 1450 batches. With with_span=False the conversion is most of the time: 0.55 s with it cached. This is faster at every size measured, so there is no size gate.

Stacked on #156.

A local bridge is an edge whose two ends share no neighbor. They are the
edges of the strict upper triangle where a masked A @ A has no entry;
A keeps its self-loops, so a node with one is its own neighbor, as in
NetworkX. Each pair comes with its end first in node order first, as
G.edges gives it, and the pairs come in node order of both ends.

A span is the shortest length between the two ends without their edge.
Spans are computed for a batch of edges at a time as the generator is
read, a row per edge, each row started from the first end's other
neighbors with that end taken as reached. Without a weight this is a
breadth-first search that stops at the other end. With a weight it is
Bellman-Ford over lengths that changed and are still below the other
end's length so far. Weights are served when they are Python ints or
floats, at least 0 and finite, and ints while no length can reach
2**63; other weights, and weight functions, are left to NetworkX.

A batch holds as many rows as its memory allows, at the bytes a row
takes per node at a batch's peak, measured for each kind of row. The
rows filled so far are held as a bitmap once more than 1% full: held
sparse, they were copied at each update, and a random tree of 20000
nodes, where each row searches a whole side, peaked at 1.7 GiB without
weights and 3.3 GiB with them. It now peaks at 0.2 and 0.3 GiB.

Values a matrix cannot hold, such as strings, are declined at the
conversion, so NetworkX answers without spans, as it reads no weight
then, and with spans raises as its generator is read.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.40828% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.12%. Comparing base (83d8c57) to head (663d3c0).

Additional details and impacted files
@@                   Coverage Diff                   @@
##           61-generic-declines     #158      +/-   ##
=======================================================
+ Coverage                92.90%   93.12%   +0.21%     
=======================================================
  Files                      182      185       +3     
  Lines                    11294    11632     +338     
  Branches                  1281     1313      +32     
=======================================================
+ Hits                     10493    10832     +339     
  Misses                     547      547              
+ Partials                   254      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