Skip to content

Fix min_weight_matching - #8062

Merged
dschult merged 6 commits into
networkx:mainfrom
fei0319:fix-min-weight-matching
Jun 1, 2025
Merged

dschult merged 6 commits into
networkx:mainfrom
fei0319:fix-min-weight-matching

Conversation

@fei0319

@fei0319 fei0319 commented May 21, 2025

Copy link
Copy Markdown
Contributor

The current min_weight_matching claims to compute a minimum-weight maximal matching, but it is actually computing a minimum-weighted maximum-cardinality matching.

The minimum-weight maximal matching, which it claims to solve, is NP-hard. See the paper:

As we know since 1961, all natural variants of perfect matchings and maximum matchings can be found in polynomial time, even in general graphs. Here we study a different problem — Minimum Maximal Matching (MMM). The task is — given graph G, to find an inclusion-wise maximal matching M with the smallest cardinality (or weight in the weighted version).

The MMM problem was studied as early as 1980, when Yannakakis and Gavril showed, that it is NP-hard even in some restricted cases.

Changes include:

  • Corrected the docstring description to fit the function behavior
  • Removed an incorrect test, which mistakenly tried to set maxcardinality (the min version does not have such a keyword parameter)
  • Added a test to show that we are computing a minimum-weighted maximum-cardinality matching instead of a minimum-weight maximal matching

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

Having read through the Wikipedia articles on maximal matchings and maximum cardinality matching, I agree that the definition should be updated.

A couple minor comments related to the test changes but the doc updates LGTM!

fei0319 and others added 2 commits May 24, 2025 23:24
@fei0319

fei0319 commented May 29, 2025

Copy link
Copy Markdown
Contributor Author

main has been merged into this. The added test_min_weight_matching_max_cardinality has been aligned with #8068 style.

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

LGTM, thanks @fei0319 !

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

Just some spelling nits.

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

@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 to me!

@dschult
dschult merged commit 3284e6a into networkx:main Jun 1, 2025
amcandio pushed a commit to amcandio/networkx that referenced this pull request Jul 20, 2025
* Fix `min_weight_matching`

* Apply a suggestion

Co-authored-by: Ross Barnowski <[email protected]>

* Tests for weight=None

* 1 -> weight=None

* Apply suggestions from code review

Co-authored-by: Gilles Peiffer <[email protected]>

---------

Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>
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