Skip to content

Commit 2760c27

Browse files
Use GenericGraph for testing centrality algorithms (#272)
1 parent 745add6 commit 2760c27

File tree

11 files changed

+42
-42
lines changed

11 files changed

+42
-42
lines changed

src/centrality/betweenness.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function betweenness_centrality(
8181
)
8282
return betweenness_centrality(
8383
g,
84-
sample(vertices(g), k; rng=rng, seed=seed),
84+
sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed),
8585
distmx;
8686
normalize=normalize,
8787
endpoints=endpoints,
@@ -124,8 +124,7 @@ function _accumulate_endpoints!(
124124
v1 = collect(Base.OneTo(n_v))
125125
v2 = state.dists
126126
S = reverse(state.closest_vertices)
127-
s = vertices(g)[si]
128-
betweenness[s] += length(S) - 1 # 289
127+
betweenness[si] += length(S) - 1 # 289
129128

130129
for w in S
131130
coeff = (1.0 + δ[w]) / σ[w]

src/centrality/stress.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function stress_centrality(
5151
rng::Union{Nothing,AbstractRNG}=nothing,
5252
seed::Union{Nothing,Integer}=nothing,
5353
)
54-
return stress_centrality(g, sample(vertices(g), k; rng=rng, seed=seed))
54+
return stress_centrality(g, sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed))
5555
end
5656

5757
function _stress_accumulate_basic!(

src/core.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ julia> add_vertices!(g, 2)
4444
"""
4545
add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n])
4646

47+
# TODO the behaviour of indegree (and as well outdegree and degree) is very
48+
# badly documented for the case indegree(g, vs) where vs is not a single vertex
49+
# but rather a collection of vertices
50+
4751
"""
4852
indegree(g[, v])
4953
@@ -68,7 +72,7 @@ julia> indegree(g)
6872
```
6973
"""
7074
indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v))
71-
indegree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [indegree(g, x) for x in v]
75+
indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs]
7276

7377
"""
7478
outdegree(g[, v])
@@ -94,7 +98,7 @@ julia> outdegree(g)
9498
```
9599
"""
96100
outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v))
97-
outdegree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [outdegree(g, x) for x in v]
101+
outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs]
98102

99103
"""
100104
degree(g[, v])
@@ -122,10 +126,16 @@ julia> degree(g)
122126
```
123127
"""
124128
function degree end
125-
@traitfn degree(g::::IsDirected, v::Integer) = indegree(g, v) + outdegree(g, v)
126-
@traitfn degree(g::::(!IsDirected), v::Integer) = indegree(g, v)
127129

128-
degree(g::AbstractGraph, v::AbstractVector=vertices(g)) = [degree(g, x) for x in v]
130+
function degree(g::AbstractGraph, v::Integer)
131+
if !is_directed(g)
132+
return outdegree(g, v)
133+
end
134+
return indegree(g, v) + outdegree(g, v)
135+
end
136+
137+
degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs]
138+
129139

130140
"""
131141
Δout(g)

test/centrality/betweenness.jl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
@testset "Betweenness" begin
22
rng = StableRNG(1)
33
# self loops
4-
s2 = SimpleDiGraph(3)
5-
add_edge!(s2, 1, 2)
6-
add_edge!(s2, 2, 3)
7-
add_edge!(s2, 3, 3)
8-
s1 = SimpleGraph(s2)
9-
g3 = path_graph(5)
4+
s1 = GenericGraph(SimpleGraph(Edge.([(1,2), (2,3), (3, 3)])))
5+
s2 = GenericDiGraph(SimpleDiGraph(Edge.([(1,2), (2,3), (3, 3)])))
6+
7+
g3 = GenericGraph(path_graph(5))
108

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

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

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

32-
g = SimpleGraph(2)
33-
add_edge!(g, 1, 2)
30+
g = GenericGraph(path_graph(2))
3431
z = @inferred(betweenness_centrality(g; normalize=true))
3532
@test z[1] == z[2] == 0.0
3633
z2 = @inferred(betweenness_centrality(g, vertices(g)))
37-
z3 = @inferred(betweenness_centrality(g, [vertices(g);]))
34+
z3 = @inferred(betweenness_centrality(g, collect(vertices(g))))
3835
@test z == z2 == z3
3936

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

4340
# Weighted Graph tests
44-
g = Graph(6)
45-
add_edge!(g, 1, 2)
46-
add_edge!(g, 2, 3)
47-
add_edge!(g, 3, 4)
48-
add_edge!(g, 2, 5)
49-
add_edge!(g, 5, 6)
50-
add_edge!(g, 5, 4)
41+
g = GenericGraph(SimpleGraph(Edge.([(1, 2), (2, 3), (2, 5), (3, 4), (4, 5), (5, 6)])))
5142

5243
distmx = [
5344
0.0 2.0 0.0 0.0 0.0 0.0
@@ -77,7 +68,7 @@
7768

7869
adjmx2 = [0 1 0; 1 0 1; 1 1 0] # digraph
7970
a2 = SimpleDiGraph(adjmx2)
80-
for g in testdigraphs(a2)
71+
for g in test_generic_graphs(a2)
8172
distmx2 = [Inf 2.0 Inf; 3.2 Inf 4.2; 5.5 6.1 Inf]
8273
c2 = [0.24390243902439027, 0.27027027027027023, 0.1724137931034483]
8374
@test isapprox(
@@ -99,7 +90,7 @@
9990
)
10091
end
10192
# test #1405 / #1406
102-
g = grid([50, 50])
93+
g = GenericGraph(grid([50, 50]))
10394
z = betweenness_centrality(g; normalize=false)
10495
@test maximum(z) < nv(g) * (nv(g) - 1)
10596
end

test/centrality/closeness.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
add_edge!(g5, 1, 3)
66
add_edge!(g5, 3, 4)
77

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

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

2626
g5 = SimpleGraph(5)
2727
add_edge!(g5, 1, 2)
28-
for g in testgraphs(g5)
28+
for g in test_generic_graphs(g5)
2929
z = @inferred(closeness_centrality(g))
3030
@test z[1] == z[2] == 0.25
3131
@test z[3] == z[4] == z[5] == 0.0
3232
end
3333

3434
adjmx1 = [0 1 0; 1 0 1; 0 1 0] # graph
3535
a1 = SimpleGraph(adjmx1)
36-
for g in testgraphs(a1)
36+
for g in test_generic_graphs(a1)
3737
distmx1 = [Inf 2.0 Inf; 2.0 Inf 4.2; Inf 4.2 Inf]
3838
c1 = [0.24390243902439027, 0.3225806451612903, 0.1923076923076923]
3939
y = @inferred(closeness_centrality(g, distmx1; normalize=false))

test/centrality/degree.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
add_edge!(g5, 2, 3)
55
add_edge!(g5, 1, 3)
66
add_edge!(g5, 3, 4)
7-
for g in testdigraphs(g5)
7+
for g in test_generic_graphs(g5)
88
@test @inferred(degree_centrality(g)) ==
99
[0.6666666666666666, 0.6666666666666666, 1.0, 0.3333333333333333]
1010
@test @inferred(indegree_centrality(g, normalize=false)) == [0.0, 1.0, 2.0, 1.0]

test/centrality/eigenvector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
g1 = smallgraph(:house)
33
g2 = cycle_digraph(4)
44

5-
for g in testgraphs(g1)
5+
for g in test_generic_graphs(g1)
66
y = @inferred(eigenvector_centrality(g))
77
@test round.(y, digits=3) ==
88
round.(
@@ -16,7 +16,7 @@
1616
digits=3,
1717
)
1818
end
19-
for g in testdigraphs(g2)
19+
for g in test_generic_graphs(g2)
2020
y = @inferred(eigenvector_centrality(g))
2121
@test round.(y, digits=3) == round.([0.5, 0.5, 0.5, 0.5], digits=3)
2222
end

test/centrality/katz.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
add_edge!(g5, 2, 3)
55
add_edge!(g5, 1, 3)
66
add_edge!(g5, 3, 4)
7-
for g in testdigraphs(g5)
7+
for g in test_generic_graphs(g5)
88
z = @inferred(katz_centrality(g, 0.4))
99
@test round.(z, digits=2) == [0.32, 0.44, 0.62, 0.56]
1010
end

test/centrality/pagerank.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
add_edge!(g6, 1, 3)
2424
add_edge!(g6, 3, 4)
2525
for α in [0.75, 0.85]
26-
for g in testdigraphs(g5)
26+
for g in test_generic_graphs(g5)
2727
@test pagerank(g)[3] 0.318 atol = 0.001
2828
@test length(@inferred(pagerank(g))) == nv(g)
2929
@test_throws ErrorException pagerank(g, 2)
3030
@test_throws ErrorException pagerank(g, α, 2)
3131
@test isapprox(pagerank(g, α), dense_pagerank_solver(g, α), atol=0.001)
3232
end
3333

34-
for g in testgraphs(g6)
34+
for g in test_generic_graphs(g6)
3535
@test length(@inferred(pagerank(g))) == nv(g)
3636
@test_throws ErrorException pagerank(g, 2)
3737
@test_throws ErrorException pagerank(g, α, 2)

test/centrality/radiality.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
gint = loadgraph(joinpath(testdir, "testdata", "graph-50-500.jgz"), "graph-50-500")
33

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

1010
g1 = cycle_graph(4)
1111
add_vertex!(g1)
1212
add_edge!(g1, 4, 5)
13-
for g in testgraphs(g1)
13+
for g in test_generic_graphs(g1)
1414
z = @inferred(radiality_centrality(g))
1515
@test z [5//6, 3//4, 5//6, 11//12, 2//3]
1616
end

0 commit comments

Comments
 (0)