JuliaGraphs / Graphs.jl

An optimized graphs package for the Julia programming language
http://juliagraphs.org/Graphs.jl/
Other
451 stars 87 forks source link

`SimpleGraph(::Base.OneTo)` #365

Open mtfishman opened 3 months ago

mtfishman commented 3 months ago

I think it would be helpful to add constructors:

SimpleGraph(Base.OneTo(4)) == SimpleGraph(4)
SimpleDiGraph(Base.OneTo(4)) == SimpleDiGraph(4)

representing the more general concept of constructing a graph from a set of vertices. It would enable writing generic code like:

rem_all_edges(g::AbstractGraph) = typeof(g)(vertices(g))

which would work with graphs that don't have simple vertices.

This is analogous to constructing Julia arrays from axes:

julia> zeros(Base.OneTo(3), Base.OneTo(3))
3×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

(though for whatever reason Matrix{Float64}(undef, (Base.OneTo(3), Base.OneTo(3))) doesn't work).

gdalle commented 3 months ago

While this makes sense for SimpleGraphs, the method you defined for general graphs would not be guaranteed to work in general. Typically, there are some graphs that cannot exist without edges (think grids) so the constructor would fail.