| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
/* Modularity Quality definition: |
| 12 |
|
| 13 |
We assume undirected graph. Directed graph should be converted by summing edge weights. |
| 14 |
|
| 15 |
Given a partition P of V into k clusters. |
| 16 |
|
| 17 |
Let E(i,j) be the set of edges between cluster i and j. |
| 18 |
Let |E(i,j)| be the sum of edge weights of edges in E(i,j). |
| 19 |
|
| 20 |
Let E(i,i) be the set of edges within cluster i, but excluding self-edges. |
| 21 |
Let |E(i,i)| be the sum of edge weights of edges in E(i,i). |
| 22 |
|
| 23 |
Let V(i) be the sets of vertices in i |
| 24 |
|
| 25 |
The intra-cluster edges concentration for a cluster i is |
| 26 |
(the denominator could be |V(i)|*(|V(i)-1)/2 strictly speaking as we exclude self-edges): |
| 27 |
|
| 28 |
|E(i,i)| |
| 29 |
----------- |
| 30 |
(|V(i)|^2/2) |
| 31 |
|
| 32 |
The inter-cluster edges concentration between cluster i and j is |
| 33 |
|
| 34 |
|E(i,j)| |
| 35 |
------------ |
| 36 |
|V(i)|*|V(j)| |
| 37 |
|
| 38 |
So the cluster index is defined as the average intra cluster edge concentration, minus |
| 39 |
the inter-cluster edge concentration: |
| 40 |
|
| 41 |
. |E(i,i)| |E(i,j)| |
| 42 |
MQ(P) = (1/k) * \sum_{i=1...k} ------------ - (1/(k*(k-1)/2)) * \sum_{i<j} ------------------- = mq_in/k - mq_out/(k*(k-1)/2) |
| 43 |
. (|V(i)|^2/2) |V(i)|*|V(j)| |
| 44 |
|
| 45 |
or |
| 46 |
|
| 47 |
. |E(i,i)| |E(i,j)| |
| 48 |
MQ(P)/2 = (1/k) * \sum_{i=1...k} ------------ - (1/(k*(k-1))) * \sum_{i<j} ------------------ = mq_in/k - mq_out/(k*(k-1)) |
| 49 |
. |V(i)|^2 |V(i)|*|V(j)| |
| 50 |
|
| 51 |
Notice that if we assume the graph is unweights (edge weights = 1), then 0<= MQ <= 1. |
| 52 |
For weighted graph, MQ may not be within 0 to 1. We could normalized it, but |
| 53 |
for comparing clustering quality of the same graph but different partitioning, this |
| 54 |
unnormalized quantity is not a problem. |
| 55 |
|
| 56 |
*/ |
| 57 |
|
| 58 |
#include "config.h" |
| 59 |
|
| 60 |
#define STANDALONE |
| 61 |
#include <limits.h> |
| 62 |
#include <sparse/general.h> |
| 63 |
#include <sparse/SparseMatrix.h> |
| 64 |
#include <sparse/mq.h> |
| 65 |
#include <stdbool.h> |
| 66 |
#include <string.h> |
| 67 |
#include <util/alloc.h> |
| 68 |
#include <util/list.h> |
| 69 |
|
| 70 |
static double get_mq(SparseMatrix A, int *assignment, int *ncluster0, double *mq_in0, double *mq_out0, double **dout0){ |
| 71 |
/* given a symmetric matrix representation of a graph and an assignment of nodes into clusters, calculate the modularity quality. |
| 72 |
assignment: assignment[i] gives the cluster assignment of node i. 0 <= assignment[i] < ncluster. |
| 73 |
ncluster: number of clusters |
| 74 |
mq_in: the part of MQ to do with intra-cluster edges, before divide by 1/k |
| 75 |
mq_out: the part of MQ to do with inter-cluster edges, before divide by 1/(k*(k-1)) |
| 76 |
mq = 2*(mq_in/k - mq_out/(k*(k-1))); |
| 77 |
*/ |
| 78 |
int ncluster = 0; |
| 79 |
int n = A->m; |
| 80 |
bool test_pattern_symmetry_only = false; |
| 81 |
int *counts, *ia = A->ia, *ja = A->ja, k, i, j, jj; |
| 82 |
double mq_in = 0, mq_out = 0, *a = NULL, Vi, Vj; |
| 83 |
int c; |
| 84 |
double *dout; |
| 85 |
|
| 86 |
|
| 87 |
assert(SparseMatrix_is_symmetric(A, test_pattern_symmetry_only)); |
| 88 |
(void)test_pattern_symmetry_only; |
| 89 |
assert(A->n == n); |
| 90 |
if (A->type == MATRIX_TYPE_REAL) a = A->a; |
| 91 |
|
| 92 |
counts = gv_calloc(n, sizeof(int)); |
| 93 |
|
| 94 |
for (i = 0; i < n; i++){ |
| 95 |
assert(assignment[i] >= 0 && assignment[i] < n); |
| 96 |
if (counts[assignment[i]] == 0) ncluster++; |
| 97 |
counts[assignment[i]]++; |
| 98 |
} |
| 99 |
k = ncluster; |
| 100 |
assert(ncluster <= n); |
| 101 |
|
| 102 |
for (i = 0; i < n; i++){ |
| 103 |
assert(assignment[i] < ncluster); |
| 104 |
c = assignment[i]; |
| 105 |
Vi = counts[c]; |
| 106 |
for (j = ia[i] ; j < ia[i+1]; j++){ |
| 107 |
/* ASSUME UNDIRECTED */ |
| 108 |
jj = ja[j]; |
| 109 |
if (jj >= i) continue; |
| 110 |
assert(assignment[jj] < ncluster); |
| 111 |
Vj = counts[assignment[jj]]; |
| 112 |
if (assignment[jj] == c){ |
| 113 |
if (a) { |
| 114 |
mq_in += a[j]/(Vi*Vi); |
| 115 |
} else { |
| 116 |
mq_in += 1./(Vi*Vi); |
| 117 |
} |
| 118 |
} else { |
| 119 |
if (a) { |
| 120 |
mq_out += a[j]/(Vi*Vj); |
| 121 |
} else { |
| 122 |
mq_out += 1./(Vi*Vj); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* calculate scaled out degree */ |
| 130 |
dout = gv_calloc(n, sizeof(double)); |
| 131 |
for (i = 0; i < n; i++){ |
| 132 |
for (j = ia[i]; j < ia[i+1]; j++){ |
| 133 |
jj = ja[j]; |
| 134 |
if (jj == i) continue; |
| 135 |
if (a){ |
| 136 |
dout[i] += a[j]/(double) counts[assignment[jj]]; |
| 137 |
} else { |
| 138 |
dout[i] += 1./(double) counts[assignment[jj]]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
*ncluster0 = k; |
| 144 |
*mq_in0 = mq_in; |
| 145 |
*mq_out0 = mq_out; |
| 146 |
*dout0 = dout; |
| 147 |
free(counts); |
| 148 |
|
| 149 |
if (k > 1){ |
| 150 |
return 2*(mq_in/k - mq_out/(k*(k-1))); |
| 151 |
} else { |
| 152 |
return 2*mq_in; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static Multilevel_MQ_Clustering Multilevel_MQ_Clustering_init(SparseMatrix A, int level){ |
| 157 |
Multilevel_MQ_Clustering grid; |
| 158 |
int n = A->n, i; |
| 159 |
int *matching; |
| 160 |
|
| 161 |
assert(A->type == MATRIX_TYPE_REAL); |
| 162 |
assert(SparseMatrix_is_symmetric(A, false)); |
| 163 |
|
| 164 |
if (!A) return NULL; |
| 165 |
assert(A->m == n); |
| 166 |
grid = gv_alloc(sizeof(struct Multilevel_MQ_Clustering_struct)); |
| 167 |
grid->level = level; |
| 168 |
grid->n = n; |
| 169 |
grid->A = A; |
| 170 |
grid->P = NULL; |
| 171 |
grid->next = NULL; |
| 172 |
grid->prev = NULL; |
| 173 |
grid->delete_top_level_A = false; |
| 174 |
matching = grid->matching = gv_calloc(n, sizeof(double)); |
| 175 |
grid->deg_intra = NULL; |
| 176 |
grid->dout = NULL; |
| 177 |
grid->wgt = NULL; |
| 178 |
|
| 179 |
if (level == 0){ |
| 180 |
double mq = 0, mq_in, mq_out; |
| 181 |
int ncluster; |
| 182 |
double *deg_intra, *wgt, *dout; |
| 183 |
|
| 184 |
grid->deg_intra = gv_calloc(n, sizeof(double)); |
| 185 |
deg_intra = grid->deg_intra; |
| 186 |
|
| 187 |
grid->wgt = gv_calloc(n, sizeof(double)); |
| 188 |
wgt = grid->wgt; |
| 189 |
|
| 190 |
for (i = 0; i < n; i++){ |
| 191 |
deg_intra[i] = 0; |
| 192 |
wgt[i] = 1.; |
| 193 |
} |
| 194 |
for (i = 0; i < n; i++) matching[i] = i; |
| 195 |
mq = get_mq(A, matching, &ncluster, &mq_in, &mq_out, &dout); |
| 196 |
fprintf(stderr,"ncluster = %d, mq = %f\n", ncluster, mq); |
| 197 |
grid->mq = mq; |
| 198 |
grid->mq_in = mq_in; |
| 199 |
grid->mq_out = mq_out; |
| 200 |
grid->dout = dout; |
| 201 |
grid->ncluster = ncluster; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
return grid; |
| 207 |
} |
| 208 |
|
| 209 |
static void Multilevel_MQ_Clustering_delete(Multilevel_MQ_Clustering grid){ |
| 210 |
if (!grid) return; |
| 211 |
if (grid->A){ |
| 212 |
if (grid->level == 0) { |
| 213 |
if (grid->delete_top_level_A) SparseMatrix_delete(grid->A); |
| 214 |
} else { |
| 215 |
SparseMatrix_delete(grid->A); |
| 216 |
} |
| 217 |
} |
| 218 |
SparseMatrix_delete(grid->P); |
| 219 |
free(grid->matching); |
| 220 |
free(grid->deg_intra); |
| 221 |
free(grid->dout); |
| 222 |
free(grid->wgt); |
| 223 |
Multilevel_MQ_Clustering_delete(grid->next); |
| 224 |
free(grid); |
| 225 |
} |
| 226 |
|
| 227 |
static Multilevel_MQ_Clustering Multilevel_MQ_Clustering_establish(Multilevel_MQ_Clustering grid, int maxcluster){ |
| 228 |
int *matching = grid->matching; |
| 229 |
SparseMatrix A = grid->A; |
| 230 |
int n = grid->n, level = grid->level, nc = 0, nclusters = n; |
| 231 |
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; |
| 232 |
int *ia = A->ia, *ja = A->ja; |
| 233 |
double amax = 0; |
| 234 |
double *deg_intra = grid->deg_intra, *wgt = grid->wgt; |
| 235 |
int i, j, k, jj, jc, jmax; |
| 236 |
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; |
| 237 |
double maxgain = 0; |
| 238 |
double total_gain = 0; |
| 239 |
|
| 240 |
LIST(int) *neighbors = gv_calloc(n, sizeof(neighbors[0])); |
| 241 |
|
| 242 |
mq = grid->mq; |
| 243 |
mq_in = grid->mq_in; |
| 244 |
mq_out = grid->mq_out; |
| 245 |
|
| 246 |
double *deg_intra_new = gv_calloc(n, sizeof(double)); |
| 247 |
double *wgt_new = gv_calloc(n, sizeof(double)); |
| 248 |
double *deg_inter = gv_calloc(n, sizeof(double)); |
| 249 |
int *mask = gv_calloc(n, sizeof(int)); |
| 250 |
double *dout_new = gv_calloc(n, sizeof(double)); |
| 251 |
for (i = 0; i < n; i++) mask[i] = -1; |
| 252 |
|
| 253 |
assert(n == A->n); |
| 254 |
for (i = 0; i < n; i++) matching[i] = UNMATCHED; |
| 255 |
|
| 256 |
/* gain in merging node A into cluster B is |
| 257 |
mq_in_new = mq_in - |E(A,A)|/(V(A))^2 - |E(B,B)|/(V(B))^2 + (|E(A,A)|+|E(B,B)|+|E(A,B)|)/(|V(A)|+|V(B)|)^2 |
| 258 |
. = mq_in - deg_intra(A)/|A|^2 - deg_intra(B)/|B|^2 + (deg_intra(A)+deg_intra(B)+a(A,B))/(|A|+|B|)^2 |
| 259 |
|
| 260 |
mq_out_new = mq_out - |E(A,B)|/(|V(A)|*V(B)|)-\sum_{C and A connected, C!=B} |E(A,C)|/(|V(A)|*|V(C)|)-\sum_{C and B connected,C!=B} |E(B,C)|/(|V(B)|*|V(C)|) |
| 261 |
. + \sum_{C connected to A or B, C!=A, C!=B} (|E(A,C)|+|E(B,C)|)/(|V(C)|*(|V(A)|+|V(B)|) |
| 262 |
. = mq_out + a(A,B)/(|A|*|B|)-\sum_{C and A connected} a(A,C)/(|A|*|C|)-\sum_{C and B connected} a(B,C)/(|B|*|C|) |
| 263 |
. + \sum_{C connected to A or B, C!=A, C!=B} (a(A,C)+a(B,C))/(|C|*(|A|+|B|)) |
| 264 |
Denote: |
| 265 |
dout(i) = \sum_{j -- i} a(i,j)/|j| |
| 266 |
then |
| 267 |
|
| 268 |
mq_out_new = mq_out - |E(A,B)|/(|V(A)|*V(B)|)-\sum_{C and A connected, C!=B} |E(A,C)|/(|V(A)|*|V(C)|)-\sum_{C and B connected,C!=B} |E(B,C)|/(|V(B)|*|V(C)|) |
| 269 |
. + \sum_{C connected to A or B, C!=A, C!=B} (|E(A,C)|+|E(B,C)|)/(|V(C)|*(|V(A)|+|V(B)|) |
| 270 |
. = mq_out + a(A,B)/(|A|*|B|)-dout(A)/|A| - dout(B)/|B| |
| 271 |
. + (dout(A)+dout(B))/(|A|+|B|) - (a(A,B)/|A|+a(A,B)/|B|)/(|A|+|B|) |
| 272 |
. = mq_out -dout(A)/|A| - dout(B)/|B| + (dout(A)+dout(B))/(|A|+|B|) |
| 273 |
after merging A and B into cluster AB, |
| 274 |
dout(AB) = dout(A) + dout(B); |
| 275 |
dout(C) := dout(C) - a(A,C)/|A| - a(B,C)/|B| + a(A,C)/(|A|+|B|) + a(B, C)/(|A|+|B|) |
| 276 |
|
| 277 |
mq_new = mq_in_new/(k-1) - mq_out_new/((k-1)*(k-2)) |
| 278 |
gain = mq_new - mq |
| 279 |
*/ |
| 280 |
double *a = A->a; |
| 281 |
for (i = 0; i < n; i++){ |
| 282 |
if (matching[i] != UNMATCHED) continue; |
| 283 |
/* accumulate connections between i and clusters */ |
| 284 |
for (j = ia[i]; j < ia[i+1]; j++){ |
| 285 |
jj = ja[j]; |
| 286 |
if (jj == i) continue; |
| 287 |
if ((jc=matching[jj]) != UNMATCHED){ |
| 288 |
if (mask[jc] != i) { |
| 289 |
mask[jc] = i; |
| 290 |
deg_inter[jc] = a[j]; |
| 291 |
} else { |
| 292 |
deg_inter[jc] += a[j]; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
deg_in_i = deg_intra[i]; |
| 297 |
wgt_i = wgt[i]; |
| 298 |
dout_i = dout[i]; |
| 299 |
|
| 300 |
maxgain = 0; |
| 301 |
jmax = -1; |
| 302 |
for (j = ia[i]; j < ia[i+1]; j++){ |
| 303 |
jj = ja[j]; |
| 304 |
if (jj == i) continue; |
| 305 |
jc = matching[jj]; |
| 306 |
if (jc == UNMATCHED){ |
| 307 |
a_ij = a[j]; |
| 308 |
wgt_j = wgt[jj]; |
| 309 |
deg_in_j = deg_intra[jj]; |
| 310 |
dout_j = dout[jj]; |
| 311 |
} else if (deg_inter[jc] < 0){ |
| 312 |
continue; |
| 313 |
} else { |
| 314 |
a_ij = deg_inter[jc]; |
| 315 |
wgt_j = wgt_new[jc]; |
| 316 |
deg_inter[jc] = -1; // so that we do not redo the calculation when we hit another neighbor in cluster jc |
| 317 |
deg_in_j = deg_intra_new[jc]; |
| 318 |
dout_j = dout_new[jc]; |
| 319 |
} |
| 320 |
|
| 321 |
mq_in_new = mq_in - deg_in_i/pow(wgt_i, 2) - deg_in_j/pow(wgt_j,2) |
| 322 |
+ (deg_in_i + deg_in_j + a_ij)/pow(wgt_i + wgt_j,2); |
| 323 |
|
| 324 |
mq_out_new = mq_out - dout_i/wgt_i - dout_j/wgt_j + (dout_i + dout_j)/(wgt_i + wgt_j); |
| 325 |
|
| 326 |
if (nclusters > 2){ |
| 327 |
mq_new = 2*(mq_in_new/(nclusters - 1) - mq_out_new/((nclusters - 1)*(nclusters - 2))); |
| 328 |
} else { |
| 329 |
mq_new = 2*mq_in_new/(nclusters - 1); |
| 330 |
} |
| 331 |
|
| 332 |
#ifdef DEBUG |
| 333 |
{int ncluster; |
| 334 |
double mq2, mq_in2, mq_out2, *dout2; |
| 335 |
int nc2 = nc; |
| 336 |
int *matching2 = gv_calloc(A->m, sizeof(int)); |
| 337 |
memcpy(matching2, matching, sizeof(double)*A->m); |
| 338 |
if (jc != UNMATCHED) { |
| 339 |
matching2[i] = jc; |
| 340 |
} else { |
| 341 |
matching2[i] = nc2; |
| 342 |
matching2[jj] = nc2; |
| 343 |
nc2++; |
| 344 |
} |
| 345 |
for (k = 0; k < n; k++) if (matching2[k] == UNMATCHED) matching2[k] =nc2++; |
| 346 |
mq2 = get_mq(A, matching2, &ncluster, &mq_in2, &mq_out2, &dout2); |
| 347 |
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); |
| 348 |
|
| 349 |
mq_new = mq2; |
| 350 |
|
| 351 |
} |
| 352 |
#endif |
| 353 |
|
| 354 |
gain = mq_new - mq; |
| 355 |
if (Verbose) fprintf(stderr,"gain in merging node %d with node %d = %f-%f = %f\n", i, jj, mq, mq_new, gain); |
| 356 |
if (j == ia[i] || gain > maxgain){ |
| 357 |
maxgain = gain; |
| 358 |
jmax = jj; |
| 359 |
amax = a_ij; |
| 360 |
dout_max = dout_j; |
| 361 |
wgt_jmax = wgt_j; |
| 362 |
mq_max = mq_new; |
| 363 |
mq_in_max = mq_in_new; |
| 364 |
mq_out_max = mq_out_new; |
| 365 |
} |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
/* now merge i and jmax */ |
| 370 |
if (maxgain > 0 || (nc >= 1 && nc > maxcluster)){ |
| 371 |
total_gain += maxgain; |
| 372 |
jc = matching[jmax]; |
| 373 |
if (jc == UNMATCHED){ |
| 374 |
fprintf(stderr, "maxgain=%f, merge %d, %d\n",maxgain, i, jmax); |
| 375 |
LIST_APPEND(&neighbors[nc], jmax); |
| 376 |
LIST_APPEND(&neighbors[nc], i); |
| 377 |
dout_new[nc] = dout_i + dout_max; |
| 378 |
matching[i] = matching[jmax] = nc; |
| 379 |
wgt_new[nc] = wgt[i] + wgt[jmax]; |
| 380 |
deg_intra_new[nc] = deg_intra[i] + deg_intra[jmax] + amax; |
| 381 |
nc++; |
| 382 |
} else { |
| 383 |
fprintf(stderr,"maxgain=%f, merge with existing cluster %d, %d\n",maxgain, i, jc); |
| 384 |
LIST_APPEND(&neighbors[jc], i); |
| 385 |
dout_new[jc] = dout_i + dout_max; |
| 386 |
wgt_new[jc] += wgt[i]; |
| 387 |
matching[i] = jc; |
| 388 |
deg_intra_new[jc] += deg_intra[i] + amax; |
| 389 |
} |
| 390 |
mq = mq_max; |
| 391 |
mq_in = mq_in_max; |
| 392 |
mq_out = mq_out_max; |
| 393 |
nclusters--; |
| 394 |
} else { |
| 395 |
fprintf(stderr,"gain: %f -- no gain, skip merging node %d\n", maxgain, i); |
| 396 |
assert(maxgain <= 0); |
| 397 |
LIST_APPEND(&neighbors[nc], i); |
| 398 |
matching[i] = nc; |
| 399 |
deg_intra_new[nc] = deg_intra[i]; |
| 400 |
wgt_new[nc] = wgt[i]; |
| 401 |
nc++; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
/* update scaled outdegree of neighbors of i and its merged node/cluster jmax */ |
| 406 |
jc = matching[i]; |
| 407 |
for (size_t l = LIST_SIZE(&neighbors[jc]) - 1; l != SIZE_MAX; --l) { |
| 408 |
mask[LIST_GET(&neighbors[jc], l)] = n + i; |
| 409 |
} |
| 410 |
|
| 411 |
for (size_t l = LIST_SIZE(&neighbors[jc]) - 1; l != SIZE_MAX; --l) { |
| 412 |
k = LIST_GET(&neighbors[jc], l); |
| 413 |
for (j = ia[k]; j < ia[k+1]; j++){ |
| 414 |
jj = ja[j]; |
| 415 |
if (mask[jj] == n+i) continue;/* link to within cluster */ |
| 416 |
if ((jc = matching[jj]) == UNMATCHED){ |
| 417 |
if (k == i){ |
| 418 |
dout[jj] += -a[j]/wgt_i + a[j]/(wgt_i + wgt_jmax); |
| 419 |
} else { |
| 420 |
dout[jj] += -a[j]/wgt_jmax + a[j]/(wgt_i + wgt_jmax); |
| 421 |
} |
| 422 |
} else { |
| 423 |
if (k == i){ |
| 424 |
dout_new[jc] += -a[j]/wgt_i + a[j]/(wgt_i + wgt_jmax); |
| 425 |
} else { |
| 426 |
dout_new[jc] += -a[j]/wgt_jmax + a[j]/(wgt_i + wgt_jmax); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
} |
| 433 |
|
| 434 |
fprintf(stderr,"verbose=%d\n",Verbose); |
| 435 |
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, |
| 436 |
level, n, nc, total_gain, mq_in, mq_out); |
| 437 |
|
| 438 |
#ifdef DEBUG |
| 439 |
{int ncluster; |
| 440 |
|
| 441 |
mq = get_mq(A, matching, &ncluster, &mq_in, &mq_out, &dout); |
| 442 |
fprintf(stderr," mq = %f\n",mq); |
| 443 |
|
| 444 |
} |
| 445 |
#endif |
| 446 |
|
| 447 |
if (nc >= 1 && (total_gain > 0 || nc < n)){ |
| 448 |
/* now set up restriction and prolongation operator */ |
| 449 |
SparseMatrix P, R, R0, B, cA; |
| 450 |
double one = 1.; |
| 451 |
Multilevel_MQ_Clustering cgrid; |
| 452 |
|
| 453 |
R0 = SparseMatrix_new(nc, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD); |
| 454 |
for (i = 0; i < n; i++){ |
| 455 |
jj = matching[i]; |
| 456 |
SparseMatrix_coordinate_form_add_entry(R0, jj, i, &one); |
| 457 |
} |
| 458 |
R = SparseMatrix_from_coordinate_format(R0); |
| 459 |
SparseMatrix_delete(R0); |
| 460 |
P = SparseMatrix_transpose(R); |
| 461 |
B = SparseMatrix_multiply(R, A); |
| 462 |
SparseMatrix_delete(R); |
| 463 |
if (!B) { |
| 464 |
free(deg_intra_new); |
| 465 |
free(wgt_new); |
| 466 |
free(dout_new); |
| 467 |
goto RETURN; |
| 468 |
} |
| 469 |
cA = SparseMatrix_multiply(B, P); |
| 470 |
SparseMatrix_delete(B); |
| 471 |
if (!cA) { |
| 472 |
free(deg_intra_new); |
| 473 |
free(wgt_new); |
| 474 |
free(dout_new); |
| 475 |
goto RETURN; |
| 476 |
} |
| 477 |
grid->P = P; |
| 478 |
level++; |
| 479 |
cgrid = Multilevel_MQ_Clustering_init(cA, level); |
| 480 |
deg_intra_new = gv_recalloc(deg_intra_new, n, nc, sizeof(double)); |
| 481 |
wgt_new = gv_recalloc(wgt_new, n, nc, sizeof(double)); |
| 482 |
cgrid->deg_intra = deg_intra_new; |
| 483 |
cgrid->mq = grid->mq + total_gain; |
| 484 |
cgrid->wgt = wgt_new; |
| 485 |
dout_new = gv_recalloc(dout_new, n, nc, sizeof(double)); |
| 486 |
cgrid->dout = dout_new; |
| 487 |
|
| 488 |
cgrid = Multilevel_MQ_Clustering_establish(cgrid, maxcluster); |
| 489 |
|
| 490 |
grid->next = cgrid; |
| 491 |
cgrid->prev = grid; |
| 492 |
} else { |
| 493 |
/* no more improvement, stop and final clustering found */ |
| 494 |
for (i = 0; i < n; i++) matching[i] = i; |
| 495 |
|
| 496 |
free(deg_intra_new); |
| 497 |
free(wgt_new); |
| 498 |
free(dout_new); |
| 499 |
} |
| 500 |
|
| 501 |
RETURN: |
| 502 |
for (i = 0; i < n; i++) LIST_FREE(&neighbors[i]); |
| 503 |
free(neighbors); |
| 504 |
|
| 505 |
free(deg_inter); |
| 506 |
free(mask); |
| 507 |
return grid; |
| 508 |
} |
| 509 |
|
| 510 |
static Multilevel_MQ_Clustering Multilevel_MQ_Clustering_new(SparseMatrix A0, int maxcluster){ |
| 511 |
/* maxcluster is used to specify the maximum number of cluster desired, e.g., maxcluster=10 means that a maximum of 10 clusters |
| 512 |
is desired. this may not always be realized, and mq may be low when this is specified. Default: maxcluster = 0 */ |
| 513 |
Multilevel_MQ_Clustering grid; |
| 514 |
SparseMatrix A = A0; |
| 515 |
|
| 516 |
if (maxcluster <= 0) maxcluster = A->m; |
| 517 |
if (!SparseMatrix_is_symmetric(A, false) || A->type != MATRIX_TYPE_REAL){ |
| 518 |
A = SparseMatrix_get_real_adjacency_matrix_symmetrized(A); |
| 519 |
} |
| 520 |
grid = Multilevel_MQ_Clustering_init(A, 0); |
| 521 |
|
| 522 |
grid = Multilevel_MQ_Clustering_establish(grid, maxcluster); |
| 523 |
|
| 524 |
if (A != A0) grid->delete_top_level_A = true; // be sure to clean up later |
| 525 |
return grid; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
static void hierachical_mq_clustering(SparseMatrix A, int maxcluster, |
| 530 |
int *nclusters, int **assignment, double *mq){ |
| 531 |
/* find a clustering of vertices by maximize mq |
| 532 |
A: symmetric square matrix n x n. If real value, value will be used as edges weights, otherwise edge weights are considered as 1. |
| 533 |
maxcluster: used to specify the maximum number of cluster desired, e.g., maxcluster=10 means that a maximum of 10 clusters |
| 534 |
. is desired. this may not always be realized, and mq may be low when this is specified. Default: maxcluster = 0 |
| 535 |
nclusters: on output the number of clusters |
| 536 |
assignment: dimension n. Node i is assigned to cluster "assignment[i]". 0 <= assignment < nclusters |
| 537 |
*/ |
| 538 |
|
| 539 |
Multilevel_MQ_Clustering grid, cgrid; |
| 540 |
int *matching, i; |
| 541 |
SparseMatrix P; |
| 542 |
assert(A->m == A->n); |
| 543 |
|
| 544 |
*mq = 0.; |
| 545 |
|
| 546 |
grid = Multilevel_MQ_Clustering_new(A, maxcluster); |
| 547 |
|
| 548 |
/* find coarsest */ |
| 549 |
cgrid = grid; |
| 550 |
while (cgrid->next){ |
| 551 |
cgrid = cgrid->next; |
| 552 |
} |
| 553 |
|
| 554 |
/* project clustering up */ |
| 555 |
double *u = gv_calloc(cgrid->n, sizeof(double)); |
| 556 |
for (i = 0; i < cgrid->n; i++) u[i] = (double) (cgrid->matching)[i]; |
| 557 |
*nclusters = cgrid->n; |
| 558 |
*mq = cgrid->mq; |
| 559 |
|
| 560 |
while (cgrid->prev){ |
| 561 |
double *v = NULL; |
| 562 |
P = cgrid->prev->P; |
| 563 |
SparseMatrix_multiply_vector(P, u, &v); |
| 564 |
free(u); |
| 565 |
u = v; |
| 566 |
cgrid = cgrid->prev; |
| 567 |
} |
| 568 |
|
| 569 |
if (*assignment){ |
| 570 |
matching = *assignment; |
| 571 |
} else { |
| 572 |
matching = gv_calloc(grid->n, sizeof(int)); |
| 573 |
*assignment = matching; |
| 574 |
} |
| 575 |
for (i = 0; i < grid->n; i++) (matching)[i] = (int) u[i]; |
| 576 |
free(u); |
| 577 |
|
| 578 |
Multilevel_MQ_Clustering_delete(grid); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
void mq_clustering(SparseMatrix A, int maxcluster, |
| 584 |
int *nclusters, int **assignment, double *mq){ |
| 585 |
/* find a clustering of vertices by maximize mq |
| 586 |
A: symmetric square matrix n x n. If real value, value will be used as edges weights, otherwise edge weights are considered as 1. |
| 587 |
maxcluster: used to specify the maximum number of cluster desired, e.g., maxcluster=10 means that a maximum of 10 clusters |
| 588 |
. is desired. this may not always be realized, and mq may be low when this is specified. Default: maxcluster = 0 |
| 589 |
nclusters: on output the number of clusters |
| 590 |
assignment: dimension n. Node i is assigned to cluster "assignment[i]". 0 <= assignment < nclusters |
| 591 |
*/ |
| 592 |
SparseMatrix B; |
| 593 |
|
| 594 |
assert(A->m == A->n); |
| 595 |
|
| 596 |
B = SparseMatrix_symmetrize(A, false); |
| 597 |
|
| 598 |
if (B == A) { |
| 599 |
B = SparseMatrix_copy(A); |
| 600 |
} |
| 601 |
|
| 602 |
B = SparseMatrix_remove_diagonal(B); |
| 603 |
|
| 604 |
if (B->type != MATRIX_TYPE_REAL) B = SparseMatrix_set_entries_to_real_one(B); |
| 605 |
|
| 606 |
hierachical_mq_clustering(B, maxcluster, nclusters, assignment, mq); |
| 607 |
|
| 608 |
if (B != A) SparseMatrix_delete(B); |
| 609 |
|
| 610 |
} |