Repository navigation
Speed up connected_components and weakly_connected_components - #7971
Conversation
|
There is some computational cost to networkx counting the number of edges. It is about a third the time of using Are there cases when a user would probably not want these shortcuts computed? For the case of 1000 nodes, I estimate it would take 465us instead of 350us (~30% longer) for a non-shortcut result. And 115us for the shortcut case. But those estimates are not using this code. If that's an issue we could add a kwarg to turn on (or off) the shortcut feature. Thoughts? |
|
Ah, I did not know that! I suspected len was expensive after seeing some comments in the code. What is the historical context for those fields to not be cached? |
|
In terms of caching, we thought about caching way back in the day -- and some factors led us not to choose it:
Another data structure could have avoided this issue -- if we had the dict-of-dict point to the relevant edge info within a list of edge info dicts. Then the number of (unweighted) edges would be fast: That's the history of it -- but the history leading to no caching doesn't mean we can't start caching now -- of course, we should take into account these factors in our decision. I did a little more testing, and it looks like for |
78d122c to
9314c2d
Compare
connected_components and weakly_connected_components
|
Thanks for the context! I believe adding caching would unblock many optimizations like this.
Yes, I can see why now. It's because DFS is exiting early while edge counting needs to visit all edges.
Not many besides a quick check with an adverse graph. I was so convinced len(G.edges()) was O(1) that I left that for when finishing up the PR. I removed those optimizations for now (hopefully we can add them in some future). I updated the PR and left only the optimization for the DFS but need to spend some time to get better measurements. |
connected_components and weakly_connected_componentsconnected_components and weakly_connected_components
|
Hey @dschult I got time to come back to this. I removed the number of edges optimizations because they were actually slower, leaving the PR with only the BFS stop-early optimization. I'm seeing very good results on graphs where one big component is explored last. For the rest of tested graphs, I see small mixed results. What do you think? |
dschult
left a comment
There was a problem hiding this comment.
This is a good straight-forward speedup of our finding components. I believe it is an oversight of the original code. The _plain_bfs takes the argument n so that it can check if the bfs has found every node. So when we switch to a new component we can update that number to have it hopefully work for us on the last component.
Thanks!
Speed up
connected_componentsandweakly_connected_componentsThis PR introduces lightweight optimizations to several connectivity-checking functions by adding early returns based on simple graph invariants. These heuristics help short-circuit computation in cases where connectivity can be ruled out without traversing the graph.
Summary of changes:
connected_componentsandweakly_connected_componentsto reduce traversal overhead when many nodes have already been seen.These changes preserve correctness while reducing unnecessary work, especially on dense graphs.
Performance improvements results
The optimization shows the most significant gains on disconnected graphs with large components, achieving up to a 367× speed-up for n=1000. This case is artificially generated to make current version need to visit the same nodes in a connected component multiple times. These are the types of cases where optimization works best.
For connected and random graphs, performance changes are generally smaller, often within a ±10% range. In a few low-n random graph cases, the optimized version was marginally slower, likely due to overhead not offset by structural gains.
Overall, the optimization is highly effective in worst-case disconnected scenarios while maintaining neutral or slightly improved performance elsewhere.
Single Node + Complete Component (Disconnected, 100 runs)
Random Graphs (p=0.01, 10000 runs each)
Connected Graphs (Complete Graphs, 10000 runs)
Benchmark code