Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Induced bipartite #147

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From convention it would be better to use G instead of T for the graph type. Furthermore, as far as I understand, this method would not work on any kind of AbstractGraph, only on SimpleGraph and SimpleDiGraph. So I would restrict it to these types of graphs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the style of induced_subgraph that also uses T for the graph type. If this is more consistent with the convention, I can change it. Also, we could follow what's been done for induced_subgraph which also returns a mapping from the subgraph to the whole graph. This solution would let people use this function for any AbstractGraph (for example, using the mapping to retrieve information in a metagraph)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think following induced_subgraph is a good idea for the mapping, for the types I'd go with G


X ∩ Y != [] && throw("X and Y sould not intersect!")
Codsilla marked this conversation as resolved.
Show resolved Hide resolved
!(X ⊆ 1:nv(g) && Y ⊆ 1:nv(g)) && throw("X and Y sould be subsets of the vertices")
Codsilla marked this conversation as resolved.
Show resolved Hide resolved

unique!(X)
unique!(Y)
Codsilla marked this conversation as resolved.
Show resolved Hide resolved

n = length(X) + length(Y)
G = T(n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no guarantee this constructor exists (I'm not sure how we solve that though)


newIndex = Dict(reverse.(collect(enumerate([X;Y]))))

for x in X
for y in outneighbors(g,x) ∩ Y
gdalle marked this conversation as resolved.
Show resolved Hide resolved
add_edge!(G,newIndex[x],newIndex[y])
end
end

for y in Y
for x in outneighbors(g,y) ∩ X
add_edge!(G,newIndex[y],newIndex[x])
end
end
G
end



"""
compute_shifts(n::Int, x::AbstractArray)
Expand Down
9 changes: 9 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
@test sort(vm) == [1:5;]
end


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless diff

gs = star_graph(10)
distgs = fill(4.0, 10, 10)
@testset "Egonet: $g" for g in testgraphs(gs)
Expand All @@ -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
Comment on lines +322 to +328
Copy link
Contributor

@simonschoelly simonschoelly Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is good, I think we need a few more test cases, e.g.

  • For empty graphs
  • For graphs with self-loops
  • For graphs that are not complete graphs
  • For directed graphs
  • For cases, where an exception is being thrown
  • For U of different type than Int

It might also be good to test more than just some properties, namely if the subgraph is actually the one that we wanted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that more tests would be nice


gx = SimpleGraph(100)
@testset "Length: $g" for g in testgraphs(gx)
@test length(g) == 10000
Expand Down