Skip to content

Commit

Permalink
[Port] fix induced_subgraph indexing with vector of Bools (#22)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
etiennedeg and bkamins authored Jan 9, 2022
1 parent c5844a1 commit 9bb747f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ function induced_subgraph(g::T, vlist::AbstractVector{U}) where T <: AbstractGra
return h, vmap
end

function induced_subgraph(g::AbstractGraph, vlist::AbstractVector{Bool})
length(vlist) == length(g) || throw(BoundsError(g, vlist))
return induced_subgraph(g, findall(vlist))
end

function induced_subgraph(g::AG, elist::AbstractVector{U}) where AG <: AbstractGraph{T} where T where U <: AbstractEdge
h = zero(g)
Expand Down
14 changes: 14 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@
@test h2 == h
@test vm == collect(r)
@test h2 == g[r]

r2 = falses(length(g))
r2[r] .= true
h2, vm = @inferred(induced_subgraph(g, r2))
@test h2 == h
@test vm == findall(r2)
@test h2 == g[r2]

end

g10 = complete_graph(10)
Expand All @@ -285,6 +293,12 @@
@test sg2 == sg
@test vm[4] == 8

bv = falses(length(g))
bv[5:8] .= true
sg2, vm = @inferred(induced_subgraph(g, bv))
@test sg2 == sg
@test vm[4] == 8

elist = [
SimpleEdge(1, 2), SimpleEdge(2, 3), SimpleEdge(3, 4),
SimpleEdge(4, 5), SimpleEdge(5, 1)
Expand Down

0 comments on commit 9bb747f

Please sign in to comment.