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

similar for generically constructing graphs of the same/similar type without edges #33

Open
mtfishman opened this issue Feb 13, 2023 · 0 comments

Comments

@mtfishman
Copy link
Member

Graphs.jl doesn't seem to provide a generic interface for creating graphs of the same type without any edges, and possibly with new vertices.

The closest concept in Julia seems to be Base.similar, for example for sparse arrays:

julia> using SparseArrays

julia> A = sprandn(4, 4, 0.5)
4×4 SparseMatrixCSC{Float64, Int64} with 7 stored entries:
                            
 -0.21712       -0.741368   0.199437
                          1.78635
  0.486078       0.593271  -0.199654

julia> similar(A, 4, 4)
4×4 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
               
               
               
               

though for some reason it doesn't work for types:

julia> similar(typeof(A), 4, 4)
ERROR: MethodError: no method matching SparseMatrixCSC{Float64, Int64}(::UndefInitializer, ::Tuple{Int64, Int64})
Closest candidates are:
  SparseMatrixCSC{Tv, Ti}(::LinearAlgebra.UniformScaling, ::Tuple{Int64, Int64}) where {Tv, Ti} at /Applications/Julia-1.8.5.app/Contents/Resources/julia/share/julia/stdlib/v1.8/SparseArrays/src/sparsematrix.jl:1788
Stacktrace:
 [1] similar(#unused#::Type{SparseMatrixCSC{Float64, Int64}}, dims::Tuple{Int64, Int64})
   @ Base ./abstractarray.jl:841
 [2] similar(::Type{SparseMatrixCSC{Float64, Int64}}, ::Int64, ::Int64)
   @ Base ./abstractarray.jl:839
 [3] top-level scope
   @ REPL[20]:1

We could adopt the interface:

Base.similar(g::AbstractGraph, vertices)
Base.similar(g::AbstractGraph, nvertices::Integer)
Base.similar(G::Type{<:AbstractGraph}, vertices)
Base.similar(G::Type{<:AbstractGraph}, nvertices::Integer)

as a way to generically create a graph of the same type G without any edges with either the specified vertex collection or number of vertices. Additionally this could be used as an interface for creating a similar graph with a new vertex type, and new vertex data and edge data types in the case of data graphs.

Originally posted by @mtfishman in #32 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant