Skip to content

fix: handle empty candidate sets in ISMAGS.largest_common_subgraph - #8890

Merged
MridulS merged 2 commits into
networkx:mainfrom
mturac:fix/issue-8885
Sep 11, 2026
Merged

MridulS merged 2 commits into
networkx:mainfrom
mturac:fix/issue-8885

Conversation

@mturac

@mturac mturac commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

fixes #8885

largest_common_subgraph() crashes with ValueError: min() iterable argument is empty when subgraph and graph dont share any node colors. the guard on line 839 was checking any(candidate_sets.values()) wich is allways truthy because the values are singleton sets even when the inner frozenset is empty.

fixed it to check any(to_be_mapped) insted — if theres nothing to map, yield nothing and return. added two tests for the case where no colors overlap.

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

Thanks for this! Unfortunately this doesn't cover all the cases that the old code covered. The nodes can have no candidates due to either node color or number of self-loops. suggestions below.

assert ismags._sgn_partition == [{1}, {0}, {2}]
assert ismags._gn_partition == [{5}, set(), set(), {2}]

def test_no_common_colors(self):

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.

Let's test for colors and self-loops. Both conditions will result in nodes with no candidates.

Something like:

    def test_color_mismatches(self):
        # see gh-8885
        graph = nx.path_graph(5)
        subgraph = nx.path_graph(5)
        for n in subgraph:
            subgraph.nodes[n]["color"] = "blue"

        # check that normal case works as expected
        ismags = iso.ISMAGS(graph, subgraph)
        assert list(ismags.largest_common_subgraph()) == [{i: i for i in subgraph}]

        # check case when a subgraph node has no candidate color match
        nodematch = nx.isomorphism.categorical_node_match("color", None)
        ismags = iso.ISMAGS(graph, subgraph, node_match=nodematch)
        assert list(ismags.largest_common_subgraph()) == []

        # check case when a subgraph node has no candidate sue to selfloops
        for n in graph:
            graph.add_edge(n, n)
        ismags = iso.ISMAGS(graph, subgraph)
        assert list(ismags.largest_common_subgraph()) == []

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

@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 took the liberty of adding a few corner case tests for largest common subgraph:

  • null graphs
  • small empty graphs (that either do or don't have color mismatches)
  • I changed the PR test of multi-node but no color matches to test both color matches and self-loop matches.

For the fix itself, I updated the if-clause to check for self-loop and/or color no-matches and I tightened up comments somewhat.

I approve this PR. Thanks @mturac

@MridulS
MridulS merged commit c94928e into networkx:main Sep 11, 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 raises ValueError if no nodes are compatible

3 participants