@@ -162,12 +162,50 @@ Compute the weighted subgraph induced by a list of vertices.
162
162
Return a tuple containing the new graph and the list of vertices.
163
163
"""
164
164
function 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)
168
168
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)]
170
170
newg = zero (g)
171
171
newg. weights = new_weights
172
- return newg, Vector {E} (vlist)
172
+ return newg, Vector {T} (vlist)
173
+ end
174
+
175
+ function Graphs. induced_subgraph (
176
+ g:: G , elist:: AbstractVector{E}
177
+ ) where {G<: AbstractSimpleWeightedGraph } where {E<: AbstractEdge }
178
+ allunique (elist) || throw (ArgumentError (" Edges in subgraph list must be unique" ))
179
+ T, U = eltype (g), weighttype (g)
180
+ vertex_set = Set {T} ()
181
+ for e in elist
182
+ if has_edge (g, e)
183
+ push! (vertex_set, src (e), dst (e))
184
+ else
185
+ @warn " Skipping the edge $(e) , since it does not exist in the graph!"
186
+ end
187
+ end
188
+ vertex_list = collect (vertex_set)
189
+ sort! (vertex_list)
190
+ index_map = Dict (vertex_list[i] => i for i in eachindex (vertex_list))
191
+ n = length (vertex_list)
192
+ new_weights = spzeros (weighttype (g), T, n, n)
193
+ I, J, W = T[], T[], U[]
194
+ for e in elist
195
+ if has_edge (g, e)
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)
201
+ if ! is_directed (g)
202
+ push! (I, i)
203
+ push! (J, j)
204
+ push! (W, w)
205
+ end
206
+ end
207
+ end
208
+ new_weights = sparse (I, J, W)
209
+ newg = G (new_weights)
210
+ return newg, vertex_list
173
211
end
0 commit comments