From a815e792073579282818c1e2a1fb0834b892bcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Mon, 5 Jul 2021 18:39:58 +0200 Subject: [PATCH 1/2] 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. --- src/operators.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/operators.jl b/src/operators.jl index 77496d323..4c563eabc 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -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) From f7f7a1e2fd739aee52d7b1a6f28952d3fe1919ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Mon, 5 Jul 2021 18:43:44 +0200 Subject: [PATCH 2/2] add tests --- test/operators.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/operators.jl b/test/operators.jl index a0809205f..c406e431e 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -266,6 +266,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) @@ -278,6 +286,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)