From 3ec79c53cb4e2e199e26ba5b56c55eda1c6e4a06 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 20 Jun 2022 03:30:37 +0100 Subject: [PATCH 1/5] added support for induced bipartite subgraphs --- src/operators.jl | 34 ++++++++++++++++++++++++++++++++++ test/operators.jl | 9 +++++++++ 2 files changed, 43 insertions(+) diff --git a/src/operators.jl b/src/operators.jl index daa597b66..6fd925c3e 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -708,6 +708,40 @@ 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("X and Y sould not intersect!") + !(X ⊆ 1:nv(G) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") + + unique!(X) + unique!(Y) + + n = length(X) + length(Y) + G = T(n) + + newIndex = Dict(reverse.(collect(enumerate([X;Y])))) + + for x in X + for y in neighbors(g,x) ∩ Y + add_edge!(G,newIndex[x],newIndex[y]) + end + end + + for y in Y + for x in neighbors(g,y) ∩ X + 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 From dc3b24cf59ec29a5e7a85765af485317f7b62856 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 20 Jun 2022 03:41:14 +0100 Subject: [PATCH 2/5] added support for DiGraphs --- src/operators.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index 6fd925c3e..8c0b4057c 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -728,13 +728,13 @@ function induced_bipartite_subgraph(g::T,X::AbstractVector{U},Y::AbstractVector{ newIndex = Dict(reverse.(collect(enumerate([X;Y])))) for x in X - for y in neighbors(g,x) ∩ Y + for y in outneighbors(g,x) ∩ Y add_edge!(G,newIndex[x],newIndex[y]) end end for y in Y - for x in neighbors(g,y) ∩ X + for x in outneighbors(g,y) ∩ X add_edge!(G,newIndex[y],newIndex[x]) end end From 8ed3577db7f1c42b7c39ca3bc1d8b4331dba741a Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 21 Jun 2022 17:30:40 +0100 Subject: [PATCH 3/5] refactor better to match style --- src/Graphs.jl | 2 +- src/operators.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 8c0b4057c..af3d31836 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -717,7 +717,7 @@ Return the bipartite subgraph of `g` induced by the disjoint subsets of vertices function induced_bipartite_subgraph(g::T,X::AbstractVector{U},Y::AbstractVector{U}) where T <: AbstractGraph where U <: Integer X ∩ Y != [] && throw("X and Y sould not intersect!") - !(X ⊆ 1:nv(G) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") + !(X ⊆ 1:nv(g) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") unique!(X) unique!(Y) From 21edb53b1ea1070409f0dce17175db7be0a750a5 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 21 Jun 2022 17:30:40 +0100 Subject: [PATCH 4/5] refactor to better match style --- src/Graphs.jl | 2 +- src/operators.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 8c0b4057c..af3d31836 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -717,7 +717,7 @@ Return the bipartite subgraph of `g` induced by the disjoint subsets of vertices function induced_bipartite_subgraph(g::T,X::AbstractVector{U},Y::AbstractVector{U}) where T <: AbstractGraph where U <: Integer X ∩ Y != [] && throw("X and Y sould not intersect!") - !(X ⊆ 1:nv(G) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") + !(X ⊆ 1:nv(g) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") unique!(X) unique!(Y) From d48860a919b0b2f0c383b76235be53fdcc7fcca7 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 21 Jun 2022 20:39:01 +0100 Subject: [PATCH 5/5] included most of the comments made by simonschoelly --- src/operators.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index af3d31836..3c6dd0668 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -716,25 +716,28 @@ Return the bipartite subgraph of `g` induced by the disjoint subsets of vertices """ function induced_bipartite_subgraph(g::T,X::AbstractVector{U},Y::AbstractVector{U}) where T <: AbstractGraph where U <: Integer - X ∩ Y != [] && throw("X and Y sould not intersect!") - !(X ⊆ 1:nv(g) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices") - - unique!(X) - unique!(Y) + 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 + 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 + for x in outneighbors(g,y) + (x ∉ X_set) && continue add_edge!(G,newIndex[y],newIndex[x]) end end