Skip to content

Add ISMAGS monomorphism support - #8838

Merged
rossbar merged 8 commits into
networkx:mainfrom
dschult:ismags_mono
Aug 18, 2026
Merged

rossbar merged 8 commits into
networkx:mainfrom
dschult:ismags_mono

Conversation

@dschult

@dschult dschult commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

This PR adds support for monomorphism checking with ISMAGS.
That means we can iterate over monomorphisms and get only one
morphisms for each group of morphisms that are symmetrically the same.
The symmetries are found in the subgraph and used to restrict reported
monomorphisms.

The monomorphism code is intertwined with the isomorphisms using if
clauses and operator function SG_fits and mono_fits functions.
That reduces a lot of duplicate code -- but might affect performance.
I did run a profiler and found that creating gn2_cands was poorly behaved
so rewrote those loops. I'm sure more can be done for optimization.
But this at least gets the functionality in place.

The isomorphism checking suite of tools has been upgraded. The ISMAGS symmetry
aware isomorphism and subgraph isomorphism checking tools now support directed
and multigraph Graphs. And, now in addition to support for (induced) subgraph
isomorphisms, they support (non-induced subgraph) monomorphism.

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

A very nice new feature, thanks @dschult !

Generally LGTM! One high-level interface question - do we intend all_morphisms to be a method that users actually call, or is it more for internal orchestration (i.e. mapping "finding morphisms" to the correct problem type). If it's the latter, I'd vote to make it explicitly private so that users can focus on the direct methods (subgraph_isomorphisms_iter, monomorphisms_iter, etc.).

Not really a blocker, just an attempt to keep the number of ways to access functionality on the ISMAGS class more manageable!

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

Thanks again @dschult , this LGTM!

@rossbar

rossbar commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

I think this is ready to go, so I will go ahead and merge. @pckroon , @Peiffap if you want to/have time to take a look your feedback would be very appreciated!

@rossbar
rossbar merged commit ef98155 into networkx:main Aug 18, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Aug 18, 2026

@pckroon pckroon 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'm very excited to watch the nx ismags development take off from the sidelines, but I really need to admit it's quickly going over my head :)
Time to read up on wikipedia what a monomorphism is...

@dschult

dschult commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

Monomorphism means a not-necessarily-induced-subgraph of graph is isomorphic to the input subgraph. With subgraph isomorphism, an induced subgraph of graph is isomorphic to subgraph. With monomorphism, all the edges in subgraph have to map from an edge in graph, but edges in graph need not map to edges in subgraph even if the incident nodes are both mapped.

The single-edge graph is not isomorphic to the 2-node graph with no edges. But it is monomorphic.

@pckroon

pckroon commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Thanks, learned something new today :) Your explanation is much easier to understand than Wikipedia's.
This week is pretty busy, but I can whip up some thorough tests next week.

@pckroon

pckroon commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I did find a few inconsistencies. I'll put them here, but please let me know if you'd prefer them in an issue. Doesn't seem like deal breaking problems though, it seems to be a discrepancy in the symmetry handling between isormorphism and monomorphism.
The test is (roughly) as follows:

  • draw an arbitrary graph
  • draw arbitrary nodes from that graph, induce subgraph containing those nodes
  • find all induced subgraph isomorphisms between graph and subgraph with symmetry=False
  • draw edges to add to graph, add them
  • find all monomorphisms between subgraph and the augmented graph with symmetry=False (note: symmetry of the augmented graph is different from the symmetry of graph!)
  • Assert that found_isomorphisms <= found_monomorphisms

With isomorphisms_iter(symmetry=False) and monomorphisms_iter(symmetry=False) a failing case is found:

Graph: nodes: [0, 1, 2, 3]; edges: [(0, 1), (0, 2), (0, 3)]
Draw 2 (subgraph nodes): {0, 1, 2, 3}
Draw 3 (extra edges): {(1, 2)}

FAILED derp/test_ismags_monomorphism.py::test_isomorphism_match - assert {frozenset({(1, 1), (3, 3), (2, 2), (0, 0)}), frozenset({(2, 3), (1, 1), (3, 2), (0, 0)}), frozenset({(3, 1), (1, 2), (2, 3), (0, 0)}), frozenset({(3, 1), (1, 3), (2, 2), (0, 0)}), frozenset({(3, 3), (1, 2), (2, 1), (0, 0)}), frozenset({(3, 2), (1, 3), (2, 1), (0, 0)})} <= {frozenset({(1, 1), (3, 3), (2, 2), (0, 0)}), frozenset({(3, 3), (1, 2), (2, 1), (0, 0)})}
  
  Extra items in the left set:
  frozenset({(0, 0), (1, 2), (2, 3), (3, 1)})
  frozenset({(0, 0), (1, 3), (2, 2), (3, 1)})
  frozenset({(0, 0), (1, 1), (2, 3), (3, 2)})
  frozenset({(0, 0), (1, 3), (2, 1), (3, 2)})

However, with isomorphisms_iter(symmetry=True) and monomorphisms_iter(symmetry=False) tests pass.

@dschult

dschult commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

Thanks for this work and for the nice example!
It looks like the subgraph is a star graph with 3 rays from a center.
The symmetries there are permutations of the 3 ray endpoints.

Adding an edge to make the full graph, means there aren't any
subgraph isomorphisms, but we still get monomorphisms (that can
ignore that added edge). So... It makes sense that the identity map
is a monomorphism. The edges in SG are covered by that map --
and the extra edge in FG doesn't restrict the monomorphism.

But I am confused by your result that isomorphisms_iter gives any maps at all.
I don't think isomorphisms can work because the edge (1, 2) is induced but doesn't appear in the subgraph.
I tried to reproduce your result and got no isomoprhisms and no subgraph isomorphisms.
Can you show your code, or pick out what I'm doing differently?

G = nx.empty_graph([(0, 1), (0, 2), (0, 3)])
FG = G.copy()
FG.add_edge(1, 2)

mono_maps = list(nx.isomorphism.ISMAGS(FG, G).monomorphisms_iter(symmetry=True))
mono_maps_all = list(nx.isomorphism.ISMAGS(FG, G).monomorphisms_iter(symmetry=False))

iso_maps = list(nx.isomorphism.ISMAGS(FG, G).subgraph_isomorphisms_iter(symmetry=True))
iso_maps_all = list(nx.isomorphism.ISMAGS(FG, G).subgraph_isomorphisms_iter(symmetry=False))

print(f"{iso_maps=}\n{mono_maps=}")
print(f"\nNo Symmetry Restriction on Maps")
print(f"{iso_maps_all=}\n{mono_maps_all=}")

with output:

iso_maps=[]
mono_maps=[{0: 0, 1: 1, 2: 2, 3: 3}]

No Symmetry Restriction on Maps
iso_maps_all=[]
mono_maps_all=[{0: 0, 1: 1, 2: 2, 3: 3}, {0: 0, 1: 1, 3: 2, 2: 3}, {0: 0, 2: 1, 1: 2, 3: 3}, {0: 0, 2: 1, 3: 2, 1: 3}, {0: 0, 3: 1, 1: 2, 2: 3}, {0: 0, 3: 1, 2: 2, 1: 3}]

@pckroon

pckroon commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Not quite. What I do is test the hypothesis that all isomorphisms S <= G are also monomorphism S <= H where H is G with added edges.
In code:

G = nx.empty_graph([(0, 1), (0, 2), (0, 3)])
S = nx.subgraph({0, 1, 2, 3})  
H = G.copy()
H.add_edge(1, 2)

isomorphisms = list(nx.isomorphism.ISMAGS(G, S).isomorphisms_iter(symmetry=False))
monomorphisms = list(nx.isomorphism.ISMAGS(H, S).monomorphisms_iter(symmetry=False))
# Turn the 2 lists-of-dicts into 2 sets-of-frozensets-of-tuples
assert isomorphisms <= monomorphisms

Since S == G (in this example) there should be 6 symmetry equivalent isomorphisms (3 rotation x 2 mirror):
Isomorphisms: [{1: 1, 3: 3, 2: 2, 0: 0}, {2: 3, 1: 1, 3: 2, 0: 0}, {3: 1, 1: 2, 2: 3, 0: 0}, {3: 1, 1: 3, 2: 2, 0: 0}, {3: 3, 1: 2, 2: 1, 0: 0}, {3: 2, 1: 3, 2: 1, 0: 0}]
These should also be found as monomorphisms , but only the 2 mirrors are found:
Monomorphisms: [{1: 1, 3: 3, 2: 2, 0: 0}, {3: 3, 1: 2, 2: 1, 0: 0}]

@dschult

dschult commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

I got this working -- after tweaking the code to 1) use Graph instead of empty_graph (which made the nodes into 2-tuples intended to be edges?) and 2) use G.subgraph instead of nx.subgraph. I also added code to make the set of frozensets of tuples directly instead of a list first.

import networkx as nx
G = nx.Graph([(0, 1), (0, 2), (0, 3)])
S = nx.subgraph(G, {0, 1, 2, 3})  
H = G.copy()
H.add_edge(1, 2)

isomorphisms = {frozenset(m.items()) for m in nx.isomorphism.ISMAGS(G, S).isomorphisms_iter(symmetry=False)}
monomorphisms = {frozenset(m.items()) for m in nx.isomorphism.ISMAGS(H, S).monomorphisms_iter(symmetry=False)}
print(f"{isomorphisms=}\n{monomorphisms=}")
assert isomorphisms <= monomorphisms

I get 6 mappings for both isomorphisms and monomorphisms (and they are equal).

@pckroon

pckroon commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Apologies for not checking my code more carefully, and thanks for taking a look.
I found the issue in my test. The hypothesis test originally didn't have a G.copy(), so the subgraph also got the added edge between (1, 2). Oops... Note to self: just copy the code

Anyway, for any simple graph I throw at it, isomorphisms <= monomorphisms now. Nice work!

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.

3 participants