Fix the __eq__ method of GeneralGraph and add __hash__#254
Merged
kunwuz merged 5 commits intopy-why:mainfrom Feb 18, 2026
Merged
Fix the __eq__ method of GeneralGraph and add __hash__#254kunwuz merged 5 commits intopy-why:mainfrom
kunwuz merged 5 commits intopy-why:mainfrom
Conversation
Signed-off-by: Ykabrit <ykabrit@yanibou.fr>
Signed-off-by: Ykabrit <ykabrit@yanibou.fr>
Signed-off-by: Ykabrit <ykabrit@yanibou.fr>
zhi-yi-huang
requested changes
Feb 17, 2026
Collaborator
zhi-yi-huang
left a comment
There was a problem hiding this comment.
Thanks for your contribution! I have one suggestion regarding the hash calculation: instead of computing hash(node_tuple) + hash(graph_tuple) and using that sum as the combined hash, it would be better to combine the two tuples into a single tuple first and then hash that—i.e., hash((node_tuple, graph_tuple)). This helps to better maintain the uniformity and uniqueness of hashes.
Signed-off-by: Ykabrit <ykabrit@yanibou.fr>
Contributor
Author
|
You're right! Here, normally it's fixed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves #251 by modifying the
__eq__method inGeneralGraph.As implemented initially, this method breaks graphs by sorting
self.nodesin-place (which renders the list of nodes incoherent withself.graphandself.node_map), as well as returningFalsewhen two identical (but having different node ordering inself.nodes) graphs because it comparesself.graphandother.graphwhich are not identical if the node ordering was different.In the modified method,
self.nodesandother.nodesare both sorted using thesorted()method and stored in local variables. Secondly, rows and columns ofother.graphare permutated in order to correspond toself.graphand make them comparable, which was not the case before.Not entirely related, but I also added
__hash__methods toGeneralGraphandEdge.