Skip to content

fix optimize_edit_paths handling of self-loops - #8207

Merged
MridulS merged 5 commits into
networkx:mainfrom
albastardoto:bufix-for-issue-8188
Aug 31, 2025
Merged

MridulS merged 5 commits into
networkx:mainfrom
albastardoto:bufix-for-issue-8188

Conversation

@albastardoto

Copy link
Copy Markdown
Contributor

My first ever contribution to an open source project !

bugfix for : nx.graph_edit_distance returns wrong value when one graph contains only one node and loops and the other graph is empty #8188

I added the previously failing tests to the test suite.

The fix was to only attempt to match edges after one node match has been made but only if it's possible that a substitution even exists. This "only if it's possible that a substitution even exists" was what was missing before. To check if a substitution is possible, there has to be at least one pending edge in both g and h.

@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 generally approve this change with the caveat that I don't have a complete understanding of the graph_edit_distance code and am heavily relying on the test suite here to catch unintended behavior changes. This definitely needs at least one more set of eyes!

Anyways, thanks for taking this on @albastardoto !

Comment thread networkx/algorithms/similarity.py Resolved
Comment thread networkx/algorithms/similarity.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.

I think this is a reasonable version of the two loop suggestion @rossbar linked to.
The current second test does test two loops, but from the same node in a MultiDiGraph instead of a DiGraph.

Otherwise this looks good to me.

@dschult
dschult force-pushed the bufix-for-issue-8188 branch from 4c59377 to 4b19e00 Compare August 30, 2025 21:04

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

I added the test requested in @rossbar's review.
I think this is ready now.

@dschult dschult changed the title fixing match_edges edge_case to ensure it applies only when an edge s… fix optimize_edit_paths to delay self-loop removal only if substitutions are possible Aug 30, 2025
@dschult dschult changed the title fix optimize_edit_paths to delay self-loop removal only if substitutions are possible fix optimize_edit_paths handling of self-loops Aug 30, 2025
@MridulS MridulS added this to the 3.6 milestone Aug 31, 2025
@MridulS
MridulS merged commit 9c17836 into networkx:main Aug 31, 2025
alfred-mikhael pushed a commit to alfred-mikhael/networkx that referenced this pull request Sep 3, 2025
* fixing match_edges edge_case to ensure it applies only when an edge substitution is possible

* minor formatting change for tests

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

* implement suggestion to use precomputed values

* Update networkx/algorithms/tests/test_similarity.py

---------

Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Dan Schult <[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.

nx.graph_edit_distance returns wrong value when one graph contains only one node and loops and the other graph is empty

5 participants