Skip to content

Commit 9bb747f

Browse files
etiennedegbkamins
andauthored
[Port] fix induced_subgraph indexing with vector of Bools (#22)
* fix induced_subgraph indexing with vector of Bools Currently both `getindex` and `induced_subgraph` handle `AbstractVector{Bool}` argument in a way inconsistent with Julia Base rules. * add tests Co-authored-by: Bogumił Kamiński <[email protected]>
1 parent c5844a1 commit 9bb747f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/operators.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ function induced_subgraph(g::T, vlist::AbstractVector{U}) where T <: AbstractGra
659659
return h, vmap
660660
end
661661

662+
function induced_subgraph(g::AbstractGraph, vlist::AbstractVector{Bool})
663+
length(vlist) == length(g) || throw(BoundsError(g, vlist))
664+
return induced_subgraph(g, findall(vlist))
665+
end
662666

663667
function induced_subgraph(g::AG, elist::AbstractVector{U}) where AG <: AbstractGraph{T} where T where U <: AbstractEdge
664668
h = zero(g)

test/operators.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@
273273
@test h2 == h
274274
@test vm == collect(r)
275275
@test h2 == g[r]
276+
277+
r2 = falses(length(g))
278+
r2[r] .= true
279+
h2, vm = @inferred(induced_subgraph(g, r2))
280+
@test h2 == h
281+
@test vm == findall(r2)
282+
@test h2 == g[r2]
283+
276284
end
277285

278286
g10 = complete_graph(10)
@@ -285,6 +293,12 @@
285293
@test sg2 == sg
286294
@test vm[4] == 8
287295

296+
bv = falses(length(g))
297+
bv[5:8] .= true
298+
sg2, vm = @inferred(induced_subgraph(g, bv))
299+
@test sg2 == sg
300+
@test vm[4] == 8
301+
288302
elist = [
289303
SimpleEdge(1, 2), SimpleEdge(2, 3), SimpleEdge(3, 4),
290304
SimpleEdge(4, 5), SimpleEdge(5, 1)

0 commit comments

Comments
 (0)