sbromberger / SimpleWeightedGraphs.jl

Simple weighted graphs. Requires LightGraphs.jl.
Other
49 stars 22 forks source link

Induced subgraph #33

Closed jtackm closed 5 years ago

jtackm commented 5 years ago

LightGraphs and MetaGraphs have the nice feature of selecting a subgraph through convenient slicing:

julia> using LightGraphs, MetaGraphs, Random
julia> Random.seed!(1234)
julia> G = erdos_renyi(100, 50)
{100, 50} undirected simple Int64 graph
julia> G[1:10]
{10, 1} undirected simple Int64 graph

julia> Gm = MetaGraph(G)
julia> Gm[1:10]
{10, 1} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)

However, this seems to be not yet supported in SimpleWeightedGraphs:

julia> using SimpleWeightedGraphs
julia> Gw = SimpleWeightedGraph(G)
julia> Gw[1:10]

ERROR: MethodError: no method matching SimpleWeightedEdge{Int64,Float64}(::LightGraphs.SimpleGraphs.SimpleEdge{Int64})
Closest candidates are:
  SimpleWeightedEdge{Int64,Float64}(::Any, ::Any, ::Any) where {T<:Integer, U<:Real} at /mnt/gaia/janko/.julia/packages/SimpleWeightedGraphs/diCGL/src/simpleweightededge.jl:7
  SimpleWeightedEdge{Int64,Float64}(::Pair) where {U<:Real, T<:Integer} at /mnt/gaia/janko/.julia/packages/SimpleWeightedGraphs/diCGL/src/simpleweightededge.jl:15
  SimpleWeightedEdge{Int64,Float64}(::Tuple{T,T} where T) where {U<:Real, T<:Integer} at /mnt/gaia/janko/.julia/packages/SimpleWeightedGraphs/diCGL/src/simpleweightededge.jl:17
  ...
Stacktrace:
 [1] add_edge!(::SimpleWeightedGraph{Int64,Float64}, ::LightGraphs.SimpleGraphs.SimpleEdge{Int64}) at /mnt/gaia/janko/.julia/packages/SimpleWeightedGraphs/diCGL/src/SimpleWeightedGraphs.jl:73
 [2] induced_subgraph(::SimpleWeightedGraph{Int64,Float64}, ::UnitRange{Int64}) at /mnt/gaia/janko/.julia/packages/LightGraphs/PPsyP/src/operators.jl:506
 [3] getindex(::SimpleWeightedGraph{Int64,Float64}, ::UnitRange{Int64}) at /mnt/gaia/janko/.julia/packages/LightGraphs/PPsyP/src/operators.jl:540
 [4] top-level scope at none:0

Would it be possible to add this helpful behaviour?

sbromberger commented 5 years ago

Stand by.

jtackm commented 5 years ago

Works like a charm (and is even faster than Graph). Thanks a lot!