Skip to content

BUG: Fix vf2 handling of isomorphism_iter with isolated subgraph nodes - #8895

Merged
rossbar merged 4 commits into
networkx:mainfrom
dschult:fix_vf2_for_isolated_nodes
Sep 9, 2026
Merged

rossbar merged 4 commits into
networkx:mainfrom
dschult:fix_vf2_for_isolated_nodes

Conversation

@dschult

@dschult dschult commented Sep 3, 2026

Copy link
Copy Markdown
Member

Fixes #8891

This is a corner case when subgraph has an isolated node and graph is subgraph isomorphic (and not isomorphic) to subgraph. The is_isomorphic method correctly returned False, but isomorphisms_iter generated mappings.

Added two tests (one for vf2 and one in test_common.py for all algorithms.

@dschult dschult added this to the 3.7 milestone Sep 3, 2026

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

Left some inline comments but this generally looks good to me!

d1 = sorted(d for n, d in self.G1.degree)
d2 = sorted(d for n, d in self.G2.degree)
if d1 != d2:
return

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.

Non-blocking state-consistency question: this now returns before initialize(), leaving an earlier mapping in place. For example:

gm = nx.isomorphism.GraphMatcher(nx.cycle_graph(3), nx.path_graph(3))
gm.subgraph_is_monomorphic()  # True
list(gm.isomorphisms_iter())  # []
gm.mapping

The last line was {} before this change and now retains the monomorphism. The boolean method already has similar early returns, so this is a question about intended reuse behavior. Should the iterator still reset its state, or should retaining the previous mapping be documented and tested?

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.

This state issue has been a problem with this suite of functions for a while. I shifted the self.initialize()
line to before the checks -- it is not expensive and is needed to reset self.mapping.
But users can still mess up state if they e.g. are iterating over mappings and between iterations they call a method of the same GraphMatcher. We can say "don't do that" -- python dicts say "dont do that" when changing a dict while iterating across it. But I think the better fix is to refactor all the algorithms to have a uniform function interface where each call constructs an internal private class that does the work and is protected from other activity that might mess it up. That approach/discussion is coming once all the algorithms are full-featured. Hopefully this Fall/Winter.

So, for now I'm just moving this one initialize() call, checking that the other public methods do the same early initialize, and relying on folks to not start two concurrent morphism calls with the same GraphMatcher.

Thanks for this!

@dschult dschult left a comment

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.

Thanks @Peiffap These are helpful comments.

d1 = sorted(d for n, d in self.G1.degree)
d2 = sorted(d for n, d in self.G2.degree)
if d1 != d2:
return

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.

This state issue has been a problem with this suite of functions for a while. I shifted the self.initialize()
line to before the checks -- it is not expensive and is needed to reset self.mapping.
But users can still mess up state if they e.g. are iterating over mappings and between iterations they call a method of the same GraphMatcher. We can say "don't do that" -- python dicts say "dont do that" when changing a dict while iterating across it. But I think the better fix is to refactor all the algorithms to have a uniform function interface where each call constructs an internal private class that does the work and is protected from other activity that might mess it up. That approach/discussion is coming once all the algorithms are full-featured. Hopefully this Fall/Winter.

So, for now I'm just moving this one initialize() call, checking that the other public methods do the same early initialize, and relying on folks to not start two concurrent morphism calls with the same GraphMatcher.

Thanks for this!

@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 @dschult and @Peiffap !

@rossbar
rossbar merged commit 56c7b0d into networkx:main Sep 9, 2026
43 checks passed
@dschult
dschult deleted the fix_vf2_for_isolated_nodes branch September 9, 2026 20:58
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.

GraphMatcher.isomorphsism_iter() can yield isomorphisms for non-isomorphic graphs

3 participants