45 int *ia = A->
ia, *ja = A->
ja, n = A->
n;
55 for (i = 0; i < n; i++){
58 for (j = ia[i]; j < ia[i+1]; j++){
60 if (ja[j] == i) indeg[i] = a[j];
64 if (deg_total == 0) deg_total = 1;
65 for (i = 0; i < n; i++){
66 modularity += (indeg[i] - deg[i]*deg[i]/deg_total)/deg_total;
81 if (grid->
level == 0) {
92 Multilevel_Modularity_Clustering_delete(grid->
next);
99 int n = grid->
n, level = grid->
level, nc = 0;
101 int *ia = A->
ia, *ja = A->
ja;
105 int i, j, jj, jc, jmax;
107 real *deg_inter, gain;
115 mask =
MALLOC(
sizeof(
int)*n);
116 for (i = 0; i < n; i++) mask[i] = -1;
119 for (i = 0; i < n; i++) matching[i] =
UNMATCHED;
125 for (i = 0; i < n; i++){
128 for (j = ia[i]; j < ia[i+1]; j++){
130 if (jj == i)
continue;
134 deg_inter[jc] = a[j];
136 deg_inter[jc] += a[j];
143 for (j = ia[i]; j < ia[i+1]; j++){
145 if (jj == i)
continue;
148 gain = (2*a[j] - 2*deg[i]*deg[jj]*inv_deg_total)*inv_deg_total;
150 if (deg_inter[jc] > 0){
152 gain = (2*deg_inter[jc] - 2*deg[i]*deg_new[jc]*inv_deg_total)*inv_deg_total;
159 if (jmax < 0 || gain > maxgain){
168 total_gain += maxgain;
172 matching[i] = matching[jmax] = nc;
173 deg_new[nc] = deg[i] + deg[jmax];
177 deg_new[jc] += deg[i];
183 deg_new[nc] = deg[i];
189 if (
Verbose) fprintf(stderr,
"modularity = %f new modularity = %f level = %d, n = %d, nc = %d, gain = %g\n", modularity, modularity + total_gain,
190 level, n, nc, total_gain);
193 if (ncluster_target > 0){
194 if (nc <= ncluster_target && n >= ncluster_target){
195 if (n - ncluster_target > ncluster_target - nc){
197 }
else if (n - ncluster_target <= ncluster_target - nc){
198 fprintf(stderr,
"ncluster_target = %d, close to n=%d\n", ncluster_target, n);
199 for (i = 0; i < n; i++) matching[i] = i;
203 }
else if (n < ncluster_target){
204 fprintf(stderr,
"n < target\n");
205 for (i = 0; i < n; i++) matching[i] = i;
211 if (nc >= 1 && (total_gain > 0 || nc < n)){
218 for (i = 0; i < n; i++){
228 if (!cA)
goto RETURN;
233 cgrid = Multilevel_Modularity_Clustering_init(cA, level);
235 cgrid->
deg = deg_new;
238 cgrid = Multilevel_Modularity_Clustering_establish(cgrid, ncluster_target);
248 return Multilevel_Modularity_Clustering_establish(grid, ncluster_target);
251 for (i = 0; i < n; i++) matching[i] = i;
276 grid = Multilevel_Modularity_Clustering_init(A, 0);
278 grid = Multilevel_Modularity_Clustering_establish(grid, ncluster_target);
285 static void hierachical_modularity_clustering(
SparseMatrix A,
int ncluster_target,
286 int *nclusters,
int **assignment,
real *modularity,
int *flag){
312 grid = Multilevel_Modularity_Clustering_new(A, ncluster_target);
322 for (i = 0; i < cgrid->
n; i++) u[i] = (
real) (cgrid->
matching)[i];
323 *nclusters = cgrid->
n;
336 matching = *assignment;
338 matching =
MALLOC(
sizeof(
int)*(grid->
n));
339 *assignment = matching;
341 for (i = 0; i < grid->
n; i++) (matching)[i] = (
int) u[i];
344 Multilevel_Modularity_Clustering_delete(grid);
351 int *nclusters,
int **assignment,
real *modularity,
int *flag){
373 if (!inplace && B == A) {
381 hierachical_modularity_clustering(B, ncluster_target, nclusters, assignment, modularity, flag);
Multilevel_Modularity_Clustering next
int agglomerate_regardless
void modularity_clustering(SparseMatrix A, int inplace, int ncluster_target, int use_value, int *nclusters, int **assignment, real *modularity, int *flag)
SparseMatrix SparseMatrix_set_entries_to_real_one(SparseMatrix A)
SparseMatrix SparseMatrix_copy(SparseMatrix A)
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
SparseMatrix SparseMatrix_remove_diagonal(SparseMatrix A)
int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only)
void SparseMatrix_multiply_vector(SparseMatrix A, real *v, real **res, int transposed)
SparseMatrix SparseMatrix_get_real_adjacency_matrix_symmetrized(SparseMatrix A)
Multilevel_Modularity_Clustering prev
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, int pattern_symmetric_only)
void SparseMatrix_delete(SparseMatrix A)
EXTERN unsigned char Verbose
SparseMatrix SparseMatrix_multiply(SparseMatrix A, SparseMatrix B)
SparseMatrix SparseMatrix_coordinate_form_add_entries(SparseMatrix A, int nentries, int *irn, int *jcn, void *val)
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A)
SparseMatrix SparseMatrix_transpose(SparseMatrix A)