Repository navigation
enh: short-circuit in is_regular for directed graphs - #8138
Conversation
amcandio
left a comment
There was a problem hiding this comment.
LGTM! This is an example where having a cache of the amount of edges of the graph would allow for O(1) short-circuiting
| (nx.MultiDiGraph([(0, 1), (0, 1)]), False), | ||
| ], | ||
| ) | ||
| def test_is_regular(self, graph, expected): |
There was a problem hiding this comment.
What about having two methods one for True and one for False?
There was a problem hiding this comment.
I slightly prefer doing it this way (especially since it doesn't affect readability), but if people feel differently, I'm happy to split as well!
rossbar
left a comment
There was a problem hiding this comment.
I'm not convinced that the directed expression is generally more performant... maybe if you did all(d_in == d ...) and all(d_out == d ...) to keep the advantage of early termination on the degrees individually. I also don't think this improves readability; I'm -0.5.
|
I updated the implementation to hopefully get better readability and to make the performance gain more clear. If this still doesn't seem worth the churn, let me know and I'll revert the code changes completely and just leave doc and test changes. |
rossbar
left a comment
There was a problem hiding this comment.
I went ahead and added a benchmark in #8173 just to make sure that the implementation is short circuiting as expected and as of 6f8edc5 things look as expected:
asv continuous --bench IsRegularCompleteGraph main perf/is_regular
| Change | Before [65f0ab53] | After [6f8edc54] | Ratio | Benchmark (Parameter) | |----------|----------------------------|--------------------------------------|---------|-----------------------------------------------------------------------------| | | 1.84±0μs | 1.85±0μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(10, False, False) | | | 1.93±0μs | 1.94±0.01μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(10, False, True) | | - | 2.75±0.01μs | 2.13±0μs | 0.78 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(10, True, False) | | | 2.83±0.01μs | 2.82±0μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(10, True, True) | | | 8.82±0.03μs | 8.80±0.01μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(100, False, False) | | | 8.89±0.02μs | 8.88±0.01μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(100, False, True) | | - | 13.9±0.04μs | 7.69±0.01μs | 0.55 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(100, True, False) | | | 14.0±0.02μs | 13.9±0.02μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(100, True, True) | | | 2.63±0μs | 2.63±0.01μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(20, False, False) | | | 2.71±0μs | 2.71±0.01μs | 1 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(20, False, True) | | - | 4.01±0μs | 2.75±0μs | 0.69 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(20, True, False) | | | 4.10±0μs | 4.08±0.01μs | 0.99 | benchmark_regular.IsRegularCompleteGraph.time_is_regular(20, True, True) |
i.e. an improvement for directed graphs that are not regular (or at least, directed graphs who are not regular in in-degree :) )
This LGTM now, thanks @Peiffap !
|
Gee, what are we going to do with all of these extra microseconds... Thanks for the suggestion and for the benchmarks! |
This PR adds a small performance improvement, allowing
is_regularto avoid checking all in-/outdegrees when a directed graph has nodes with different out-/indegrees. I've also slightly tweaked the docstring and added/refactored a few tests to check everything works as it should.