11using CommunityDetection
22using LightGraphs
3- using Base. Test
3+ using LinearAlgebra: I, norm
4+ using ArnoldiMethod: LR
5+ using SparseArrays: sparse
6+ using Test
47
58""" Spectral embedding of the non-backtracking matrix of `g`
69(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.
1215"""
1316function nonbacktrack_embedding_dense (g:: AbstractGraph , k:: Int )
1417 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))
1720 # TODO decide what to do with the stationary distribution ϕ[:,1]
1821 # this code just throws it away in favor of eigv[:,2:k+1].
1922 # 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)
3033 return ϕ
3134end
3235
36+ @testset " CommunityDetection" begin
37+
3338n = 10 ; k = 5
3439pg = PathGraph (n)
3540ϕ1 = CommunityDetection. nonbacktrack_embedding (pg, k)'
@@ -46,48 +51,72 @@ z = B * x
4651@test norm (y- z) < 1e-8
4752
4853# 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
5055
5156# check that we can use the implicit matvec in nonbacktrack_embedding
5257@test size (y) == size (x)
5358ϕ2 = nonbacktrack_embedding_dense (pg, k)'
5459@test size (ϕ2) == size (ϕ1)
5560
5661# 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
7177 end
7278 end
79+ end
7380
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
8197 end
98+
8299 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]))
83114
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
91118 end
92119 end
93120end
121+
122+ end
0 commit comments