Skip to content

Commit

Permalink
Use GenericGraph for testing centrality algorithms (JuliaGraphs#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschoelly authored and jonaswa committed Jul 5, 2023
1 parent b8d09d3 commit 50b48c7
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 42 deletions.
5 changes: 2 additions & 3 deletions src/centrality/betweenness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function betweenness_centrality(
)
return betweenness_centrality(
g,
sample(vertices(g), k; rng=rng, seed=seed),
sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed),
distmx;
normalize=normalize,
endpoints=endpoints,
Expand Down Expand Up @@ -124,8 +124,7 @@ function _accumulate_endpoints!(
v1 = collect(Base.OneTo(n_v))
v2 = state.dists
S = reverse(state.closest_vertices)
s = vertices(g)[si]
betweenness[s] += length(S) - 1 # 289
betweenness[si] += length(S) - 1 # 289

for w in S
coeff = (1.0 + δ[w]) / σ[w]
Expand Down
2 changes: 1 addition & 1 deletion src/centrality/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function stress_centrality(
rng::Union{Nothing,AbstractRNG}=nothing,
seed::Union{Nothing,Integer}=nothing,
)
return stress_centrality(g, sample(vertices(g), k; rng=rng, seed=seed))
return stress_centrality(g, sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed))
end

function _stress_accumulate_basic!(
Expand Down
20 changes: 15 additions & 5 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ julia> add_vertices!(g, 2)
"""
add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n])

# TODO the behaviour of indegree (and as well outdegree and degree) is very
# badly documented for the case indegree(g, vs) where vs is not a single vertex
# but rather a collection of vertices

"""
indegree(g[, v])
Expand All @@ -68,7 +72,7 @@ julia> indegree(g)
```
"""
indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v))
indegree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [indegree(g, x) for x in v]
indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs]

"""
outdegree(g[, v])
Expand All @@ -94,7 +98,7 @@ julia> outdegree(g)
```
"""
outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v))
outdegree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [outdegree(g, x) for x in v]
outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs]

"""
degree(g[, v])
Expand Down Expand Up @@ -122,10 +126,16 @@ julia> degree(g)
```
"""
function degree end
@traitfn degree(g::::IsDirected, v::Integer) = indegree(g, v) + outdegree(g, v)
@traitfn degree(g::::(!IsDirected), v::Integer) = indegree(g, v)

degree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [degree(g, x) for x in v]
function degree(g::AbstractGraph, v::Integer)
if !is_directed(g)
return outdegree(g, v)
end
return indegree(g, v) + outdegree(g, v)
end

degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs]


"""
Δout(g)
Expand Down
29 changes: 10 additions & 19 deletions test/centrality/betweenness.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
@testset "Betweenness" begin
rng = StableRNG(1)
# self loops
s2 = SimpleDiGraph(3)
add_edge!(s2, 1, 2)
add_edge!(s2, 2, 3)
add_edge!(s2, 3, 3)
s1 = SimpleGraph(s2)
g3 = path_graph(5)
s1 = GenericGraph(SimpleGraph(Edge.([(1,2), (2,3), (3, 3)])))
s2 = GenericDiGraph(SimpleDiGraph(Edge.([(1,2), (2,3), (3, 3)])))

g3 = GenericGraph(path_graph(5))

gint = loadgraph(joinpath(testdir, "testdata", "graph-50-500.jgz"), "graph-50-500")

c = vec(readdlm(joinpath(testdir, "testdata", "graph-50-500-bc.txt"), ','))
for g in testdigraphs(gint)
for g in test_generic_graphs(gint)
z = @inferred(betweenness_centrality(g))
@test map(Float32, z) == map(Float32, c)

Expand All @@ -29,25 +27,18 @@
@test @inferred(betweenness_centrality(s1)) == [0, 1, 0]
@test @inferred(betweenness_centrality(s2)) == [0, 0.5, 0]

g = SimpleGraph(2)
add_edge!(g, 1, 2)
g = GenericGraph(path_graph(2))
z = @inferred(betweenness_centrality(g; normalize=true))
@test z[1] == z[2] == 0.0
z2 = @inferred(betweenness_centrality(g, vertices(g)))
z3 = @inferred(betweenness_centrality(g, [vertices(g);]))
z3 = @inferred(betweenness_centrality(g, collect(vertices(g))))
@test z == z2 == z3

z = @inferred(betweenness_centrality(g3; normalize=false))
@test z[1] == z[5] == 0.0

# Weighted Graph tests
g = Graph(6)
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 4)
add_edge!(g, 2, 5)
add_edge!(g, 5, 6)
add_edge!(g, 5, 4)
g = GenericGraph(SimpleGraph(Edge.([(1, 2), (2, 3), (2, 5), (3, 4), (4, 5), (5, 6)])))

distmx = [
0.0 2.0 0.0 0.0 0.0 0.0
Expand Down Expand Up @@ -77,7 +68,7 @@

adjmx2 = [0 1 0; 1 0 1; 1 1 0] # digraph
a2 = SimpleDiGraph(adjmx2)
for g in testdigraphs(a2)
for g in test_generic_graphs(a2)
distmx2 = [Inf 2.0 Inf; 3.2 Inf 4.2; 5.5 6.1 Inf]
c2 = [0.24390243902439027, 0.27027027027027023, 0.1724137931034483]
@test isapprox(
Expand All @@ -99,7 +90,7 @@
)
end
# test #1405 / #1406
g = grid([50, 50])
g = GenericGraph(grid([50, 50]))
z = betweenness_centrality(g; normalize=false)
@test maximum(z) < nv(g) * (nv(g) - 1)
end
8 changes: 4 additions & 4 deletions test/centrality/closeness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
add_edge!(g5, 1, 3)
add_edge!(g5, 3, 4)

for g in testdigraphs(g5)
for g in test_generic_graphs(g5)
y = @inferred(closeness_centrality(g; normalize=false))
z = @inferred(closeness_centrality(g))
@test y == [0.75, 0.6666666666666666, 1.0, 0.0]
Expand All @@ -14,7 +14,7 @@

adjmx2 = [0 1 0; 1 0 1; 1 1 0] # digraph
a2 = SimpleDiGraph(adjmx2)
for g in testdigraphs(a2)
for g in test_generic_graphs(a2)
distmx2 = [Inf 2.0 Inf; 3.2 Inf 4.2; 5.5 6.1 Inf]
c2 = [0.24390243902439027, 0.27027027027027023, 0.1724137931034483]
y = @inferred(closeness_centrality(g, distmx2; normalize=false))
Expand All @@ -25,15 +25,15 @@

g5 = SimpleGraph(5)
add_edge!(g5, 1, 2)
for g in testgraphs(g5)
for g in test_generic_graphs(g5)
z = @inferred(closeness_centrality(g))
@test z[1] == z[2] == 0.25
@test z[3] == z[4] == z[5] == 0.0
end

adjmx1 = [0 1 0; 1 0 1; 0 1 0] # graph
a1 = SimpleGraph(adjmx1)
for g in testgraphs(a1)
for g in test_generic_graphs(a1)
distmx1 = [Inf 2.0 Inf; 2.0 Inf 4.2; Inf 4.2 Inf]
c1 = [0.24390243902439027, 0.3225806451612903, 0.1923076923076923]
y = @inferred(closeness_centrality(g, distmx1; normalize=false))
Expand Down
2 changes: 1 addition & 1 deletion test/centrality/degree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
add_edge!(g5, 2, 3)
add_edge!(g5, 1, 3)
add_edge!(g5, 3, 4)
for g in testdigraphs(g5)
for g in test_generic_graphs(g5)
@test @inferred(degree_centrality(g)) ==
[0.6666666666666666, 0.6666666666666666, 1.0, 0.3333333333333333]
@test @inferred(indegree_centrality(g, normalize=false)) == [0.0, 1.0, 2.0, 1.0]
Expand Down
4 changes: 2 additions & 2 deletions test/centrality/eigenvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
g1 = smallgraph(:house)
g2 = cycle_digraph(4)

for g in testgraphs(g1)
for g in test_generic_graphs(g1)
y = @inferred(eigenvector_centrality(g))
@test round.(y, digits=3) ==
round.(
Expand All @@ -16,7 +16,7 @@
digits=3,
)
end
for g in testdigraphs(g2)
for g in test_generic_graphs(g2)
y = @inferred(eigenvector_centrality(g))
@test round.(y, digits=3) == round.([0.5, 0.5, 0.5, 0.5], digits=3)
end
Expand Down
2 changes: 1 addition & 1 deletion test/centrality/katz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
add_edge!(g5, 2, 3)
add_edge!(g5, 1, 3)
add_edge!(g5, 3, 4)
for g in testdigraphs(g5)
for g in test_generic_graphs(g5)
z = @inferred(katz_centrality(g, 0.4))
@test round.(z, digits=2) == [0.32, 0.44, 0.62, 0.56]
end
Expand Down
4 changes: 2 additions & 2 deletions test/centrality/pagerank.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
add_edge!(g6, 1, 3)
add_edge!(g6, 3, 4)
for α in [0.75, 0.85]
for g in testdigraphs(g5)
for g in test_generic_graphs(g5)
@test pagerank(g)[3] 0.318 atol = 0.001
@test length(@inferred(pagerank(g))) == nv(g)
@test_throws ErrorException pagerank(g, 2)
@test_throws ErrorException pagerank(g, α, 2)
@test isapprox(pagerank(g, α), dense_pagerank_solver(g, α), atol=0.001)
end

for g in testgraphs(g6)
for g in test_generic_graphs(g6)
@test length(@inferred(pagerank(g))) == nv(g)
@test_throws ErrorException pagerank(g, 2)
@test_throws ErrorException pagerank(g, α, 2)
Expand Down
4 changes: 2 additions & 2 deletions test/centrality/radiality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
gint = loadgraph(joinpath(testdir, "testdata", "graph-50-500.jgz"), "graph-50-500")

c = vec(readdlm(joinpath(testdir, "testdata", "graph-50-500-rc.txt"), ','))
for g in testdigraphs(gint)
for g in test_generic_graphs(gint)
z = @inferred(radiality_centrality(g))
@test z == c
end

g1 = cycle_graph(4)
add_vertex!(g1)
add_edge!(g1, 4, 5)
for g in testgraphs(g1)
for g in test_generic_graphs(g1)
z = @inferred(radiality_centrality(g))
@test z [5//6, 3//4, 5//6, 11//12, 2//3]
end
Expand Down
4 changes: 2 additions & 2 deletions test/centrality/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
gint = loadgraph(joinpath(testdir, "testdata", "graph-50-500.jgz"), "graph-50-500")

c = vec(readdlm(joinpath(testdir, "testdata", "graph-50-500-sc.txt"), ','))
for g in testdigraphs(gint)
for g in test_generic_graphs(gint)
z = @inferred(stress_centrality(g))
@test z == c

Expand All @@ -16,7 +16,7 @@
g1 = cycle_graph(4)
add_vertex!(g1)
add_edge!(g1, 4, 5)
for g in testgraphs(g1)
for g in test_generic_graphs(g1)
z = @inferred(stress_centrality(g))
@test z == [4, 2, 4, 10, 0]
end
Expand Down

0 comments on commit 50b48c7

Please sign in to comment.