Skip to content

51 transitive closure - #150

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:51-transitive-closure
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:51-transitive-closure

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add transitive_closure, transitive_closure_dag and transitive_reduction. Their edges, the data
on them and the nodes are NetworkX's; each node's neighbors come in node order, which may
differ from NetworkX's.

  • A directed graph's closure is a breadth-first search from every node with an out-edge at
    once, in blocks of sources whose bitmap stays under 64 MB (one block up to 8192 nodes). The
    diagonal it reaches is the nodes on a cycle, the self-loops reflexive=False adds; True
    adds one at every node, and None none but the graph's own.
  • An undirected graph's closure joins each node to the rest of its component, read off the
    component labels. As in NetworkX, whose edge_bfs never walks back along an edge, it gains no
    self-loop unless reflexive=True.
  • transitive_closure_dag is that closure after the cycle check. transitive_reduction keeps
    each edge whose head no walk of two or more edges from its tail reaches. Only nodes with two
    out-edges or more are walked from: a lone out-edge is the only way to its head.
  • The results are NetworkX graphs, written a row at a time rather than through
    add_edges_from. As NetworkX's G.copy() does, a closure keeps the graph attributes and
    each edge's weight, its new edges carry no data, and an edge's data dict is the one both
    its adjacencies hold. The reduction is a plain DiGraph with the nodes and nothing else.
    Data a matrix cannot hold (node attributes, two edge attributes, weighted edges beside bare
    ones) and a subclass are left to NetworkX by the existing rule for names that rebuild their
    input. A weight comes back in the matrix's type, as in those names: where ints and floats
    are mixed, 1 comes back as 1.0, and a numpy scalar comes back as a Python number.
  • A reflexive that only equals a bool, such as 1, 0.0 or numpy's True, adds no edge, as in
    NetworkX; any other value raises NetworkX's NetworkXError, or TypeError if unhashable. A
    cycle raises NetworkXUnfeasible in transitive_closure_dag and NetworkXError in
    transitive_reduction, as NetworkX's do.
  • Left to NetworkX: a closure of more than 2**24 edges (an undirected edge counted both
    ways), found while walking and before the NetworkX graph is built. The call peaks at about
    215 bytes an edge (NetworkX's own loop at 180 to 205), so about 3.6 GB at the bound; and a
    topo_order that is not a list or tuple of every node once in a topological order, since
    NetworkX does not check it and its answer follows the order given.

Speed, Indicative (one shared machine at load 3.5 to 4.3; each call in a fresh process from a
NetworkX graph, conversion and building the result included; best of two interleaved rounds):
faster than NetworkX in every case measured, with conversion.

call graph edges out graphblas networkx
transitive_closure random digraph, 2000 nodes 3.5M 0.32 s 12.8 s
transitive_closure, reflexive=None same 3.5M 0.33 s 3.1 s
transitive_closure random graph, 1500 nodes 1.1M 0.10 s 7.4 s
transitive_closure path, 3000 nodes 4.5M 0.55 s 8.1 s
transitive_closure random tree, 300000 nodes 3.5M 2.12 s 9.0 s
transitive_closure_dag random DAG, 11000 nodes 6.3M 0.69 s 3.7 s
transitive_closure_dag path, 3000 nodes 4.5M 0.66 s 1.9 s
transitive_reduction random DAG, 50000 nodes 0.24M 0.65 s 9.5 s
transitive_reduction random tree, 300000 nodes 0.30M 0.57 s 1.8 s

Stacked on #121.

A directed graph's closure is a breadth-first search from every node
with an out-edge at once, in blocks of sources whose bitmap stays under
64 MB. Its diagonal holds the nodes on a cycle, the self-loops
reflexive=False adds; reflexive=True adds one at every node and None
adds none but the graph's own. An undirected graph's closure joins each
node to the rest of its component, read off the component labels, and
as in NetworkX it gains no self-loop there unless reflexive is True.
The reduction keeps each edge whose head no walk of two or more edges
from its tail reaches, walking only from nodes with two out-edges or
more.

The results are NetworkX graphs written a row at a time rather than
through add_edges_from. As NetworkX's copy does, a closure keeps the
graph attributes and each edge's weight, its new edges carry no data,
and an edge's data dict is the one both its adjacencies hold; the
reduction keeps the nodes and nothing else. A reflexive that only
equals a bool, such as 1, adds no edge, as in NetworkX. A cycle raises
NetworkXUnfeasible in transitive_closure_dag and NetworkXError in
transitive_reduction, as NetworkX's do.

A closure of more than 2**24 edges is left to NetworkX, to bound the
memory of the graph built. So is a topo_order that is not a list or
tuple of every node once in a topological order, since NetworkX does
not check it and its answer follows the order given.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.58420% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.15%. Comparing base (5b54ace) to head (661d895).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #150      +/-   ##
========================================================
+ Coverage                 92.88%   93.15%   +0.27%     
========================================================
  Files                       182      183       +1     
  Lines                     11227    11701     +474     
  Branches                   1274     1333      +59     
========================================================
+ Hits                      10428    10900     +472     
- Misses                      546      548       +2     
  Partials                    253      253              
🚀 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