Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should undirected edges == and hash be undirected? #184

Open
DylanModesitt opened this issue Oct 19, 2022 · 4 comments
Open

Should undirected edges == and hash be undirected? #184

DylanModesitt opened this issue Oct 19, 2022 · 4 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@DylanModesitt
Copy link

DylanModesitt commented Oct 19, 2022

Edges for SimpleGraph still consider their directedness when testing for equality and hashing. This is understandable given that SimpleEdge is used for directed graphs also. Is it worth considering adding a SimpleUndirectedEdge (or similar)? Something along the lines of:

...

function ==(e1::SimpleUndirectedEdge, e2::SimpleUndirectedEdge)
    return (src(e1) == src(e2) && dst(e1) == dst(e2)) ||
        (src(e1) == dst(e2) && dst(e1) == src(e2))
end

hash(e::SimpleUndirectedEdge, h::UInt) = hash(src(e), h)  hash(dst(e), h)

And then having SimpleGraph instead return edges of this type by default?

This would more easily allow users to maintain sets / dictionaries of edges. For instance, the reason I'm opening this is the associated awkwardness I encountered when implementing a minimum cycle basis that maintains such edge sets.

Happy to do a PR if this change is palatable.

@DylanModesitt DylanModesitt added the bug Something isn't working label Oct 19, 2022
@gdalle
Copy link
Member

gdalle commented Nov 20, 2022

Ran into a related issue recently for a graph that had both directed and undirected edges. At the moment this is not supported by Graphs.jl but maybe with something along the lines what you propose it could be

@gdalle gdalle added enhancement New feature or request bug Something isn't working and removed bug Something isn't working labels Nov 20, 2022
@gdalle gdalle added this to the v1.9 milestone Jun 28, 2023
@gdalle
Copy link
Member

gdalle commented Sep 14, 2023

@DylanModesitt a PR would be welcome but I don't know whether it would break things. @simonschoelly thoughts?

@simonschoelly
Copy link
Contributor

I am also afraid this might break things - perhaps this is something we can do in a next mayor release. For simplicity I would propose the types

Edge  # undirected edge
DiEdge  # directed edge

or maybe UEdge instead of Edge?

Something I did in SimpleValueGraphs.jl was to enforce in the constructor of that type, that the first vertex is always smaller than the second one. Then we don't need to check in hash and equals.

@gdalle
Copy link
Member

gdalle commented Oct 9, 2023

Yeah the presence of directed edges in undirected graph is a major correctness footgun in my opinion. I like the idea of ordering by default, and I think deprecating src / dst to replace them with neighbor(e, u) would be a welcome change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
No open projects
Status: Todo
Development

No branches or pull requests

3 participants