Skip to content

fix multigraph isomorphism tests and fix support in ISMAGS and in VF2pp - #8616

Merged
dschult merged 6 commits into
networkx:mainfrom
dschult:fix_ismags_multiedges
Apr 17, 2026
Merged

dschult merged 6 commits into
networkx:mainfrom
dschult:fix_ismags_multiedges

Conversation

@dschult

@dschult dschult commented Apr 14, 2026

Copy link
Copy Markdown
Member

The existing multigraph tests for isomorphism were flawed in that they never had any multiedges (see below).

This PR updates the tests to ensure the multiedges intended to be created actually are making it into the graph. It also adds a few tests just for multigraphs. And it adds checks that all morphisms are found for the suite of graph tests. That means we have to know what all the morphisms are! And I changed the names of many tests from *_subgraph_* to *_SG_* (cant help myself I guess)

The new tests revealed a couple of failures for ismags, which is expected because ismags only recently supported multigraphs and it wasn't being tested well. So the 2nd commit here adds code to account for multiedges in two critical places: when computing degree and when checking that the number of edges between two nodes matches for candidate node-pairs. That fixed the errors for ISMAGS. There was also one failure for vf2pp multigraphs. The 3rd commit holds the fix for vf2pp.

After this PR vf2pp and ismags agree with vf2 on all these tests. And the tests have been expanded slightly, with new tests and also with known ground truth for number of morphisms between specific graphs.

details
The test trouble with multiedges was due to my adding copies of existing edges using 3-tuples instead of 2-tuples. Usually we want to use 3-tuples for multigraphs. But if we need to add more edges, using a 3-tuple just adds the same edge multiple times, rather than creating new edges. The fixed code is, e.g.:

G.add_edges_from([e for i, e in enumerate(G.edges()) if i % 2 == 0])

And the silently broken version used G.edges instead of G.edges(). Usually you want that, but not to create new edges. In one specific graph I switched from this shot-gun approach to adding multiedges to adding a targeted multiedge. I should do that for more of the test graphs. Adding arbitrary multiedges quickly reduces symmetries and isomorphism counts. With targeted edges we can keep multiple isomorphisms even for MultiDiGraph cases.

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

Generally LGTM, another great improvement to the isomorphism package!

The test failure on the Wikipedia graph looks real, so that should be addressed (I haven't dived into it [yet]).

Everything else looks good though, so I will approve conditioned on fixing the broken wiki test!

@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 had a chance to look at this again (including the latest 2 commits) - my approval stands!

@dschult

dschult commented Apr 16, 2026

Copy link
Copy Markdown
Member Author

I have added more tests that compare MultiGraph vs Graph and MultiDiGraph vs DiGraph.
With directed we just raise the input graphs don't match. How can you compare them?
But with MultiGraph you might have a simple graph stored as a multigraph. And we can find
isomorphic subgraphs and monomorphic subgraphs. But we haven't done much testing there.

Anyway, its tested now. (thanks to the CI failure from test_ismags.py upon the first push in this PR)

@dschult
dschult merged commit 32b2c75 into networkx:main Apr 17, 2026
44 checks passed
@dschult dschult added this to the 3.7 milestone Apr 17, 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