Skip to content

Improve chordal code readability by removing helper function - #8479

Merged
MridulS merged 2 commits into
networkx:mainfrom
rossbar:chordal_rm_max_cardinality_helper
Jan 22, 2026
Merged

MridulS merged 2 commits into
networkx:mainfrom
rossbar:chordal_rm_max_cardinality_helper

Conversation

@rossbar

@rossbar rossbar commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

Inspired by changes proposed in #8408 .

This PR replaces the _max_cardinality_node internal helper function with a more Pythonic one-line implementation that makes use of the key= feature of max along with set operations with dict keys.

The two commits here are intentional: the first (32d1c27) essentially shows the change from the original implementation (iterating over all nodes in a set) to the new implementation which uses max(..., key=). The second commit (0ada50d) then simply removes the helper function altogether and "interns" the one-line implementation in the two places where the helper funtion was originally called (along with a comment).

The main motivation here is improved readability, but I did run the benchmarks to ensure that this change wouldn't cause a performance regression. Indeed the new implementation is more performant too:

Results of asv continuous --bench ChordalBenchmarks main chordal_rm_max_cardinality_helper
| Change   | Before [64d154e8]    | After [0ada50d3]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------------|--------------------------------------------------------|---------|----------------------------------------------------------------------|
| -        | 76.9±0.1ms                 | 69.6±0.1ms                                             |    0.9  | benchmark_chordal.ChordalBenchmarks.time_is_chordal_complete(100)    |
| -        | 1.11±0.01ms                | 965±7μs                                                |    0.87 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_wheel(50)        |
| -        | 3.45±0.04ms                | 2.88±0ms                                               |    0.83 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_wheel(100)       |
| -        | 634±0.7μs                  | 517±3μs                                                |    0.82 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_cycle_graph(50)  |
| -        | 608±1μs                    | 500±9μs                                                |    0.82 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(50)         |
| -        | 624±2μs                    | 503±1μs                                                |    0.81 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_path(50)         |
| -        | 1.98±0.01ms                | 1.50±0.01ms                                            |    0.76 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_cycle_graph(100) |
| -        | 1.92±0ms                   | 1.45±0.01ms                                            |    0.76 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_star(100)        |
| -        | 1.97±0ms                   | 1.49±0ms                                               |    0.75 | benchmark_chordal.ChordalBenchmarks.time_is_chordal_path(100)        |

@MridulS
MridulS merged commit 7934c19 into networkx:main Jan 22, 2026
47 checks passed
@dschult dschult added this to the 3.7 milestone Jan 22, 2026
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