Skip to content

Add vf2pp subgraph isomorphism and monomorphism - #8506

Merged
dschult merged 14 commits into
networkx:mainfrom
dschult:vf2pp_subgraph_combine_helpers
Feb 28, 2026
Merged

dschult merged 14 commits into
networkx:mainfrom
dschult:vf2pp_subgraph_combine_helpers

Conversation

@dschult

@dschult dschult commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

Update vf2pp code to support subgraph isomorphism and subgraph monomorphism.

Adding subgraph isomorphism required checking <= instead of == in a number of places. I pass around an operator called subgraph_fits which is either eq or le depending on the problem type. The name is because we are testing whether the subgraph fits into the full graph. I was pleasantly surprised to find that very few places needed more complicated logic. And there I just checked if subgraph_fits == operator.eq.

That strategy worked well for monomorphism too. I defined a mono_fits operator (again eq or le). All of the subgraph_fits checks have to be the same as for "subgraph iso", and the additional checks that need to change from == to <= call mono_fits. That way the subgraph_fits is used to see if the node counts fit. For ISO, we need all counts eq. For IND (induced subgraph iso) we use le for node counts but edge counts on nodes must still be eq. For MONO we need the le for node counts and also for edge counts. So the two operators are enough to handle the three problem types.

I added a couple of tests, but mostly the tests are test_common.py which covers all 3 problem types by all three algorithms. The vf2pp_helpers tests were a bear though... So I spent the time to clean it up. Reduction of duplications across Graph/DiGraph reduced thousands of lines from the test_vf2pp_helpers.py file. All tests are still there -- most of the Graph and DiGraph class tests were the same except for tweaks to make it DiGraph instead of Graph.

The main features still coming are:

  • monomorphism for ISMAGS
  • edge labels for vf2pp
  • decide whether all algs will support "symmetry" or not. Symmetry is the ISMAGS core feature where we only find/report one isomorphism when others exist by just switching the nodes around in that same mapping (applying a symmetry). For example, a cycle graph has an isomorphism for each rotation of the nodes around a cycle. So only one mapping is needed to describe all those mappings when symmetry is used. It does slow down the computation though.
  • better tests for subgraph iso and mono cases
  • revamp documentation
  • I'm also aiming to include some new algorithms once the overarching API is more settled.

Notes/Questions:

  • the VF2++ paper uses G1 & G2 as the subgraph and graph respectively. But the VF2 paper uses G1 & G2 in the opposite order: full graph & subgraph. This causes confusion!! So I have changed the names for the graphs in vf2pp code, which allows me to more easily switch their order in the function sign so it matches VF2 and ISMAGS. Note that this is backwards compatible because we only supported graph isomorphism before where the order doesn't matter. The new names are FG & SG for "full graph" and "smaller graph" (or subgraph if you prefer).
  • I changed the function names cut and consistent because one returns True to continue and the other returns False. And its not clear why there are 2 criteria just based on the names. I named them feasible_node_pair for the one that looks at the candidate node pair itself, and feasible_look_ahead for the one that looks at the neighbors and neighbors neighbors to see if any contradictions arise that would block all future node pairs. They both return True to continue exploring this node pair.
  • It was hard to figure out how to manage multiedges and selfloops because the paper only directly talks about simple graphs. Just like with DiGraph support, there are more than one generalizations of the algorithm that would work. The papers talk briefly about how this is possible, but for simplicity they weren't going to talk about it. Hopefully checking for multiedges and selfloops doesn't slow the simple graph case down much. We could add a shortcut e.g. if there are no selfloops in either graph (and check that at the beginning). But I don't do that here. We end up checking for selfloops for each node as it is selected for mapping. Probably good to have those values pre-computed and stored. Maybe its the same for multigraphs. There might be some optimization possible here.

Review: There is way too much for one PR here. Sorry about that. Especially when some changes (like G1, G2 -> FG, SG touching almost every line of some tests). I suggest looking at the commits rather than the full diff. The first commit has the changes to the algorithm. The rest are mostly clean-up and renaming, etc.

The isomorphism checking suite of tools has been upgraded. The VF2++ functions now provide subgraph isomorphism and subgraph monomorphism checking.

dschult and others added 2 commits February 9, 2026 02:25
Not sure why this is firing - perhaps a mypy regex bug?
Either way - the simple thing to do is disable it!
@rossbar

rossbar commented Feb 9, 2026

Copy link
Copy Markdown
Contributor

Excellent @dschult ! I will try to get at this this week!

I went ahead and tweaked the mypy configuration in 15d557c to make the red x's go away (that seems like a mypy issue anyways...).

So the two operators are enough to handle the three problem types.

This is great! The described abstraction seems like an excellent addition to me, especially since it makes more clear exactly which things are being compared and by which checks for sub/mono morphism! It's always nice when abstractions "just work" :)

The vf2pp_helpers tests were a bear though... So I spent the time to clean it up. Reduction of duplications across Graph/DiGraph reduced thousands of lines from the test_vf2pp_helpers.py file

This is great news - worthwhile cleanup even if the diffs are huge!

So I have changed the names for the graphs in vf2pp code, which allows me to more easily switch their order in the function sign so it matches VF2 and ISMAGS. Note that this is backwards compatible because we only supported graph isomorphism before where the order doesn't matter. The new names are FG & SG for "full graph" and "smaller graph" (or subgraph if you prefer).

Excellent!

I changed the function names cut and consistent because one returns True to continue and the other returns False. And its not clear why there are 2 criteria just based on the names. I named them feasible_node_pair for the one that looks at the candidate node pair itself, and feasible_look_ahead for the one that looks at the neighbors and neighbors neighbors to see if any contradictions arise that would block all future node pairs. They both return True to continue exploring this node pair.

This too seems like a good idea - it's much easier to reason about checks being passed than one check and one filter.

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

Okay - I had a chance to work my way through everything! I left a few minor comments but on the whole this PR represents a very nice improvement, both in terms of new functionality (vf2pp SG isomorphism/monomorphism) as well as clean-up/readability improvements for the isomorphism package as a whole! Thanks @dschult for undertaking this herculean effort!

@dschult
dschult merged commit fe601f2 into networkx:main Feb 28, 2026
38 checks passed
@dschult dschult added this to the 3.7 milestone Feb 28, 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.

2 participants