@@ -162,23 +162,23 @@ Compute the weighted subgraph induced by a list of vertices.
162162Return a tuple containing the new graph and the list of vertices.
163163"""
164164function Graphs. induced_subgraph (
165- g:: T , vlist:: AbstractVector{U}
166- ) where {T <: AbstractSimpleWeightedGraph ,U<: Integer }
167- E = eltype (g)
165+ g:: G , vlist:: AbstractVector{U}
166+ ) where {G <: AbstractSimpleWeightedGraph ,U<: Integer }
167+ T = eltype (g)
168168 allunique (vlist) || throw (ArgumentError (" Vertices in subgraph list must be unique" ))
169- new_weights = g. weights[E .(vlist), E .(vlist)]
169+ new_weights = g. weights[T .(vlist), T .(vlist)]
170170 newg = zero (g)
171171 newg. weights = new_weights
172- return newg, Vector {E } (vlist)
172+ return newg, Vector {T } (vlist)
173173end
174174
175175function Graphs. induced_subgraph (
176- g:: T , elist:: AbstractVector{U }
177- ) where {T <: AbstractSimpleWeightedGraph } where {U <: AbstractEdge }
176+ g:: G , elist:: AbstractVector{E }
177+ ) where {G <: AbstractSimpleWeightedGraph } where {E <: AbstractEdge }
178178 allunique (elist) || throw (ArgumentError (" Edges in subgraph list must be unique" ))
179- E = eltype (g)
180- vertex_set = Set {E } ()
181- @inbounds for e in elist
179+ T, U = eltype (g), weighttype (g)
180+ vertex_set = Set {T } ()
181+ for e in elist
182182 if has_edge (g, e)
183183 push! (vertex_set, src (e), dst (e))
184184 else
@@ -187,18 +187,25 @@ function Graphs.induced_subgraph(
187187 end
188188 vertex_list = collect (vertex_set)
189189 sort! (vertex_list)
190- index_map = Dict (vertex_list[i] => i for i in 1 : length (vertex_list))
190+ index_map = Dict (vertex_list[i] => i for i in eachindex (vertex_list))
191191 n = length (vertex_list)
192- new_weights = spzeros (weighttype (g), E, E (n), E (n))
193- @inbounds for e in elist
192+ new_weights = spzeros (weighttype (g), T, n, n)
193+ I, J, W = T[], T[], U[]
194+ for e in elist
194195 if has_edge (g, e)
195- weights = g. weights[dst (e), src (e)]
196- new_weights[index_map[dst (e)], index_map[src (e)]] = weights
196+ i, j = index_map[src (e)], index_map[dst (e)]
197+ w = get_weight (g, dst (e), src (e))
198+ push! (I, j) # storage is transposed!
199+ push! (J, i)
200+ push! (W, w)
197201 if ! is_directed (g)
198- new_weights[index_map[src (e)], index_map[dst (e)]] = weights
202+ push! (I, i)
203+ push! (J, j)
204+ push! (W, w)
199205 end
200206 end
201207 end
202- newg = T (new_weights)
203- return newg, Vector {edgetype(g)} (collect (edges (newg)))
208+ new_weights = sparse (I, J, W)
209+ newg = G (new_weights)
210+ return newg, vertex_list
204211end
0 commit comments