Skip to content

Commit

Permalink
minor code layout improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Nov 6, 2019
1 parent db20042 commit 4ab0aba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
14 changes: 6 additions & 8 deletions src/bipartite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function LightGraphs.has_edge(b::BipartiteView, s, d)
end
end

LightGraphs.has_vertex(b::BipartiteView, v::Integer) = 1 <= v <= LightGraphs.nv(b)
LightGraphs.has_vertex(b::BipartiteView, v::Integer) = 1 <= v <= LightGraphs.nv(b)


LightGraphs.outneighbors(b::BipartiteView, v::Integer) = LightGraphs.all_neighbors(b::BipartiteView, v)
Expand All @@ -72,8 +72,6 @@ function LightGraphs.SimpleGraph(b::BipartiteView)
g
end



LightGraphs.is_directed(b::BipartiteView{T}) where T = false

LightGraphs.is_directed(::Type{BipartiteView{T}}) where T = false
Expand All @@ -98,17 +96,17 @@ end

"""
LightGraphs.SimpleGraphs.fadj(b::BipartiteView)
Generates an adjency list for this view of a hypergraph.
Generates an adjency list for this view of a hypergraph.
"""
function LightGraphs.SimpleGraphs.fadj(b::BipartiteView)
res = Vector{Vector{Int}}(undef, LightGraphs.nv(b))

h_nv = length(b.h.v2he)
for i in 1:h_nv
res[i] = h_nv .+ sort!(collect(keys(b.h.v2he[i])))
end
for i in 1:length(b.h.he2v)
for i in 1:length(b.h.he2v)
res[i+h_nv] = sort!(collect(keys(b.h.he2v[i])))
end
res
Expand All @@ -120,4 +118,4 @@ LightGraphs.edges(b::BipartiteView) = LightGraphs.SimpleGraphs.SimpleEdgeIter(b)
LightGraphs.edgetype(b::BipartiteView{T}) where T = LightGraphs.SimpleGraphs.SimpleEdge{Int}

LightGraphs.zero(t::BipartiteView{T}) where T = BipartiteView(Hypergraph{T}(0,0))
LightGraphs.zero(::Type{BipartiteView{T}}) where T = BipartiteView(Hypergraph{T}(0,0))
LightGraphs.zero(::Type{BipartiteView{T}}) where T = BipartiteView(Hypergraph{T}(0,0))
34 changes: 17 additions & 17 deletions src/twosection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Represents a 2-section view of a hypergraph `h`.
Note (1) this is a view - changes to the original hypergraph will be automatically reflected in the view.
Note (2) The view will only work correctly for hypergraphs not having overlapping hyperedges.
To check whether a graph has overlapping edges try `has_overlapping_hedges(h)` - for such graph
you need to fully materialize it rather than use a view.
To check whether a graph has overlapping edges try `has_overlapping_hedges(h)` - for such graph
you need to fully materialize it rather than use a view.
This can be achieved via the `get_twosection_adjacency_mx(h)` method.
**Constructors**
Expand All @@ -19,22 +19,22 @@ Several LightGraphs methods are provided for the compability.
"""
struct TwoSectionView{T<:Real} <: LightGraphs.SimpleGraphs.AbstractSimpleGraph{Int}
h::Hypergraph{T}
TwoSectionView(h::Hypergraph{T}) where { T <: Real } = begin
TwoSectionView(h::Hypergraph{T}) where { T <: Real } = begin
has_overlapping_hedges(h) && error("A two section view can be created only for a graph with non overlapping edges")
new{T}(h)
end
end

"""
has_overlapping_hedges(h::Hypergraph{T}) where { T <: Real }
has_overlapping_hedges(h::Hypergraph{T}) where { T <: Real }
Checks whether a hypergraph has hyperedges connecting the same pairs of vertices.
Such hypergraph cannot be presented as a two section view
"""

function has_overlapping_hedges(h::Hypergraph{T}) where { T <: Real }
function has_overlapping_hedges(h::Hypergraph{T}) where { T <: Real }
minimum(size(h)) == 0 && return false
maximum(get_twosection_adjacency_mx(h;replace_weights=1)) > 1
maximum(get_twosection_adjacency_mx(h;replace_weights=1)) > 1
end

"""
Expand Down Expand Up @@ -76,12 +76,12 @@ function LightGraphs.has_edge(t::TwoSectionView, s, d)
end


LightGraphs.has_vertex(t::TwoSectionView, v::Integer) = 1 <= v <= LightGraphs.nv(t)
LightGraphs.has_vertex(t::TwoSectionView, v::Integer) = 1 <= v <= LightGraphs.nv(t)

LightGraphs.outneighbors(t::TwoSectionView, v::Integer) =
LightGraphs.all_neighbors(t::TwoSectionView, v)

LightGraphs.inneighbors(t::TwoSectionView, v::Integer) =
LightGraphs.inneighbors(t::TwoSectionView, v::Integer) =
LightGraphs.all_neighbors(t::TwoSectionView, v)

"""
Expand Down Expand Up @@ -128,8 +128,8 @@ end

"""
LightGraphs.SimpleGraphs.fadj(t::TwoSectionView)
Generates an adjency list for this view of a hypergraph.
Generates an adjency list for this view of a hypergraph.
"""
function LightGraphs.SimpleGraphs.fadj(t::TwoSectionView)
res = [Vector{Int}() for _ in 1:LightGraphs.nv(t)]
Expand All @@ -152,12 +152,14 @@ LightGraphs.zero(t::TwoSectionView{T}) where T = TwoSectionView(Hypergraph{T}(0,
LightGraphs.zero(::Type{TwoSectionView{T}}) where T = TwoSectionView(Hypergraph{T}(0,0))

"""
get_twosection_adjacency_mx(h::Hypergraph{T,V,E}; count_self_loops::Bool=false, replace_weights::Union{Nothing,Real}=nothing ) where {T<:Real, V, E}
get_twosection_adjacency_mx(h::Hypergraph{T,V,E}; count_self_loops::Bool=false,
replace_weights::Union{Nothing,Real}=nothing ) where {T<:Real, V, E}
Returns an adjacency matrix for a two section view of a hypergraph `h`.
If the
If the
"""
function get_twosection_adjacency_mx(h::Hypergraph{T,V,E}; count_self_loops::Bool=false, replace_weights::Union{Nothing,Real}=nothing ) where {T<:Real, V, E}
function get_twosection_adjacency_mx(h::Hypergraph{T,V,E}; count_self_loops::Bool=false,
replace_weights::Union{Nothing,Real}=nothing ) where {T<:Real, V, E}
mx = zeros(replace_weights==nothing ? T : typeof(replace_weights), nhv(h), nhv(h))
for he in 1:nhe(h)
for v1 in keys(h.he2v[he])
Expand All @@ -169,5 +171,3 @@ function get_twosection_adjacency_mx(h::Hypergraph{T,V,E}; count_self_loops::Boo
end
mx
end


0 comments on commit 4ab0aba

Please sign in to comment.