Skip to content

Commit

Permalink
Functionalities (#36)
Browse files Browse the repository at this point in the history
* add hypergraphs builing models

* add models.jl and test

* add random model.jl functions

* add remove_hyperedge! and clean!

* update for pull request #36

* Co-authored-by: Alessia Antelmi <[email protected]>
  • Loading branch information
spagnuolocarmine authored Apr 29, 2020
1 parent de98bef commit 9531524
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/SimpleHypergraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using JSON3
using JSON

export Hypergraph, getvertices, gethyperedges
export add_vertex!, add_hyperedge!, remove_vertex!
export add_vertex!, add_hyperedge!, remove_vertex!, remove_hyperedge!, prune_hypergraph!, prune_hypergraph
export set_vertex_meta!, get_vertex_meta
export set_hyperedge_meta!, get_hyperedge_meta
export BipartiteView, shortest_path
Expand Down
52 changes: 52 additions & 0 deletions src/hypergraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ function remove_vertex!(h::Hypergraph, v::Int)
h
end


"""
add_hyperedge!(h::Hypergraph{T, V, E, D};
vertices::D = D(), he_meta::Union{E,Nothing}=nothing
Expand All @@ -280,6 +281,57 @@ function add_hyperedge!(h::Hypergraph{T, V, E, D};
ix
end

"""
remove_hyperedge!(h::Hypergraph, e::Int)
Removes the heyperedge `e` from a given hypergraph `h`.
Note that running this function will cause reordering of hyperedges in the
hypergraph: the hyperedge `e` will replaced by the last hyperedge of the hypergraph
and the list of hyperedges will be shrunk.
"""
function remove_hyperedge!(h::Hypergraph, e::Int)
ne = nhe(h)
@assert(e <= ne)
if e < ne
h.he2v[e] = h.he2v[ne]
h.he_meta[e] = h.he_meta[ne]
end

for he in h.v2he
if e < ne && haskey(he, ne)
he[e] = he[ne]
delete!(he, ne)
else
delete!(he, e)
end
end
resize!(h.he2v, length(h.he2v) - 1)
h
end

"""
prune_hypergraph!(h::Hypergraph)
Removes all vertices with degree 0 and all hyperedges of size 0.
"""

function prune_hypergraph!(h::Hypergraph)
for e in reverse(1:nhe(h))
length(h.he2v[e]) == 0 && remove_hyperedge!(h,e)
end
for v in reverse(1:nhv(h))
length(h.v2he[v]) == 0 && remove_vertex!(h,v)
end
h
end

"""
prune_hypergraph(h::Hypergraph)
Returns a pruned copy of `h`, removing all vertices with degree 0 and all hyperedges of size 0.
"""

function prune_hypergraph(h::Hypergraph)
prune_hypergraph!(deepcopy(h))
end

"""
set_vertex_meta!(h::Hypergraph{T, V, E, D}, new_value::Union{V,Nothing},
id::Int) where {T <: Real, V, E, D <: AbstractDict{Int,T}}
Expand Down
2 changes: 1 addition & 1 deletion src/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ function next_nodes(h::Hypergraph, size::Int)
deleteat!(ids, index)
end
nodes
end
end
17 changes: 16 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ h1[5,2] = 6.5

Hᵣ3 = random_model(5,1)
@test nhe(Hᵣ3) == 1

= random_kuniform_model(5, 5, 3)
@test nhv(Hκ) == 5
@test nhe(Hκ) == 5
Expand Down Expand Up @@ -168,6 +168,21 @@ end;
setindex!(h1_0, nothing, 1, 1)
@test h1_0[1,1] == nothing
@test_throws BoundsError setindex!(h1_0, nothing, 10, 9)

h1_1 = Hypergraph([nothing nothing nothing nothing
1 1 nothing nothing
nothing nothing 1 nothing
nothing nothing 1 nothing])
@test add_hyperedge!(h1_1) == 5
@test size(remove_hyperedge!(h1_1, 5))[2] == 4
@test add_vertex!(h1_1) == 5
@test add_hyperedge!(h1_1) == 5
hp = prune_hypergraph(h1_1)
@test size(hp)[1] == 3 && size(h)[1] == 4
@test size(hp)[2] == 3 && size(h)[1] == 4
prune_hypergraph!(h1_1)
@test size(h1_1)[1] == 3
@test size(h1_1)[2] == 3
end;

@testset "SimpleHypergraphs BipartiteView " begin
Expand Down

0 comments on commit 9531524

Please sign in to comment.