Graphs.jl icon indicating copy to clipboard operation
Graphs.jl copied to clipboard

`todirected` and `toundirected` interfaces

Open filchristou opened this issue 2 years ago • 1 comments

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 :)

filchristou avatar Feb 02 '23 12:02 filchristou

I think this would help a lot! Related: #98

gdalle avatar Jun 16 '23 13:06 gdalle