1
1
using CommunityDetection
2
2
using LightGraphs
3
- using Base. Test
3
+ using LinearAlgebra: I, norm
4
+ using ArnoldiMethod: LR
5
+ using SparseArrays: sparse
6
+ using Test
4
7
5
8
""" Spectral embedding of the non-backtracking matrix of `g`
6
9
(see [Krzakala et al.](http://www.pnas.org/content/110/52/20935.short)).
@@ -12,8 +15,8 @@ return : a matrix ϕ where ϕ[:,i] are the coordinates for vertex i.
12
15
"""
13
16
function nonbacktrack_embedding_dense (g:: AbstractGraph , k:: Int )
14
17
B, edgeid = non_backtracking_matrix (g)
15
- λ,eigv,conv = eigs (B, nev= k+ 1 , v0 = ones (Float64, size (B, 1 ) ))
16
- ϕ = zeros (Complex64 , k- 1 , nv (g))
18
+ λ, eigv = LightGraphs . LinAlg . eigs (B, nev= k+ 1 , which = LR ( ))
19
+ ϕ = zeros (ComplexF32 , k- 1 , nv (g))
17
20
# TODO decide what to do with the stationary distribution ϕ[:,1]
18
21
# this code just throws it away in favor of eigv[:,2:k+1].
19
22
# we might also use the degree distribution to scale these vectors as is
@@ -30,6 +33,8 @@ function nonbacktrack_embedding_dense(g::AbstractGraph, k::Int)
30
33
return ϕ
31
34
end
32
35
36
+ @testset " CommunityDetection" begin
37
+
33
38
n = 10 ; k = 5
34
39
pg = PathGraph (n)
35
40
ϕ1 = CommunityDetection. nonbacktrack_embedding (pg, k)'
@@ -46,48 +51,72 @@ z = B * x
46
51
@test norm (y- z) < 1e-8
47
52
48
53
# check that matmat works and full(nbt) == B
49
- @test norm (nbt* eye ( nbt. m) - B) < 1e-8
54
+ @test norm (nbt* Matrix {Float64} (I, nbt . m, nbt. m) - B) < 1e-8
50
55
51
56
# check that we can use the implicit matvec in nonbacktrack_embedding
52
57
@test size (y) == size (x)
53
58
ϕ2 = nonbacktrack_embedding_dense (pg, k)'
54
59
@test size (ϕ2) == size (ϕ1)
55
60
56
61
# check that this recovers communities in the path of cliques
57
- n= 10
58
- g10 = CompleteGraph (n)
59
- z = copy (g10)
60
- for k= 2 : 5
61
- z = blkdiag (z, g10)
62
- add_edge! (z, (k- 1 )* n, k* n)
63
-
64
- c = community_detection_nback (z, k)
65
- @test sort (union (c)) == [1 : k;]
66
- a = collect (n: n: k* n)
67
- @test length (c[a]) == length (unique (c[a]))
68
- for i= 1 : k
69
- for j= (i- 1 )* n+ 1 : i* n
70
- @test c[j] == c[i* n]
62
+ @testset " community_detection_nback(z, k)" begin
63
+ n= 10
64
+ g10 = CompleteGraph (n)
65
+ z = copy (g10)
66
+ for k= 2 : 5
67
+ z = blockdiag (z, g10)
68
+ add_edge! (z, (k- 1 )* n, k* n)
69
+
70
+ c = community_detection_nback (z, k)
71
+ @test sort (union (c)) == [1 : k;]
72
+ a = collect (n: n: k* n)
73
+ @test length (c[a]) == length (unique (c[a]))
74
+ for i= 1 : k
75
+ cluster_range = (1 : n) .+ (i- 1 )* n
76
+ @test length (unique (c[cluster_range])) == 1
71
77
end
72
78
end
79
+ end
73
80
74
- c = community_detection_bethe (z, k)
75
- @test sort (union (c)) == [1 : k;]
76
- a = collect (n: n: k* n)
77
- @test length (c[a]) == length (unique (c[a]))
78
- for i= 1 : k
79
- for j= (i- 1 )* n+ 1 : i* n
80
- @test c[j] == c[i* n]
81
+ @testset " community_detection_bethe(z, k)" begin
82
+ n= 10
83
+ g10 = CompleteGraph (n)
84
+ z = copy (g10)
85
+ for k= 2 : 5
86
+ z = blockdiag (z, g10)
87
+ add_edge! (z, (k- 1 )* n, k* n)
88
+
89
+ c = community_detection_bethe (z, k)
90
+ @test sort (union (c)) == [1 : k;]
91
+ a = collect (n: n: k* n)
92
+ @test length (c[a]) == length (unique (c[a]))
93
+
94
+ for i= 1 : k
95
+ cluster_range = (1 : n) .+ (i- 1 )* n
96
+ @test length (unique (c[cluster_range])) == 1
81
97
end
98
+
82
99
end
100
+ end
101
+
102
+ @testset " community_detection_bethe(z)" begin
103
+ n= 10
104
+ g10 = CompleteGraph (n)
105
+ z = copy (g10)
106
+ for k= 2 : 5
107
+ z = blockdiag (z, g10)
108
+ add_edge! (z, (k- 1 )* n, k* n)
109
+
110
+ c = community_detection_bethe (z)
111
+ @test sort (union (c)) == [1 : k;]
112
+ a = collect (n: n: k* n)
113
+ @test length (c[a]) == length (unique (c[a]))
83
114
84
- c = community_detection_bethe (z)
85
- @test sort (union (c)) == [1 : k;]
86
- a = collect (n: n: k* n)
87
- @test length (c[a]) == length (unique (c[a]))
88
- for i= 1 : k
89
- for j= (i- 1 )* n+ 1 : i* n
90
- @test c[j] == c[i* n]
115
+ for i= 1 : k
116
+ cluster_range = (1 : n) .+ (i- 1 )* n
117
+ @test length (unique (c[cluster_range])) == 1
91
118
end
92
119
end
93
120
end
121
+
122
+ end
0 commit comments