Repository navigation
ENH: Speed up connectivity checks in random_reference and lattice_reference - #8893
Merged
amcandio merged 1 commit intoSep 2, 2026
Conversation
jtorrents
approved these changes
Sep 2, 2026
jtorrents
left a comment
Member
There was a problem hiding this comment.
Thanks for this! The change is correct and I think should go in: local_edge_connectivity(G, a, b) == 0 and not nx.has_path(G, a, b) are testing exactly the same thing. Worth noting that connected_double_edge_swap already uses nx.has_path for exactly this post-swap check.
I'd like to point out that the original code called local_edge_connectivity without auxiliary and residual params, so every call (inside a tight loop no less) rebuilt the auxiliary digraph and the residual network from scratch. That's two full graph constructions per swap attempt. The flow computation itself is a small part of the cost. But even that is not needed, a simple BFS does the trick.
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.
The motivation for this change is to speed up
nx.random_referenceandnx.lattice_reference.Currently, connectivity after an edge swap is checked using
nx.local_edge_connectivity(G, a, b)==0, where(a, b)is one of the edges selected for the swap. However, only reachability between nodesaandbneeds to be checked here; thereforenx.has_pathis sufficient for this purpose.In a simple local benchmark (benchmark.py), replacing
nx.local_edge_connectivitywithnx.has_pathmadenx.random_referenceabout 548× faster andnx.lattice_referenceabout 45× faster on average.I also confirmed that the current implementation and this modification generate identical graphs for five different random seeds by comparing hashes of their edge lists.
The code after this change passes the tests in
test_smallworld.py.