Skip to content

refactor: simplify k_factor - #8139

Merged
dschult merged 10 commits into
networkx:mainfrom
Peiffap:refactor/k_factor
Jul 30, 2025
Merged

dschult merged 10 commits into
networkx:mainfrom
Peiffap:refactor/k_factor

Conversation

@Peiffap

@Peiffap Peiffap commented Jul 8, 2025 •

Copy link
Copy Markdown
Member

This PR simplifies the k_factor function by removing the largely redundant classes for gadgets. I've also cleaned up the docstring a little bit and made the test suite more comprehensive.

Since this function returns a graph, should it also be renamed to include the _graph suffix?

Comment thread networkx/algorithms/regular.py Resolved

@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.

As a general comment - when making PRs that change both the code and the related tests, I would vastly prefer if the test changes and code changes were at least split into separate PRs. One of the main purposes of the test suite is to guarantee that the existing behavior is preserved under the new changes in the code. When both are changed simultaneously, this is no longer possible... at least not without significant overhead of checking out old tests from other branches locally etc.

@Peiffap
Peiffap force-pushed the refactor/k_factor branch from 951a82a to 40249aa Compare July 9, 2025 08:31
@Peiffap

Peiffap commented Jul 9, 2025

Copy link
Copy Markdown
Member Author

@rossbar Your comment makes a lot of sense! I've rebased to get rid of the test-related changes, and will open a separate PR for those.

@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.

Thanks @Peiffap , getting rid of the KGadget classes is certainly a readability improvement IMO. So much so that I have trouble using the original implementation as a reference.

Generally I think things are consistent but this should definitely have another set of eyes, particularly if anyone is familiar with k-factors!

I left one additional comment near the end about designing a test to probe a specific set of lines, but that's not a blocker to this PR for me. Thanks @Peiffap !

Comment thread networkx/algorithms/regular.py Resolved
Comment thread networkx/algorithms/regular.py Resolved
Co-authored-by: Ross Barnowski <[email protected]>
@Peiffap

Peiffap commented Jul 15, 2025 •

Copy link
Copy Markdown
Member Author

I added some benchmarks too. We're roughly in the same ballpark as before (which shouldn't be surprising as the underlying algorithm hasn't changed).

All benchmarks:

| Change   | Before [4410959e] <main>       | After [5817a5c3] <jj/keep/5817a5c35cdddca3b5f3c31e6fb725ddf0305f80>   | Ratio   | Benchmark (Parameter)                                                                          |
|----------|--------------------------------|-----------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------|
|          | 121±4ms                        | 119±2ms                                                               | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(20)', 1)                  |
|          | 144±0.8ms                      | 144±1ms                                                               | 1.00    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(20)', 2)                  |
|          | 161±4ms                        | 157±1ms                                                               | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(20)', 3)                  |
|          | 147±4ms                        | 145±2ms                                                               | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(20)', 4)                  |
|          | 2.00±0.1ms                     | 1.96±0.1ms                                                            | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(6)', 1)                   |
|          | 2.88±0.3ms                     | 2.43±0.1ms                                                            | ~0.84   | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(6)', 2)                   |
|          | 1.12±0.2ms                     | 1.07±0.1ms                                                            | 0.95    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(6)', 3)                   |
| -        | 950±40μs                       | 814±10μs                                                              | 0.86    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.complete_graph(6)', 4)                   |
|          | 5.61±0.1ms                     | 5.53±0.07ms                                                           | 0.99    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(10, 0.7)', 1)          |
|          | 6.95±0.2ms                     | 6.79±0.1ms                                                            | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(10, 0.7)', 2)          |
|          | 5.61±0.02ms                    | 5.52±0.2ms                                                            | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(10, 0.7)', 3)          |
|          | 4.09±0.03ms                    | 3.84±0.03ms                                                           | 0.94    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(10, 0.7)', 4)          |
|          | 785±8ms                        | 776±10ms                                                              | 0.99    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(100, 0.1)', 1)         |
|          | 883±20ms                       | 867±10ms                                                              | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(100, 0.1)', 2)         |
|          | 1.02±0.02s                     | 1.02±0.01s                                                            | 1.00    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(100, 0.1)', 3)         |
|          | 918±10ms                       | 916±10ms                                                              | 1.00    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.erdos_renyi_graph(100, 0.1)', 4)         |
|          | 185±9ms                        | 181±2ms                                                               | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.grid_graph([10, 10], periodic=True)', 1) |
|          | 81.0±1ms                       | 81.1±0.9ms                                                            | 1.00    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.grid_graph([10, 10], periodic=True)', 2) |
|          | 57.0±1ms                       | 56.1±0.3ms                                                            | 0.98    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.grid_graph([10, 10], periodic=True)', 3) |
|          | 38.7±1ms                       | 37.2±0.09ms                                                           | 0.96    | benchmark_regular.RegularBenchmarks.time_k_factor('nx.grid_graph([10, 10], periodic=True)', 4) |

@Peiffap
Peiffap force-pushed the refactor/k_factor branch from 464ff01 to ad0189f Compare July 15, 2025 00:41
@Peiffap
Peiffap force-pushed the refactor/k_factor branch from ad0189f to 5817a5c Compare July 15, 2025 00:49
Comment thread networkx/algorithms/regular.py Outdated
matching_weight: string, optional (default="weight")
Edge data key corresponding to the edge weight.
Used for finding the max-weighted perfect matching.
If the key is not found, all edges are considered to have the same weight.

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 changed line is confusing to me. How can a missing edge key on one edge mean that all edges will have the edge weights ignored. I know this change is intended to be mostly about indentation. But sneaking in a few wording changes makes it hard to tell what was changed here. Can we leave the wording the same?

Or change the wording to our typical ("node" instead of "vertex", "edge attribute name" instead of "edge data key", "is not present" instead of "not found")

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've update the wording to match the rest of the codebase, and reverted the previous change (which was wrong). Thanks for catching this; It wasn't my intention to do anything sneaky here! :)

Comment thread networkx/algorithms/regular.py Resolved
Comment thread networkx/algorithms/regular.py Resolved
Co-authored-by: Dan Schult <[email protected]>

@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.

Thanks for this @Peiffap !!
It reads much better now with replacement of the class.
And the docs missing the k parameter is a good catch too!
:)

@dschult
dschult merged commit 6604f35 into networkx:main Jul 30, 2025
@Peiffap
Peiffap deleted the refactor/k_factor branch July 30, 2025 15:43
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.

3 participants