Skip to content

Commit 6daee73

Browse files
committed
reimplement induced_subgraph for edge induced subgraphs, fix code style
1 parent 86feac3 commit 6daee73

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/overrides.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,29 @@ end
148148
function induced_subgraph(g::T, elist::AbstractVector{U}) where T <: AbstractSimpleWeightedGraph where U <: AbstractEdge
149149
allunique(elist) || throw(ArgumentError("Edges in subgraph list must be unique"))
150150
E = eltype(g)
151-
W = edgetype(g)
152-
vertexSet = Set{E}()
153-
for e in elist
151+
vertex_set = Set{E}()
152+
@inbounds for e in elist
154153
if has_edge(g, e)
155-
push!(vertexSet, src(e), dst(e))
154+
push!(vertex_set, src(e), dst(e))
156155
else
157156
@warn "Skipping the edge $(e), since it does not exist in the graph!"
158157
end
159158
end
160-
vertexList = collect(vertexSet)
161-
new_weights = g.weights[vertexList, vertexList]
162-
163-
newg = zero(g)
164-
newg.weights = new_weights
165-
for e in edges(newg)
166-
if e elist
167-
newg.weights[dst(e), src(e)] = 0
159+
vertex_list = collect(vertex_set)
160+
sort!(vertex_list)
161+
index_map = Dict(vertex_list[i] => i for i=1:length(vertex_list))
162+
n = length(vertex_list)
163+
new_weights = spzeros(weighttype(g), E, E(n), E(n))
164+
@inbounds for e in elist
165+
if has_edge(g, e)
166+
weights = g.weights[dst(e), src(e)]
167+
new_weights[index_map[dst(e)], index_map[src(e)]] = weights
168+
if !is_directed(g)
169+
new_weights[index_map[src(e)], index_map[dst(e)]] = weights
170+
end
168171
end
169172
end
170-
return newg, Vector{W}(collect(edges(newg)))
173+
newg = zero(g)
174+
newg.weights = new_weights
175+
return newg, Vector{edgetype(g)}(collect(edges(newg)))
171176
end

test/simpleweightedgraph.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,25 @@ using SparseArrays
334334
# this testset was implemented for https://github.com/JuliaGraphs/SimpleWeightedGraphs.jl/issues/32
335335
@testset "induced_subgraph should preserve weights for edge lists" begin
336336
g = SimpleWeightedGraph([0 2; 2 0])
337-
graphWeights = weights(g)
338-
expectedGraphWeights = sparse([0 2; 2 0])
339-
@test graphWeights == expectedGraphWeights
337+
expected_graph_weights = sparse([0 2; 2 0])
340338
# vertex induced subgraph
341-
vertexInducedSubgraphWeights = weights(first(induced_subgraph(g, [1, 2])))
342-
@test vertexInducedSubgraphWeights == expectedGraphWeights
339+
vertex_induced_subgraph_weights = weights(first(induced_subgraph(g, [1, 2])))
340+
@test vertex_induced_subgraph_weights == expected_graph_weights
343341
# edge induced subgraph
344-
edgeInducedSubgraphWeights = weights(first(induced_subgraph(g, [Edge(1, 2)])))
345-
@test edgeInducedSubgraphWeights == expectedGraphWeights
342+
edge_induced_subgraph_weights = weights(first(induced_subgraph(g, [Edge(1, 2)])))
343+
@test edge_induced_subgraph_weights == expected_graph_weights
344+
345+
# test edge induced graph with one edge removed
346+
# graph isomorphic to C_5
347+
g = SimpleWeightedGraph([0 2 0 0 2; 2 0 2 0 0; 0 2 0 2 0; 0 0 2 0 2; 2 0 0 2 0])
348+
expected_graph_weights = sparse([0 2 0 0 0; 2 0 2 0 0; 0 2 0 2 0; 0 0 2 0 2; 0 0 0 2 0]);
349+
# create edge induced subgraph isomorphic to P_5. The edge (1, 5) is missing and test if weights are correct.
350+
edge_induced_subgraph_weights = weights(first(induced_subgraph(g, [Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 5)])))
351+
@test edge_induced_subgraph_weights == expected_graph_weights
352+
353+
# test edge induced subgraph which does not contain the whole vertex set, especially remove the first column (vertex 1)
354+
edge_induced_subgraph_weights = weights(first(induced_subgraph(g, [Edge(2, 3)])))
355+
expected_graph_weights = sparse([0 2; 2 0])
356+
@test edge_induced_subgraph_weights == expected_graph_weights
346357
end
347358
end

0 commit comments

Comments
 (0)