-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectralClustering.m
More file actions
189 lines (149 loc) · 4.73 KB
/
Copy pathSpectralClustering.m
File metadata and controls
189 lines (149 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
function [idx,idxsizes] = SpectralClustering(G,k,type,verbose)
% INPUT
% G is the given graph
% k is the MAXIMUM number of clusters that this algorithm can make
% type = 'neutral'
% 'neutralsquared'
% 'symmetrized'
% 'symmetrizedsquared'
% 'inward'
% 'outward'
% 'inoutward'
% 'outinward'
% 'strong'
% OUPUT
% idx(i) contains the community to which node i belongs
% idxsizes(j) tells the size of cluster j
%% 1 - computes weakly connected components
[bins,binsizes,NCC,idx,idxsizes] = initclustering(G,verbose);
%% 2A - Component clustering, if k == NCC
if k == NCC
return
end
%% 2B - Component clustering, if k < NCC
if k < NCC
[idx,idxsizes] = underclustering(k,bins,binsizes,idx,verbose);
return
end
%% 2C - Component clustering, if k > NCC
[K,bins,binsizes] = subcldims(k,bins,binsizes);
%% 2C_a - various types of spectral clustering (not strong)
if strcmp(type,'neutral') || strcmp(type,'symmetrized') || ...
strcmp(type,'inward') || strcmp(type,'outward') ||...
strcmp(type,'neutralsquared') || strcmp(type,'symmetrizedsquared')...
|| strcmp(type,'inoutward') || strcmp(type,'outinward')
L = full(adjacency(G,'weighted'));
if strcmp(type,'neutral')
L = undLap(L);
elseif strcmp(type,'neutralsquared')
L = undLap(L);
L = L*L;
elseif strcmp(type,'symmetrized')
L = diag(sum(L+L'))-(L+L');
elseif strcmp(type,'symmetrizedsquared')
L = diag(sum(L+L'))-(L+L');
L = L'*L;
elseif strcmp(type,'inward')
L = diag(sum(L,1))-L;
L = L'*L;
elseif strcmp(type,'outward')
L = diag(sum(L,2))-L;
L = L'*L;
elseif strcmp(type,'inoutward')
LI = diag(sum(L,1))-L;
LO = diag(sum(L,2))-L;
L = LI'*(LO'*LO)*LI;
elseif strcmp(type,'outinward')
LI = diag(sum(L,1))-L;
LO = diag(sum(L,2))-L;
L = LO'*(LI'*LI)*LO;
end
c_scl = 0; % counter of subclusters
for cc = 1:NCC
K_cc = K(cc);
[nodes_cc,n_cc] = nodesinacc(bins,binsizes,cc);
idx_cc = ones(n_cc,1);
if K_cc > 1
s = -1;
if strcmp(type,'neutral') || strcmp(type,'neutralsquared')...
|| strcmp(type,'symmetrized') ||...
strcmp(type,'symmetrizedsquared')
s = 1;
elseif strcmp(type,'inward') || strcmp(type,'outward') ||...
strcmp(type,'inoutward') || strcmp(type,'outinward')
s = 0;
end
idx_cc = spectralkmeans(K_cc,squaresel(L,nodes_cc),s);
end
for i = 1:n_cc
idx(nodes_cc(i)) = idx_cc(i)+c_scl;
end
c_scl = c_scl + K_cc;
end
end
%% 2C_b - clustering based on strongly connected components
if strcmp(type,'strong')
A = adjacency(G);
c_scl = 0;
for cc = 1:NCC
K_cc = K(cc);
[nodes_cc,n_cc] = nodesinacc(bins,binsizes,cc);
Acc = sparse(squaresel(A,nodes_cc));
[bins_cc,binsizes_cc] = conncomp(digraph(Acc),'Type','strong');
max_bs = max(binsizes_cc);
for i = 1:n_cc
b_cc_i = bins_cc(i);
bs_cc_i = binsizes_cc(b_cc_i);
for j = 1:n_cc
b_cc_j = bins_cc(j);
bs_cc_j = binsizes_cc(b_cc_j);
if i ~= j && b_cc_i ~= b_cc_j && Acc(i,j) > 0
Acc(i,j) = ((bs_cc_i+bs_cc_j)/2-1)/max_bs;
end
end
end
idx_cc = SpectralClustering(digraph(Acc),K_cc,...
'symmetrized',verbose);
for i = 1:n_cc
idx(nodes_cc(i)) = idx_cc(i)+c_scl;
end
c_scl = c_scl + K_cc;
end
end
%% 2C - completion
if verbose
idxsizes = clcard(idx,k);
end
end
%% SUBFUNCTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% spectral clustering + kmeans executed on the Laplacian L_cc
function idx_cc = spectralkmeans(K_cc,L_cc,s)
[V,D] = eig(L_cc);
[~,ind] = sort(diag(D),'ComparisonMethod','abs');
V = V(:,ind);
idx_cc = kmeans(V(:,1+s:K_cc),K_cc,'start',zeros(K_cc,K_cc-s));
end
%% retrieves undirected Laplacian from a directed adjacency matrix
function L = undLap(A)
n = length(A(1,:));
L = zeros(n,n);
for i = 1:n
for j = 1:n
if i ~= j && A(i,j) > 0
if A(j,i) == 0
L(i,j) = A(i,j);
L(j,i) = L(i,j);
else
av = mean([A(i,j) A(j,i)]);
L(i,j) = av;
L(j,i) = av;
end
end
end
end
L = diag(sum(L))-L;
end