Skip to content

67 moral graph and inter-community non-edges - #165

Open
eriknw wants to merge 1 commit into
python-graphblas:50-spanning-treesfrom
eriknw:67-moral-graph
Open

eriknw wants to merge 1 commit into
python-graphblas:50-spanning-treesfrom
eriknw:67-moral-graph

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add moral_graph and inter_community_non_edges.

  • moral_graph. The pairs of nodes that share a child, and that the graph does not join either way, come from one masked product: A @ A.T under the complement of A | A.T, its upper triangle without the diagonal (algorithms.co_parents). NetworkX's answer is G.to_undirected() with those pairs added. That means deep copies of the graph's, nodes' and edges' data (a two-way edge's two dicts merged, the later one's values winning), a bare dict for each new edge, and every existing edge left as it is. A matrix holds none of that data. So, as for the spanning trees, the answer is built from the NetworkX graph that the conversion was made from (_BUILT_FROM_SOURCE), as NetworkX builds it: that graph's own to_undirected(), then each new pair joined with an empty dict of its own, the same dict in both directions. The class and node order are NetworkX's; within a node, the new neighbors may come in another order.

  • For moral_graph, building the graph is the cost. In a step-by-step profile the product is under 1% of a call (0.007 s of 1.8 s on a DAG of 3000 nodes, 0.032 s of 5.6 s on a random graph of 10^6 edges). The rest is to_undirected(), which costs what it costs in NetworkX, plus a dict for each new edge. So the saving is NetworkX's loop over every pair of parents, repeats included, set against one pass over the new pairs and the conversion.

  • should_run leaves a graph with fewer than 5 pairs of parents for each edge to NetworkX. The rule sums C(in-degree, 2) over the nodes, which is the number of pairs NetworkX's loop takes, a self-loop's node among its own parents. It compares that sum with 5 times the number of edges, in one pass over the in-degrees: 2 ms to 14 ms at 6e5 edges. On random graphs of 1e4, 1e5 and 6e5 edges, the backend with its conversion crosses NetworkX between 4 and 5 pairs per edge. In this sweep both families give each node a fixed or a Poisson number of random parents. Below 4 pairs per edge the backend runs 0.72x to 1.03x, and from 6 it runs 0.97x to 1.39x, mostly above 1.1x (Indicative, load 5 to 8). A call that names the backend is served whatever the ratio. Indicative timings follow: interleaved with NetworkX, best of 3, in one process, at load 5 to 8. "Cold" includes the conversion and "warm" runs on NetworkX's cached conversion, both naming the backend. "Automatic" has the backend first in NetworkX's priority list, where the rows marked "NetworkX" run NetworkX's own code.

    directed graph edges new edges pairs of parents per edge NetworkX cold warm automatic
    DAG, 3000 nodes, p = 0.02 89842 1333647 19.9 0.99 s 1.75x 1.81x served, 1.75x
    DAG, 6000 nodes, p = 0.01 180480 3105048 20.1 2.27 s 1.73x 1.79x served, 1.73x
    hubs as children, 2e4 nodes 99975 2055267 20.7 1.08 s 1.25x 1.30x served, 1.26x
    hubs as children, 5e4 nodes 249975 6061418 24.4 3.26 s 1.32x 1.37x served, 1.32x
    random, 2e5 nodes 1e6 2498078 2.5 3.58 s 0.92x 1.01x NetworkX
    the same, with weights and node data 1e6 2498781 2.5 4.05 s 1.05x 1.25x NetworkX
    random, 1e6 nodes 1e6 498369 0.5 3.68 s 0.94x 1.21x NetworkX
    a tree, edges away from its root, 5e5 nodes 499999 0 0 1.36 s 0.97x 1.23x NetworkX
  • moral_graph is declined, so NetworkX answers, for:

    • a multigraph;
    • a graph of this backend's own, or a conversion that was copied or unpickled, since no NetworkX graph made it (with fallback_to_nx, NetworkX answers for this backend's own graph);
    • a view whose graph has since lost a node that a new pair names (NetworkX keeps the view's cached conversion).

    A subclass is left to NetworkX, as for every graph-returning name. An undirected graph raises NetworkXNotImplemented, as in NetworkX.

  • inter_community_non_edges. NetworkX counts the inter-community edges of the complement graph, which it builds, so the cost and memory grow with n**2. Here the count is the pairs across blocks less the edges across.

    • The pairs across come from the block sizes: N**2 less the sum of the squares, halved for an undirected graph.
    • The edges across come from one product of A with the node-by-block matrix: each held node's neighbors in all blocks, less those in its own block.

    The count is exact, a Python int. A node that no block holds is not counted, since NetworkX restricts the graph to the union of the blocks. A self-loop never crosses. A directed graph counts ordered pairs, as NetworkX's directed complement does. The backend is 490x to 670x faster where NetworkX takes 4.6 s to 11 s (2000 and 3000 nodes), conversion included, and 4200x to 7300x faster on a cached conversion.

  • The partition is read as NetworkX's quotient_graph reads it, now for inter_community_edges too. Three changes:

    • A dict's blocks are its values. inter_community_edges read its keys, and raised where NetworkX answers.
    • The check follows NetworkX's steps: it reads each block twice and takes the lengths last. So a block that is a generator raises what NetworkX raises: NetworkXException, or TypeError where the graph's nodes in the blocks still add up.
    • Two kinds of partition are declined. A function is an equivalence relation, whose classes NetworkX finds. An iterator is one that NetworkX reads twice, so it raises for nearly any graph; inter_community_edges answered an iterator before.
  • Tests for moral_graph. The tests use 330 random directed graphs, with:

    • self-loops, isolated nodes and two-way edges;
    • DAGs, and graphs with many parents of a few children;
    • int, string, tuple and mixed keys;
    • graph, node and edge data, with lists among the values.

    Each graph goes through the backend cold, warm, on a conversion that another name cached, on the automatic path, and called directly. The graph, node and edge dicts must equal NetworkX's, and so must the class and the dicts' class. No dict or list may be shared with the input, and each edge must have one dict, shared by its two directions. The tests also cover every decline above, and a subclass that makes its own edge dicts (called directly, it adds the edges itself).

  • Tests for the counts. 330 random graphs and partitions: directed and undirected, partitions of some or all of the nodes, empty blocks, and list, tuple, frozenset and dict containers. The tests check both inter-community names exactly against NetworkX. They also run 17 partition shapes, the errors included, against NetworkX with the backend first in its priority list.

  • Tests for the rule. The rule is tested at its boundary: 10 and 11 parents of one child are 4.5 and 5 pairs per edge, and a self-loop moves a graph across the boundary. The tests also cover a view and the null graph, and check the automatic path: the graph is converted only where it is served. Elsewhere the tests serve every graph on the automatic path.

  • Mutants. 34 mutants of the kernels, the wrappers, the rule, the partition check and the conversion record each fail the tests, in NetworkX dev, 3.7 and 3.6.1.

  • NetworkX's tests. These run through the backend:

    • the tests of moral_graph (test_moral.py);
    • the tests of the community quality names (test_quality.py);
    • the tests of junction_tree, which calls moral_graph (test_decomposition.py).

    The same tests pass before and after: 49 in dev, 33 in 3.7 and 13 in 3.6.1, with moral_graph served 5 times in each. No NetworkX test calls inter_community_non_edges. The repo's tests pass in dev, in 3.7 on Python 3.14 and 3.13, and in 3.6.1.

Stacked on #151.

The pairs of nodes that share a child are one masked product, A @ A.T
without the pairs A already joins. NetworkX's answer is its graph's
to_undirected() with those pairs joined, so it keeps the graph's,
nodes' and edges' data, deep-copied, which a matrix does not hold. So
the answer is built, as the spanning trees are, from the NetworkX graph
the conversion was made from, as NetworkX builds it, the new edges
bare. Building the graph takes nearly all of the time; the product
takes under one percent. Where a graph has fewer than five pairs of
parents for each edge, NetworkX's loop over them costs less than the
conversion, so should_run leaves it to NetworkX.

inter_community_non_edges counts the pairs across blocks less the edges
across, from one product of A with the node-by-block matrix, where
NetworkX builds the complement graph. A partition is read as NetworkX's
quotient_graph reads it: a dict's blocks are its values, as they now
are for inter_community_edges too, which read the keys, and a function
or an iterator is left to NetworkX, which reads an iterator twice.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.53052% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.43%. Comparing base (0604cc2) to head (5f6b453).

Additional details and impacted files
@@                  Coverage Diff                  @@
##           50-spanning-trees     #165      +/-   ##
=====================================================
+ Coverage              93.20%   93.43%   +0.22%     
=====================================================
  Files                    185      188       +3     
  Lines                  11885    12301     +416     
  Branches                1336     1379      +43     
=====================================================
+ Hits                   11078    11494     +416     
  Misses                   551      551              
  Partials                 256      256              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This was referenced Dec 3, 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