72static double get_mq(
SparseMatrix A,
int *assignment,
int *ncluster0,
double *mq_in0,
double *mq_out0,
double **dout0){
81 const size_t n =
A->m;
82 bool test_pattern_symmetry_only =
false;
83 int *counts, *ia =
A->ia, *ja =
A->ja, k, j, jj;
84 double mq_in = 0, mq_out = 0, *a =
NULL, Vi, Vj;
90 (void)test_pattern_symmetry_only;
91 assert((
size_t)
A->n == n);
96 for (
size_t i = 0; i < n; i++){
97 assert(assignment[i] >= 0 && (
size_t)assignment[i] < n);
98 if (counts[assignment[i]] == 0) ncluster++;
99 counts[assignment[i]]++;
102 assert(ncluster <= (
int)n);
104 for (
size_t i = 0; i < n; i++){
105 assert(assignment[i] < ncluster);
108 for (j = ia[i] ; j < ia[i+1]; j++){
111 if (jj >= (
int)i)
continue;
112 assert(assignment[jj] < ncluster);
113 Vj = counts[assignment[jj]];
114 if (assignment[jj] == c){
116 mq_in += a[j]/(Vi*Vi);
122 mq_out += a[j]/(Vi*Vj);
124 mq_out += 1./(Vi*Vj);
133 for (
size_t i = 0; i < n; i++){
134 for (j = ia[i]; j < ia[i+1]; j++){
136 if (jj == (
int)i)
continue;
138 dout[i] += a[j]/(double) counts[assignment[jj]];
140 dout[i] += 1./(double) counts[assignment[jj]];
152 return 2*(mq_in/k - mq_out/(k*(k-1)));
232 int n =
grid->
n, level =
grid->level, nc = 0, nclusters = n;
233 double mq = 0, mq_in = 0, mq_out = 0, mq_new, mq_in_new, mq_out_new, mq_max = 0, mq_in_max = 0, mq_out_max = 0;
234 int *ia =
A->ia, *ja =
A->ja;
236 double *deg_intra =
grid->deg_intra, *wgt =
grid->wgt;
237 int i, j, k, jj, jc, jmax;
238 double gain = 0, *dout =
grid->dout, deg_in_i, deg_in_j, wgt_i, wgt_j, a_ij, dout_i, dout_j, dout_max = 0, wgt_jmax = 0;
240 double total_gain = 0;
246 mq_out =
grid->mq_out;
248 double *deg_intra_new =
gv_calloc(n,
sizeof(
double));
249 double *wgt_new =
gv_calloc(n,
sizeof(
double));
250 double *deg_inter =
gv_calloc(n,
sizeof(
double));
252 double *dout_new =
gv_calloc(n,
sizeof(
double));
253 for (i = 0; i < n; i++) mask[i] = -1;
256 for (i = 0; i < n; i++) matching[i] =
UNMATCHED;
283 for (i = 0; i < n; i++){
286 for (j = ia[i]; j < ia[i+1]; j++){
288 if (jj == i)
continue;
292 deg_inter[jc] = a[j];
294 deg_inter[jc] += a[j];
298 deg_in_i = deg_intra[i];
304 for (j = ia[i]; j < ia[i+1]; j++){
306 if (jj == i)
continue;
311 deg_in_j = deg_intra[jj];
313 }
else if (deg_inter[jc] < 0){
316 a_ij = deg_inter[jc];
319 deg_in_j = deg_intra_new[jc];
320 dout_j = dout_new[jc];
323 mq_in_new = mq_in - deg_in_i/pow(wgt_i, 2) - deg_in_j/pow(wgt_j,2)
324 + (deg_in_i + deg_in_j + a_ij)/pow(wgt_i + wgt_j,2);
326 mq_out_new = mq_out - dout_i/wgt_i - dout_j/wgt_j + (dout_i + dout_j)/(wgt_i + wgt_j);
329 mq_new = 2*(mq_in_new/(nclusters - 1) - mq_out_new/((nclusters - 1)*(nclusters - 2)));
331 mq_new = 2*mq_in_new/(nclusters - 1);
336 double mq2, mq_in2, mq_out2, *dout2;
338 int *matching2 =
gv_calloc(
A->m,
sizeof(
int));
339 memcpy(matching2, matching,
sizeof(
double)*
A->m);
347 for (k = 0; k < n; k++)
if (matching2[k] ==
UNMATCHED) matching2[k] =nc2++;
348 mq2 =
get_mq(
A, matching2, &ncluster, &mq_in2, &mq_out2, &dout2);
349 fprintf(stderr,
" {dout_i, dout_j}={%f,%f}, {predicted, calculated}: mq = {%f, %f}, mq_in ={%f,%f}, mq_out = {%f,%f}\n",dout_i, dout_j, mq_new, mq2, mq_in_new, mq_in2, mq_out_new, mq_out2);
357 if (
Verbose) fprintf(stderr,
"gain in merging node %d with node %d = %f-%f = %f\n", i, jj, mq, mq_new, gain);
358 if (j == ia[i] || gain > maxgain){
365 mq_in_max = mq_in_new;
366 mq_out_max = mq_out_new;
372 if (maxgain > 0 || (nc >= 1 && nc > maxcluster)){
373 total_gain += maxgain;
376 fprintf(stderr,
"maxgain=%f, merge %d, %d\n",maxgain, i, jmax);
379 dout_new[nc] = dout_i + dout_max;
380 matching[i] = matching[jmax] = nc;
381 wgt_new[nc] = wgt[i] + wgt[jmax];
382 deg_intra_new[nc] = deg_intra[i] + deg_intra[jmax] + amax;
385 fprintf(stderr,
"maxgain=%f, merge with existing cluster %d, %d\n",maxgain, i, jc);
387 dout_new[jc] = dout_i + dout_max;
388 wgt_new[jc] += wgt[i];
390 deg_intra_new[jc] += deg_intra[i] + amax;
397 fprintf(stderr,
"gain: %f -- no gain, skip merging node %d\n", maxgain, i);
398 assert(maxgain <= 0);
401 deg_intra_new[nc] = deg_intra[i];
402 wgt_new[nc] = wgt[i];
410 mask[
LIST_GET(&neighbors[jc], l)] = n + i;
415 for (j = ia[k]; j < ia[k+1]; j++){
417 if (mask[jj] == n+i)
continue;
420 dout[jj] += -a[j]/wgt_i + a[j]/(wgt_i + wgt_jmax);
422 dout[jj] += -a[j]/wgt_jmax + a[j]/(wgt_i + wgt_jmax);
426 dout_new[jc] += -a[j]/wgt_i + a[j]/(wgt_i + wgt_jmax);
428 dout_new[jc] += -a[j]/wgt_jmax + a[j]/(wgt_i + wgt_jmax);
436 fprintf(stderr,
"verbose=%d\n",
Verbose);
437 if (
Verbose) fprintf(stderr,
"mq = %f new mq = %f level = %d, n = %d, nc = %d, gain = %g, mq_in = %f, mq_out = %f\n", mq, mq + total_gain,
438 level, n, nc, total_gain, mq_in, mq_out);
443 mq =
get_mq(
A, matching, &ncluster, &mq_in, &mq_out, &dout);
444 fprintf(stderr,
" mq = %f\n",mq);
449 if (nc >= 1 && (total_gain > 0 || nc < n)){
456 for (i = 0; i < n; i++){
482 deg_intra_new =
gv_recalloc(deg_intra_new, n, nc,
sizeof(
double));
483 wgt_new =
gv_recalloc(wgt_new, n, nc,
sizeof(
double));
485 cgrid->
mq =
grid->mq + total_gain;
486 cgrid->
wgt = wgt_new;
487 dout_new =
gv_recalloc(dout_new, n, nc,
sizeof(
double));
488 cgrid->
dout = dout_new;
496 for (i = 0; i < n; i++) matching[i] = i;
504 for (i = 0; i < n; i++)
LIST_FREE(&neighbors[i]);