Graphs.jl
Graphs.jl copied to clipboard
`todirected` and `toundirected` interfaces
Hi. I think it would be nice to define here these two interface functions. Some pseudocode:
"""
todirected(g::AbstractGraph)
Create a directed instance of graph `g`.
If `g` is already directed, return `g` unchanged.
"""
@tratfn todirected(g::AbstractGraph::IsDirected) = g
@tratfn todirected(g::AbstractGraph::!(IsDirected)) = error("not implemented")
"""
toundirected(g::AbstractGraph)
Create a undirected instance of graph `g`.
If `g` is already undirected, return `g` unchanged.
"""
@traitfn toundirected(g::AbstractGraph::::!(IsDirected)) = g
@traitfn toundirected(g::AbstractGraph::::IsDirected) = error("not implemented")
The implementation for Simple(Di)Graph
is quite easy:
toundirected(g::SimpleDiGraph) = SimpleGraph(g)
todirected(g::SimpleGraph) = SimpleDiGraph(g)
This will help across all the Graphs julia community with packages wanting to define other graphs (e.g. MetaGraph, Multigraph). I think it's a nice interface to have.
Let me know if you are interested and I could try to find some time to make a PR :)
I think this would help a lot! Related: #98