Skip to content

Fix ISMAGS ignoring matches when graph has extra node/edge colors (gh-8738) - #8800

Merged
rossbar merged 4 commits into
networkx:mainfrom
deepakganesh78:fix/issue8738-ismags-graph-only-colors
Aug 4, 2026
Merged

rossbar merged 4 commits into
networkx:mainfrom
deepakganesh78:fix/issue8738-ismags-graph-only-colors

Conversation

@deepakganesh78

Copy link
Copy Markdown
Contributor

Fixes #8738 (the edge_match half; also fixes the analogous node-color case).

Root cause

ISMAGS.find_isomorphisms short-circuits with:

elif len(self._sgn_partition) > self.N_node_colors:
    return
elif len(self._sge_partition) > self.N_edge_colors:
    return

The intent is "some subgraph color doesn't occur in the graph, so no match is possible." But create_aligned_partitions pads the subgraph partition with empty sets so that graph-only colors (present in the graph, absent from the subgraph) line up positionally against the graph partition. Those empty padding sets make len(self._sg*_partition) larger than N_*_colors even though every subgraph color is present in the graph — so a valid match gets rejected.

This is exactly why the reported example behaves inconsistently:

graph = nx.Graph([(0, 1)])
subgraph = nx.Graph(); subgraph.add_node(0)

nx.algorithms.isomorphism.ISMAGS(graph, subgraph, edge_match=lambda e1, e2: True).find_isomorphisms()  # was []
nx.algorithms.isomorphism.ISMAGS(graph, subgraph, edge_match=None).find_isomorphisms()                 # [{0: 0}, {1: 0}]

With edge_match=None the graph edge partition is a single lumped part and nothing gets padded; with a callable, the graph's real edge produces an edge color that pads the (empty) subgraph edge partition, tripping the length check. The same latent bug exists for node colors when the graph has a node color the subgraph lacks.

Fix

Only bail out when the subgraph has a genuinely non-empty part beyond the matched colors (a color that really only the subgraph has). Padding sets are empty, so they're correctly ignored:

elif any(self._sgn_partition[self.N_node_colors :]):
    return
elif any(self._sge_partition[self.N_edge_colors :]):
    return

Tests

Added two regression tests in TestSubgraphIsomorphism:

  • test_edgeless_subgraph_with_edge_match — edge_match=None and edge_match=lambda e1, e2: True now both yield [{0: 0}, {1: 0}].
  • test_subgraph_with_graph_only_node_color — a graph-only node color no longer blocks a match, while a genuine subgraph-only color still correctly yields no isomorphisms.
$ pytest networkx/algorithms/isomorphism/tests/test_ismags.py -q
93 passed

$ pytest networkx/algorithms/isomorphism/tests/ -q
1301 passed, 960 xfailed

Note: this PR intentionally scopes to the create_aligned_partitions padding bug. The categorical_node_match([...], [None]*0) default-length pitfall discussed in the issue thread is a separate concern and not touched here.

`find_isomorphisms` bailed out early whenever the *padded* subgraph
partition was longer than the number of matched colors:

    elif len(self._sgn_partition) > self.N_node_colors:
    elif len(self._sge_partition) > self.N_edge_colors:

However `create_aligned_partitions` pads the subgraph partition with
empty sets so that graph-only colors (colors present in the graph but
absent from the subgraph) line up positionally with the graph
partition. Those empty padding sets made `len(partition)` exceed
`N_*_colors`, so a perfectly valid match was wrongly rejected.

This is why an edgeless subgraph behaved differently with
`edge_match=None` versus `edge_match=lambda e1, e2: True`: the callable
produced a real edge color in the graph that padded the (empty)
subgraph edge partition, tripping the check (networkxgh-8738).

Fix: only bail out when the subgraph has a genuinely non-empty part
beyond the matched colors, i.e. a color that really only the subgraph
has. Padding sets are empty and are correctly ignored.

Add regression tests covering both the edge-color and node-color cases,
including that a subgraph-only color still yields no isomorphisms.
@dschult

dschult commented Jul 26, 2026

Copy link
Copy Markdown
Member

Thanks for this bug-fix! The description is clear and the solution is elegant.
I haven't looked through the details thoroughly yet, but my intuition says it should work.
I'll work through the details soon.

:)

@dschult

dschult commented Jul 29, 2026

Copy link
Copy Markdown
Member

It feels like the code has a design wart that lead to the bug. Why are we padding the partition? It makes the partition look longer than it really is. Your fix is certainly a good fix. But I'm thinking there might be a way to remove the design wart so a fix isn't needed. That could help avoid some cases with lots of unneeded padding, as well as the any loop over all those empty sets. I'm worried that with a large graph with many colors, the padding on _sge_partitions could become fairly large,

These tests are really helpful in pinpointing the trouble.

[Added later]
It looks like we never use the padding in self._sge_partitions. We use the padding that is in self._ge_partitions when we look up graph node/edge candidates based on the _sge_colors. But we don't actually use the padding we create in the subgraph partition.

What do you think of reverting your fix (back to using len(self._sge_partitions) and instead removing the padding of new_sg_p on line 653?

@dschult

dschult commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

I added some tests of the node color issue. The fix here fixes those problems too.
And I went ahead and switched the fix to removing the padding for subgraphs.
We can switch it back if there is a reason to -- but it saves time and memory and
IMO removes a wart in my design of the class. Both fixes fix all the troubles from #8738.

Let me know what you think! (and pull this from your fork to your local branch before
you make any further changes)

@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 ready.
I approve this PR.

@dschult dschult added this to the 3.7 milestone Aug 2, 2026

@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 @deepakganesh78 and @dschult for chasing this down!

@rossbar
rossbar merged commit 139259c into networkx:main Aug 4, 2026
43 checks passed
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.

ISMAGS shows wrong behaviour with edge and node match functions

3 participants