Skip to content

ENH: adds ISMAGS support for directed and multigraph with tests and refactor - #8274

Merged
rossbar merged 30 commits into
networkx:mainfrom
dschult:ismags_refactored_digraph
Oct 13, 2025
Merged

rossbar merged 30 commits into
networkx:mainfrom
dschult:ismags_refactored_digraph

Conversation

@dschult

@dschult dschult commented Sep 17, 2025 •

Copy link
Copy Markdown
Member

With this PR, ISMAGS supports directed and multigraph inputs. If inputs do not have the same directed status an exception is raised. But multigraph and graph can be compared. So no restriction there.

Refactored methods _refine_node_partition, _map_nodes, and _process_ordered_pair_partitions to replace the recursive with a (DFS) queue approach. In addition, the ISMAGS class no longer requires that nodes be sortable.

Corrected error while handling candidate_sets which is a dict-of-set-of-frozensets. For each node, we want to find the minimum length of the frozensets associated with that node. So we should use min(len(x) for x in candidate_sets[n]) rather than min(candidate_sets[n], key=len) which returns the smallest length frozenset. This ensures that the nodes with the fewest candidate mapping-image nodes are considered first, which in theory should return the first isomorphism faster. (Not a correctness issue -- just a performance hit.)

In switching away from recursive methods, we somehow no longer need the bug-fix for ISMAGS. The bug arose for strange cases where symmetry was getting top/bottom partitions with different numbers of elements. I haven't chased down how that error occurred in the original code, but I suspect we were not respecting the node order or the ordering of partition list by the size of the parts. It doesn't affect the answer, but reduces the number of partitions to consider (the strange cases were aborted once detected).

This PR updates and adds tests especially DiGraph tests from VF, VF2, VF2pp.

Many comment changes, variable name changes and removal or renaming of helper functions. I also removed the manually provided cached properties in favor of just computing those attributes in the __init__ method (they would never change unless the graphs change which would violate other assumptions).

Names: a "partition" is a list of "parts". Each "part" is a set of nodes.
We should only use the plural "partitions" when we have more than one partition.

I did not change the term "color" to "label" in this PR. I also did not move functions in or out of the class structure (to make a function oriented setup instead of methods).
I did not refactor the methods like is_isomorphic to be functions. We can discuss those changes for the whole isomorphism subpackage separately.

I thought about keeping the old code alongside the new, but given the needed correction and the bug-fix shortcut, I don't think we should retain that version. The Sandia folks mentioned that they saw some suspicious parts of the ismags code.

I did not use the Sandia python version of ISMAGS because it was clearly a translation of the original java code, and didn't feel right for NetworkX. It is also a command line tool used for graphs stored in a specific ascii file structure e.g. "XX00YX" to indicate which edges exist between, e.g., 3 nodes and their edge type/color/label.

Are there things I can do to make this easier to review? It's often hard to review the switch from recursive to non-recursive. And we've got 3 such conversions here. Should I split that from the handling of DiGraph changes? It might be worth reading the new ismags.py as its own new file rather than the diff, but I'm not sure. I will wait a week or two and then review it with fresh eyes if that helps.

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

This looks great! It'll take some time for me to do a deep dive to understand what each part is doing here but in the meantime, here are some surface-level comments.

@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 @dschult , this is great work! I've done an initial readthrough and had mostly a couple questions about mid-level details. The layout of the PR and in particular the PR summary are very helpful for motivating/providing context for the changes. I'd like to take another pass and spend more time absorbing some of the more detailed methods e.g. _map_nodes and _process_ordered_pair_partitions, but I'd need to beef up on ISMAGs itself before I'd be confident weighing in more generally. After my first pass I'm very impressed and excited about these improvements!

@dschult dschult left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to address all comments either in the new commits or in the discussions.

split _refine_node_partition from _refine_opp
add tests from referenced papers
@dschult

dschult commented Oct 1, 2025

Copy link
Copy Markdown
Member Author

I added a (maybe too long) description of the algorithm in the module's doc_string. If its too much into the weeds, we can turn it into comments -- or shift it to a doc_string of maybe the class object.

I also dug into the Ordered-Pair-Paritition (Symmetry) DFS shortcuts. The ISMAGS paper has one fairly simple example and refers to a paper by Kategi, et al. which has more details. It turns out that we were not short-cutting some of the suggested short-cuts. I looked at the DFS nodes we are visiting and pruning and it all makes more sense to me now. Plus I was able to simplify some of the logic for pruning branches and add the missing cases (involving "Matching OPPs"). I hope that code is also more readable. I started getting into moving the OPP related methods to be defied near the process_OPP method, but decided moving methods around is a slippery slope and we should get this in, with a follow-up PR to reorganize the class structure, etc.

Added tests from the papers. A good one is a graph with two distinct cycles (triangle and square). And I included the Petersen Graph since that is known to have many symmetries for not many nodes.

Lots of misc tweaking here and there too. Hard to stop myself sometimes. But I think its all for the good. I will go through one more time later today for left-behind or commented out lines that should be removed. :)

@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 for the updates @dschult , they look great to me!

I'm personally in favor of the giant explainer in the notes section - reading through it helped developed a more concrete idea of some of the key concepts like OPPs, cosets, etc. and how they relate to symmetries and DFS constraints. If we wanted to move the text elsewhere (e.g. the ISMAGS module docs might be a relevant place) that's fine too, but I'm +1 on making sure this text is available somewhere (and would vote to push off deciding where exactly to a followup PR).

I left a bunch of minor suggestions related to grammar/formatting which are generally inconsequential.

Re: the big picture, if you think this is in a state where it's ready to go in (i.e. there's no major changes you think need to be added - we can always continue tweaking) then I'd be +1. For this round I only focused on the changes in 65b133c, but would happy to go over the entire PR again if you think it's ready!

@dschult
dschult force-pushed the ismags_refactored_digraph branch from 8c41162 to fad3c2b Compare October 2, 2025 14:55
@dschult

dschult commented Oct 2, 2025 •

Copy link
Copy Markdown
Member Author

Thanks @rossbar for the review -- I've got another set of wording tweaks, varnames, doc_strings, comments.

I also switched from using set operation union to |.

I found a strange feature of _get_permutations_by_length. We never call this function with more than a single length of items. That is, when we call it, all the items have the same length. The function still "works" with more than one item length as shown by the doc_string example. But the code that uses the results (it is only called in one place) relies on the output having only one length for all items. Our tests don't cover any cases with refined parts having different lengths. It might not be possible to refine a part using make_partitions and get more than one length for the new parts. If not possible, then we could simplify this function. But as is (and was before this PR), the algorithm will fail if we have refined parts after coupling with multiple lengths. So I added an assert that checks for this case -- better than silently ignoring a bunch of sets in our partition. And I added a TODO comment to figure out whether we are guaranteed to have a single length for all refined parts after coupling.

I think this PR is ready to merge!
I'll do more with ismags as we start to unify code structures with the other isomorphism tools. And that TODO will bug me until I figure it out. But those are furture work. This PR is ready and it works.

In summary:
This adds support for digraph, multigraph and non-sortable nodes.
It adds search shortcuts that are in the papers but not in the code.
It adds a fairly large (too large?) algorithm description to the class doc_string (could be put in the module doc_string instead -- we can change if desired).
It has more tests and it passes all previous tests. It hopefully improves the readability, comments and doc_strings.

@dschult
dschult force-pushed the ismags_refactored_digraph branch from a31de74 to 31a42da Compare October 2, 2025 15:21

@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 do one more look-over of the entire PR and everything LGTM! I noticed that there was one instance of the ISMAGS class docstring Attributes section having gotten out-of-date so I went ahead and removed the attributes which no longer exist1. This is a very nice refactor and provides nice new functionality - I'm excited to get this in users' hands!

On that note I'd like to get this into 3.6!

Footnotes

  1. We could store the edge_match and node_match functions as attributes on the ISMAGS object if we wanted to, though with this refactor they're only used during problem setup so there's no reason to any longer ↩

@Peiffap

Peiffap commented Oct 2, 2025

Copy link
Copy Markdown
Member

I'm not sure I'll be able to go over this in depth until next week-end (Oct. 11-12) :(. I haven't forgotten about this PR though.
When is 3.6 planned to be released?

@rossbar

rossbar commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

When is 3.6 planned to be released?

There is no exact date, but NX typically does a release in October following the Python version bump.

@dschult

dschult commented Oct 6, 2025

Copy link
Copy Markdown
Member Author

I worked through the rabbit hole of finding a case where refining partitions gives different size partitions in the same refinement. So the "assert" I put in this PR is warranted, and needs a fix.

The example involves finding a case where an initial refinement passes just fine, but a refinement of the refined parts produces multiple lengths for the refined partitions it considers. The construction looks at an underlying graph with a 4-cycle and a separated edge. I used multiedges to make all nodes have degree 3. That gets them past the initial refinement, and then past the first layer of refinements inside the algorithm. Then to get inside the next layer of refinements, I stretch the graph by replacing each edge with a path (with one node along the path). That means looking at neighbors will not be enough to find new colors until we have refined more than once on the same part of the partition. My intuition there is that each refinement of the partition checks colors that have changed on the previous refinement -- essentially moving out from the initially changed node to its neighbors-neighbors-neighbors, etc. So long as parts keep getting split (new colors formed) the refinement continues. So stretching the edges into paths gets us into the inner layer of refinements. The example in a test causes the assert for only one length at a time to fail. And the fix is pretty short -- we just need to flatten the tuple-of-tuples-of-sets returned by _get_permutations_by_length into a list of sets.

All areas of my concern have now been addressed. So any follow-up PRs can focus on code organization and alignment across methods. (I hope:). Let's wait for comments from @Peiffap unless the release comes up too quickly.

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

This PR adds directed graph and multigraph support to the ISMAGS implementation. The core algorithmic improvement is a refactor from recursive to iterative implementations for three methods: _refine_node_partition, _map_nodes, and _process_ordered_pair_partitions. This eliminates the need for sortable nodes and resolves a bug in partition refinement.
The PR also adds (very) helpful and extensive documentation for the ISMAGS algorithm, as well as a ton of tests for the new behavior.


Globally, this looks good to me! I have a bunch of nits from my initial read-through, some questions about the algorithm, a suggestion to avoid a loop. The only thing I feel somewhat strongly about are the assertions (the two I flagged + a few others); I think those should raise instead.

I agree with Ross that this should go in before the release! This is 100% a net improvement to the ISMAGS code, and if there are any issues with it, the fastest way to find out is to let people try to break it :) Thanks Dan!

@dschult dschult left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these comments! I especially like the dict-comprehension, and the discussion about 2-tuple edges vs 2-sets of edges and how that affects items(). Also a good catch about assert statements. Those shouldn't be here. I hope to find an example test that causes the exception to be raised, but that will have to wait for another day. :)

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

The exception needs an nx. prefix, and I have a test for it if you want to add one.
Once that is fixed this is good to go in for me! Thanks Dan :)

@dschult

dschult commented Oct 13, 2025 •

Copy link
Copy Markdown
Member Author

I think we need to overhaul the match function system. It's too easy to make a funky match function that messes things up. This latest commit adds a check that the partitions made via a user's match function gets checked for 1) having every pair of nodes within each part match and 2) having no two nodes in different parts match. Perhaps that turns out not to be time instensive. Then we can leave it in -- and spread it to the other isomorphism algs. Otherwise lets create some good tools for folks to roll their own and check it! Or to restrict what they are allowed to do (e.g. we could require them to have a node/edge attribute indicating the group -- and then we only match the values of a specified attribute).

Anyway, I think this solution here is overkill, but we can clean the marked unneeded part later -- and figure out how to handle matching more generally.

Thanks @Peiffap !

@rossbar

rossbar commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

Thanks @Peiffap for the round of review! I will merge this to get the largest bit of the overhaul in so folks can start focusing in on individual todo's/idea exploration in follow-up PRs. Many thanks @dschult for the excellent PR and accompanying discussion, and to @Peiffap for the nice review!

@rossbar
rossbar merged commit 3a7f729 into networkx:main Oct 13, 2025
@dschult dschult added this to the 3.6 milestone Oct 13, 2025
@dschult
dschult deleted the ismags_refactored_digraph branch October 14, 2025 12:41
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