Skip to content

enh: short-circuit in is_regular for directed graphs - #8138

Merged
dschult merged 7 commits into
networkx:mainfrom
Peiffap:perf/is_regular
Aug 4, 2025
Merged

dschult merged 7 commits into
networkx:mainfrom
Peiffap:perf/is_regular

Conversation

@Peiffap

@Peiffap Peiffap commented Jul 8, 2025

Copy link
Copy Markdown
Member

This PR adds a small performance improvement, allowing is_regular to 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.

Comment thread networkx/algorithms/regular.py Resolved
Comment thread networkx/algorithms/regular.py Resolved

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having two methods one for True and one for False?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Peiffap

Peiffap commented Aug 1, 2025

Copy link
Copy Markdown
Member Author

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 !

@Peiffap

Peiffap commented Aug 1, 2025

Copy link
Copy Markdown
Member Author

Gee, what are we going to do with all of these extra microseconds... Thanks for the suggestion and for the benchmarks!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Thanks!

@dschult
dschult merged commit 2573bd3 into networkx:main Aug 4, 2025
@Peiffap
Peiffap deleted the perf/is_regular branch August 4, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants