Skip to content

Fix graph_hash iteration counts and DiGraph handling - #7834

Merged
rossbar merged 24 commits into
networkx:mainfrom
ThijsVromen:bugfix-for-issue-7806
Mar 22, 2025
Merged

rossbar merged 24 commits into
networkx:mainfrom
ThijsVromen:bugfix-for-issue-7806

Conversation

@ThijsVromen

@ThijsVromen ThijsVromen commented Feb 6, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #7806
Includes bugfixes for the issues discussed.
Does not cover the potential extension to MultiGraphs.

Extended testing, but note that a lot of the test in this module in depend on randomly generated graphs. We continued that here. However, that does not necessarily mean all edge-cases are covered (as the original bug showed).
Note that subgraph hashing requires hashing of initial labels for digest size testing. This is not necessarily (and thus omitted) for general graph hashing.

A small optimisation is also made to the original graph hashing method. It used to collect all labels calculated during execution in a counter, and hashed that to get the final graph hash. However, the WL algorithm only relies on the colouring (labels) calculated at the last iteration, so the current versionl only hashes those to obtain the final hash.

@ThijsVromen

ThijsVromen commented Feb 6, 2025 •

Copy link
Copy Markdown
Contributor Author

I think it would make sense if @dschult would review this, since he has been active in the original issue, but I assume I cannot assign a reviewer. Any other feedback is, of course, also welcome!
I'm a bit unsure if this should be a 'draft'. I don't see anything about that in the NetworkX contributor guide.

@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! It is nice to see it all put together.

@MichaelMartini-Celonis can you review this too? Thanks!

I have some comments below, but the main question I have is what will this change for a user who has created hashes before and now updates the NetworkX version to this code. How can we describe the change and can we find words to tell them how to get the previous hash they got before with the new version? I'm hoping it is something like: "change the number of iterations to be one more than it used to be." And of course DiGraph hashes will change since the methodology changed and the previous ones were broken. I'm just hoping that for Graph there will be a way to match previous work.

At the least we need a Note in the doc_string explaining this impact of this change.
Thanks!

Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
@dschult dschult changed the title Bugfix for issue 7806 Fix graph_hash iteration counts and DiGraph handling Feb 8, 2025
@ThijsVromen
ThijsVromen marked this pull request as draft February 10, 2025 14:43
@ThijsVromen

Copy link
Copy Markdown
Contributor Author

Thanks a lot to both of you for the extensive reviews!
I have processed the remarks that I believe don't require further discussion - please feel free to confirm whether you agree with how I did that.
On the topics of terminology and compatibility I've written down my thoughts above, please let me know what you think!

Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
@ThijsVromen

Copy link
Copy Markdown
Contributor Author

Based on your feedback I've made the following changes:

  • Reverted the optimisation to counter use, to make hashes consistent (or at least reproducible) where possible.
  • Included UserWarnings and docstring warnings.

Todo:

  • In these warnings, replace 'version [TBD]' to the actual version in which these changes happen. (I don't yet know which version that would be)
  • Include a warning in the release notes. I assume that is not something I should touch? Based on some old release notes which included an update to graph hashing, I've written a suggestion for that:
    (NB the link looks off because I tried to adhere to the formatting of the release notes, which seemed to be RST instead of markdown)

Warning

Hash values observed in outputs of ~networkx.algorithms.graph_hashing.weisfeiler_lehman_graph_hash and ~networkx.algorithms.graph_hashing.weisfeiler_lehman_subgraph_hashes have changed in version [TBD] due to bug fixes. This concerns exclusively directed graphs and graphs without node/edge attributes. For more details, see issue #7806 <https://github.com/networkx/networkx/issues/7806>_. This means that comparing hashes of the same graph computed with different versions of NetworkX (i.e. before and after version [TBD]) could wrongly fail an isomorphism test (isomorphic graphs always have matching Weisfeiler-Lehman hashes). Users are advised to recalculate any stored graph hashes they may have on upgrading.

Please let me know if I can further improve these warnings!

@ThijsVromen

Copy link
Copy Markdown
Contributor Author

PS: I have no idea why the coverage test now fails, it seems to timeout on a part of the code that I have not touched. So with a bit of luck it will be fine once I've finalised the documentation and resubmit it.

@dschult dschult added this to the 3.5 milestone Mar 12, 2025

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

This PR looks ready to me. I'm going to remove Draft status for this PR and ask for a final review. I have also added the milestone v3.5. If we don't get it in for that release we can change the warnings.

Summary:

  • Issue discussed previously that is now put off until another PR: hashing collected labels instead of hashing collected hashes of labels.
  • zero iterations case now handled
  • warnings added in docs and when input is_directed

@ThijsVromen can you:

  • see what you think of the following wording suggestions
  • add the version number 3.5 instead of TBD in the warnings.
  • anything else before we give final reviews for this?

I approve this PR! (the suggestions are not blockers for merging)

Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
Comment thread networkx/algorithms/graph_hashing.py Resolved
@dschult
dschult marked this pull request as ready for review March 12, 2025 17:58
@rossbar

rossbar commented Mar 17, 2025

Copy link
Copy Markdown
Contributor

Just to make sure I have a clear picture of the current state:

This PR will change the results for the undirected case, and this change is necessary - i.e. is a bug and not a stylistic choice - is that right?

@ThijsVromen

Copy link
Copy Markdown
Contributor Author

Just to make sure I have a clear picture of the current state:

This PR will change the results for the undirected case, and this change is necessary - i.e. is a bug and not a stylistic choice - is that right?

This is correct. To be more precise, the results are changed for all directed graphs and for a subset of undirected graphs (those without node and/or edge attributes). Both changes are bugfixes.

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

Sounds like there's consensus on the change and the code LGTM, thanks @ThijsVromen @MichaelMartini-Celonis & @dschult !

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.

Bugs WL (sub)graph hashing in relation to use degree at initialisation

4 participants