-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomicClustering.m
More file actions
208 lines (180 loc) · 5.02 KB
/
Copy pathRandomicClustering.m
File metadata and controls
208 lines (180 loc) · 5.02 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function [idx,idxsizes] = RandomicClustering(G,k,type,verbose)
% INPUT
% G is the given graph
% k is the MAXIMUM number of clusters that this algorithm can make
% type = 'random'
% 'inrandom'
% 'outrandom'
% 'neutralrandom'
% 'polarizedrandom'
% 'purelyrandom'
% 'naive'
% verbose = 1 -> computes 2nd output
% OUPUT
% idx(i) contains the community to which node i belongs
% idxsizes(j) tells the size of cluster j
%% 0A - naive clustering
if strcmp(type,'naive')
n = numnodes(G);
idx = zeros(1,n);
idxsizes = zeros(1,k);
ratio = floor(n/k);
remainder = mod(n,k);
i = 0;
while i < k
i = i + 1;
for j = 1:ratio
idx((i-1)*ratio+j) = i;
end
if verbose
idxsizes(i) = ratio;
end
if i <= remainder
idx(k*ratio+i) = i;
if verbose
idxsizes(i) = idxsizes(i) + 1;
end
end
end
end
%% 0B - purerandom clustering
if strcmp(type,'purelyrandom')
n = numnodes(G);
idx = ones(1,n);
idxsizes = k;
if k == n
idx = 1:n;
if verbose
idxsizes = ones(1,k);
end
elseif 1 < k && k < n
idx = ceil(k*rand(1,n));
idxsizes = clcard(idx,k);
for kk = 1:k
if idxsizes(kk) == 0
[maxsize,kk_maxsize] = max(idxsizes);
direction = floor(2*rand);
i = 1;
if direction
i = n;
end
des_size = floor(maxsize/2);
while idxsizes(kk) < des_size
if idx(i) == kk_maxsize
idx(i) = kk;
idxsizes(kk) = idxsizes(kk) + 1;
idxsizes(kk_maxsize) = idxsizes(kk_maxsize) - 1;
end
if direction
i = i - 1;
else
i = i + 1;
end
end
end
end
end
return
end
%% 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 - random clustering
c_scl = 0; % counter of subclusters
p = 1;
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
dIN = 0;
dOUT = 0;
dd = 0;
if strcmp(type,'inrandom') || strcmp(type,'outrandom') ||...
strcmp(type,'neutralrandom') || strcmp(type,'symmetrizedrandom')
for cc2 = 1:n_cc
dcc2_in = indegree(G,nodes_cc(cc2));
dcc2_out = outdegree(G,nodes_cc(cc2));
dIN = dIN + dcc2_in;
dOUT = dOUT + dcc2_out;
dd = dd + max(dcc2_in,dcc2_out);
end
end
for cc2 = 1:n_cc
if strcmp(type,'inrandom')
dcc2 = indegree(G,nodes_cc(cc2));
p = (1+dcc2)/(1+dIN);
end
if strcmp(type,'outrandom')
dcc2 = outdegree(G,nodes_cc(cc2));
p = (1+dcc2)/(1+dOUT);
end
if strcmp(type,'neutralrandom')
dcc2_in = indegree(G,nodes_cc(cc2));
dcc2_out = outdegree(G,nodes_cc(cc2));
dcc2 = max(dcc2_in,dcc2_out);
p = (dd/(dIN+dOUT)+dcc2)/(dd/(dIN+dOUT)+dd);
end
if strcmp(type,'symmetrizedrandom')
dcc2_in = indegree(G,nodes_cc(cc2));
dcc2_out = outdegree(G,nodes_cc(cc2));
p = (2+dcc2_in+dcc2_out)/(2+dIN+dOUT);
end
if cc2 <= K_cc
idx_cc(cc2) = cc2;
else
if rand < p
idx_cc(cc2) = ceil(rand*K_cc);
else
idx_cc(cc2) = ceil(p*K_cc);
end
end
end
end
for i = 1:n_cc
idx(nodes_cc(i)) = idx_cc(i)+c_scl;
end
c_scl = c_scl + K_cc;
end
%% 2C - completion
if verbose
% check_ks = zeros(1,k);
% for kk = 1:k
% check_ks(idx(kk)) = 1;
% end
%
% jj = Inf;
% true_k = k;
% while jj > 0
% kk = 0;
% jj = 0;
% while kk < true_k && jj == 0
% kk = kk + 1;
% if check_ks(kk) == 0
% check_ks(kk) = 1;
% jj = kk;
% end
% end
% if jj > 0
% true_k = true_k - 1;
% [maxidx,posj] = max(idx);
% check_ks(maxidx) = 0;
% for j = 1:length(posj)
% idx(posj(j)) = jj;
% end
% end
% end
% idx = idx - min(idx) + 1;
idxsizes = clcard(idx,k);
end
end