Skip to content

fix conductance function and its test #43

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions src/algorithms/conductance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ Calculate unweighted hypergraph conductance of `subset`.
note: ∅ ⊊ `subset` ⊊ `1:nhv(h)`

For more information see `1. Introduction` at:
Generalizing the Hypergraph Laplacian via a Diffusion Processwith Mediators,
auhtors: T-H. Hubert Chan, Xhibin Liang.
Spectral Properties of Hypergraph Laplacian and Approximation Algorithms
auhtors: T-H. Hubert Chan, Anand Louis, Zhihao Gavin Tang, and Chenzi Zhang
"""
function conductance(h::Hypergraph, subset::Set{Int})::Float64
if isempty(subset) error("`subset` is not allowed empty.") end
if subset == Set(1:nhv(h)) error("`subset` is not allowed true subset of `h`.") end
subset2 = setdiff(Set(1:nhv(h)), subset)
wₛ = sum([length(gethyperedges(h, node)) for node in subset])
w∂s = 0
volS = sum([sum(values(gethyperedges(h, node))) for node in subset])
volV_S = sum([sum(values(gethyperedges(h, node))) for node in subset2])
cutS = 0
for he in 1:nhe(h)
he_vertices = keys(getvertices(h, he))
if isempty(intersect(he_vertices, subset)) continue end
if isempty(intersect(he_vertices, subset2)) continue end
w∂s += length(getvertices(h, he))
cutS += 1
end
return w∂s / wₛ
return cutS / min(volS, volV_S)
end
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ end;
@test typeof(randompartition(hg, 2)) == Vector{Set{Int}}
@test length(randompartition(hg, 2)) == 2
@test sort(vcat(collect.(randompartition(hg, 2))...))==1:nhv(hg)

hh = Hypergraph{Bool}(7,4)
hh[1,1] = true
hh[2,1:2] .= true
Expand Down Expand Up @@ -476,10 +476,10 @@ end;
h[4,3:4] .= 1
h[5,4] = 1
h[5,2] = 1
@test SimpleHypergraphs.conductance(h, Set(1:3)) == 5 / 5
@test SimpleHypergraphs.conductance(h, Set(1)) == 3 / 1
@test SimpleHypergraphs.conductance(h, Set([1, 4])) == 8 / 3
@test SimpleHypergraphs.conductance(h, Set(2:5)) == 3 / 8
@test SimpleHypergraphs.conductance(h, Set(1:3)) == 2 / 4
@test SimpleHypergraphs.conductance(h, Set(1)) == 1 / 1
@test SimpleHypergraphs.conductance(h, Set([1, 4])) == 3 / 3
@test SimpleHypergraphs.conductance(h, Set(2:5)) == SimpleHypergraphs.conductance(h, Set(1))
h = Hypergraph{Int}(6, 7)
h[1:3, 1] .= 1
h[1:2, 2] .= 1
Expand All @@ -489,8 +489,8 @@ end;
h[4:6, 5] .= 1
h[4:5, 6] .= 1
h[5:6, 7] .= 1
@test SimpleHypergraphs.conductance(h, Set(1:3)) == 2 / 8
@test SimpleHypergraphs.conductance(h, Set([1, 4])) == 14 / 6
@test SimpleHypergraphs.conductance(h, Set(1:3)) == 1 / 8
@test SimpleHypergraphs.conductance(h, Set([1, 4])) == 6 / 6
@test_throws ErrorException SimpleHypergraphs.conductance(h, Set{Int}())
@test_throws ErrorException SimpleHypergraphs.conductance(h, Set(1:nhv(h)))
end;
Expand Down