diff --git a/src/Graphs.jl b/src/Graphs.jl index 6d5ecf057..54b7c04d6 100644 --- a/src/Graphs.jl +++ b/src/Graphs.jl @@ -55,7 +55,7 @@ MinkowskiCost, BoundedMinkowskiCost, complement, reverse, reverse!, blockdiag, union, intersect, difference, symmetric_difference, join, tensor_product, cartesian_product, crosspath, -induced_subgraph, egonet, merge_vertices!, merge_vertices, +induced_subgraph, induced_bipartite_subgraph, egonet, merge_vertices!, merge_vertices, # bfs gdistances, gdistances!, bfs_tree, bfs_parents, has_path, diff --git a/src/operators.jl b/src/operators.jl index daa597b66..3c6dd0668 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -708,6 +708,43 @@ egonet(g::AbstractGraph{T}, v::Integer, d::Integer, distmx::AbstractMatrix{U}=we g[neighborhood(g, v, d, distmx, dir=dir)] +""" + induced_bipartite_subgraph(G,X,Y) + +Return the bipartite subgraph of `g` induced by the disjoint subsets of vertices X and Y. + +""" +function induced_bipartite_subgraph(g::T,X::AbstractVector{U},Y::AbstractVector{U}) where T <: AbstractGraph where U <: Integer + + X ∩ Y != [] && throw(ArgumentError("X and Y sould not intersect!")) + !(X ⊆ vertices(g) && Y ⊆ vertices(g)) && throw(ArgumentError("X and Y should be subsets of the vertices")) + !(allunique(X) && allunique(Y)) && throw(ArgumentError("Vertices in subsets of vertices must be unique")) + + + n = length(X) + length(Y) + G = T(n) + Y_set = Set(Y) + X_set = Set(X) + + newIndex = Dict(reverse.(collect(enumerate([X;Y])))) + + for x in X + for y in outneighbors(g,x) + (y ∉ Y_set) && continue + add_edge!(G,newIndex[x],newIndex[y]) + end + end + + for y in Y + for x in outneighbors(g,y) + (x ∉ X_set) && continue + add_edge!(G,newIndex[y],newIndex[x]) + end + end + G +end + + """ compute_shifts(n::Int, x::AbstractArray) diff --git a/test/operators.jl b/test/operators.jl index 86b48cb25..f7cd61fd8 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -308,6 +308,7 @@ @test sort(vm) == [1:5;] end + gs = star_graph(10) distgs = fill(4.0, 10, 10) @testset "Egonet: $g" for g in testgraphs(gs) @@ -318,6 +319,14 @@ @test @inferred(ndims(g)) == 2 end + g10 = complete_graph(10) + @testset "Induced bipartite Subgraphs: $g" for g in testgraphs(g10) + sg = @inferred(induced_bipartite_subgraph(g, [2,3],[4,5])) + @test nv(sg) == 4 + @test ne(sg) == 4 + @test is_bipartite(sg) + end + gx = SimpleGraph(100) @testset "Length: $g" for g in testgraphs(gx) @test length(g) == 10000