Repository navigation
Conversation
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 Report❌ Patch coverage is 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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
moral_graphandinter_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.Tunder the complement ofA | A.T, its upper triangle without the diagonal (algorithms.co_parents). NetworkX's answer isG.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 ownto_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_runleaves 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.moral_graph is declined, so NetworkX answers, for:
fallback_to_nx, NetworkX answers for this backend's own graph);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.
Awith 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_graphreads it, now forinter_community_edgestoo. Three changes:inter_community_edgesread its keys, and raised where NetworkX answers.NetworkXException, orTypeErrorwhere the graph's nodes in the blocks still add up.inter_community_edgesanswered an iterator before.Tests for moral_graph. The tests use 330 random directed graphs, with:
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:
moral_graph(test_moral.py);test_quality.py);junction_tree, which callsmoral_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_graphserved 5 times in each. No NetworkX test callsinter_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.