DotIO.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #define STANDALONE
  11. #include <sparse/general.h>
  12. #include <sparse/DotIO.h>
  13. #include <sparse/clustering.h>
  14. #include <math.h>
  15. #include <sparse/mq.h>
  16. #include <sparse/color_palette.h>
  17. #include <sparse/colorutil.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <util/agxbuf.h>
  21. #include <util/alloc.h>
  22. #include <util/startswith.h>
  23. #include <util/unreachable.h>
  24. typedef struct {
  25. Agrec_t h;
  26. unsigned int id;
  27. } Agnodeinfo_t;
  28. #define ND_id(n) (((Agnodeinfo_t*)((n)->base.data))->id)
  29. static void color_string(agxbuf *buf, int dim, double *color){
  30. if (dim > 3 || dim < 1){
  31. fprintf(stderr,"can only 1, 2 or 3 dimensional color space. with color value between 0 to 1\n");
  32. assert(0);
  33. }
  34. if (dim == 3){
  35. agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] *255), 255),
  36. MIN((unsigned int) (color[1]*255), 255), MIN((unsigned int)(color[2]*255), 255));
  37. } else if (dim == 1){
  38. agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
  39. MIN((unsigned int) (color[0]*255), 255), MIN((unsigned int)(color[0]*255), 255));
  40. } else if (dim == 2){
  41. agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
  42. 0, MIN((unsigned int)(color[1]*255), 255));
  43. }
  44. }
  45. void attach_edge_colors(Agraph_t* g, int dim, double *colors){
  46. /* colors is array of dim*nedges, with color for edge i at colors[dim*i, dim(i+1))
  47. */
  48. Agsym_t* sym = agattr(g, AGEDGE, "color", 0);
  49. Agedge_t* e;
  50. Agnode_t* n;
  51. agxbuf buf = {0};
  52. unsigned row, col;
  53. int ie = 0;
  54. if (!sym)
  55. sym = agattr (g, AGEDGE, "color", "");
  56. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  57. row = ND_id(n);
  58. for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
  59. col = ND_id(aghead(e));
  60. if (row == col) continue;
  61. color_string(&buf, dim, colors + ie*dim);
  62. agxset(e, sym, agxbuse(&buf));
  63. ie++;
  64. }
  65. }
  66. agxbfree(&buf);
  67. }
  68. /* SparseMatrix_import_dot:
  69. * Assumes g is connected and simple, i.e., we can have a->b and b->a
  70. * but not a->b and a->b
  71. */
  72. SparseMatrix SparseMatrix_import_dot(Agraph_t *g, int dim,
  73. double **x, int format) {
  74. SparseMatrix A = 0;
  75. Agnode_t* n;
  76. Agedge_t* e;
  77. Agsym_t *sym;
  78. Agsym_t *psym;
  79. int nnodes;
  80. int nedges;
  81. int i, row;
  82. int* I;
  83. int* J;
  84. double *val;
  85. double v;
  86. int type = MATRIX_TYPE_REAL;
  87. if (!g) return NULL;
  88. nnodes = agnnodes (g);
  89. nedges = agnedges (g);
  90. if (format != FORMAT_CSR && format != FORMAT_COORD) {
  91. fprintf (stderr, "Format %d not supported\n", format);
  92. graphviz_exit(1);
  93. }
  94. /* Assign node ids */
  95. i = 0;
  96. for (n = agfstnode (g); n; n = agnxtnode (g, n))
  97. ND_id(n) = i++;
  98. if (format == FORMAT_COORD){
  99. A = SparseMatrix_new(i, i, nedges, MATRIX_TYPE_REAL, format);
  100. A->nz = nedges;
  101. I = A->ia;
  102. J = A->ja;
  103. val = A->a;
  104. } else {
  105. I = gv_calloc(nedges, sizeof(int));
  106. J = gv_calloc(nedges, sizeof(int));
  107. val = gv_calloc(nedges, sizeof(double));
  108. }
  109. sym = agattr(g, AGEDGE, "weight", NULL);
  110. i = 0;
  111. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  112. row = ND_id(n);
  113. for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
  114. I[i] = row;
  115. J[i] = ND_id(aghead(e));
  116. /* edge weight */
  117. if (sym) {
  118. if (sscanf (agxget(e,sym), "%lf", &v) != 1) v = 1;
  119. } else {
  120. v = 1;
  121. }
  122. val[i] = v;
  123. i++;
  124. }
  125. }
  126. if (x && (psym = agattr(g, AGNODE, "pos", NULL))) {
  127. bool has_positions = true;
  128. char* pval;
  129. if (!(*x)) {
  130. *x = gv_calloc(dim * nnodes, sizeof(double));
  131. }
  132. for (n = agfstnode (g); n && has_positions; n = agnxtnode (g, n)) {
  133. double xx,yy, zz,ww;
  134. int nitems;
  135. i = ND_id(n);
  136. if ((pval = agxget(n, psym)) && *pval) {
  137. if (dim == 2){
  138. nitems = sscanf(pval, "%lf,%lf", &xx, &yy);
  139. if (nitems != 2) {
  140. has_positions = false;
  141. agerrorf("Node \"%s\" pos has %d < 2 values", agnameof(n), nitems);
  142. }
  143. (*x)[i*dim] = xx;
  144. (*x)[i*dim+1] = yy;
  145. } else if (dim == 3){
  146. nitems = sscanf(pval, "%lf,%lf,%lf", &xx, &yy, &zz);
  147. if (nitems != 3) {
  148. has_positions = false;
  149. agerrorf("Node \"%s\" pos has %d < 3 values", agnameof(n), nitems);
  150. }
  151. (*x)[i*dim] = xx;
  152. (*x)[i*dim+1] = yy;
  153. (*x)[i*dim+2] = zz;
  154. } else if (dim == 4){
  155. nitems = sscanf(pval, "%lf,%lf,%lf,%lf", &xx, &yy, &zz,&ww);
  156. if (nitems != 4) {
  157. has_positions = false;
  158. agerrorf("Node \"%s\" pos has %d < 4 values", agnameof(n), nitems);
  159. }
  160. (*x)[i*dim] = xx;
  161. (*x)[i*dim+1] = yy;
  162. (*x)[i*dim+2] = zz;
  163. (*x)[i*dim+3] = ww;
  164. } else if (dim == 1){
  165. nitems = sscanf(pval, "%lf", &xx);
  166. if (nitems != 1){
  167. SparseMatrix_delete(A);
  168. A = NULL;
  169. goto done;
  170. }
  171. (*x)[i*dim] = xx;
  172. } else {
  173. assert(0);
  174. }
  175. } else {
  176. has_positions = false;
  177. agerrorf("Node \"%s\" lacks position info", agnameof(n));
  178. }
  179. }
  180. if (!has_positions) {
  181. free(*x);
  182. *x = NULL;
  183. }
  184. }
  185. else if (x)
  186. agerrorf("Error: graph %s has missing \"pos\" information", agnameof(g));
  187. size_t sz = sizeof(double);
  188. if (format == FORMAT_CSR) A = SparseMatrix_from_coordinate_arrays(nedges, nnodes, nnodes, I, J, val, type, sz);
  189. done:
  190. if (format != FORMAT_COORD){
  191. free(I);
  192. free(J);
  193. free(val);
  194. }
  195. return A;
  196. }
  197. /* get spline info */
  198. int Import_dot_splines(Agraph_t* g, int *ne, char ***xsplines){
  199. /* get the list of splines for the edges in the order they appear, and store as a list of strings in xspline.
  200. If *xsplines = NULL, it will be allocated. On exit (*xsplines)[i] is the control point string for the i-th edge. This string
  201. is of the form "x1,y1 x2,y2...", the two end points of the edge is not included per Dot format
  202. Return 1 if success. 0 if not.
  203. */
  204. Agnode_t* n;
  205. Agedge_t* e;
  206. Agsym_t *sym;
  207. int nedges;
  208. unsigned i;
  209. if (!g){
  210. return 0;
  211. }
  212. *ne = nedges = agnedges (g);
  213. /* Assign node ids */
  214. i = 0;
  215. for (n = agfstnode (g); n; n = agnxtnode (g, n))
  216. ND_id(n) = i++;
  217. sym = agattr(g, AGEDGE, "pos", 0);
  218. if (!sym) return 0;
  219. *xsplines = gv_calloc(nedges, sizeof(char*));
  220. i = 0;
  221. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  222. for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
  223. /* edge weight */
  224. char *pos = agxget (e, sym);
  225. (*xsplines)[i] = strdup(pos);
  226. i++;
  227. }
  228. }
  229. return 1;
  230. }
  231. static int hex2int(char h){
  232. if (h >= '0' && h <= '9') return h - '0';
  233. if (h >= 'a' && h <= 'f') return 10 + h - 'a';
  234. if (h >= 'A' && h <= 'F') return 10 + h - 'A';
  235. return 0;
  236. }
  237. static float hexcol2rgb(const char *h) {
  238. return (hex2int(h[0])*16 + hex2int(h[1]))/255.;
  239. }
  240. void Dot_SetClusterColor(Agraph_t* g, float *rgb_r, float *rgb_g, float *rgb_b, int *clusters){
  241. Agnode_t* n;
  242. agxbuf scluster = {0};
  243. unsigned i;
  244. Agsym_t* clust_clr_sym = agattr(g, AGNODE, "clustercolor", NULL);
  245. if (!clust_clr_sym) clust_clr_sym = agattr(g, AGNODE, "clustercolor", "-1");
  246. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  247. i = ND_id(n);
  248. if (rgb_r && rgb_g && rgb_b) {
  249. rgb2hex(rgb_r[clusters[i]], rgb_g[clusters[i]], rgb_b[clusters[i]],
  250. &scluster, NULL);
  251. }
  252. agxset(n, clust_clr_sym, agxbuse(&scluster));
  253. }
  254. agxbfree(&scluster);
  255. }
  256. SparseMatrix Import_coord_clusters_from_dot(Agraph_t* g, int maxcluster, int dim, int *nn, double **label_sizes, double **x, int **clusters, float **rgb_r, float **rgb_g, float **rgb_b, float **fsz, char ***labels, int default_color_scheme, int clustering_scheme, int useClusters){
  257. SparseMatrix A = 0;
  258. Agnode_t* n;
  259. Agedge_t* e;
  260. Agsym_t* sym;
  261. Agsym_t* clust_sym;
  262. Agsym_t* clust_clr_sym;
  263. int nnodes;
  264. int nedges;
  265. int i, row, ic,nc, j;
  266. double v;
  267. int type = MATRIX_TYPE_REAL;
  268. char scluster[100];
  269. float ff;
  270. int MAX_GRPS, MIN_GRPS;
  271. bool noclusterinfo = false;
  272. bool first = true;
  273. const float *pal;
  274. int max_color = MAX_COLOR;
  275. switch (default_color_scheme){
  276. case COLOR_SCHEME_BLUE_YELLOW:
  277. pal = &(palette_blue_to_yellow[0][0]);
  278. break;
  279. case COLOR_SCHEME_WHITE_RED:
  280. pal = &(palette_white_to_red[0][0]);
  281. break;
  282. case COLOR_SCHEME_GREY_RED:
  283. pal = &(palette_grey_to_red[0][0]);
  284. break;
  285. case COLOR_SCHEME_GREY:
  286. pal = &(palette_grey[0][0]);
  287. break;
  288. case COLOR_SCHEME_PASTEL:
  289. pal = &(palette_pastel[0][0]);
  290. break;
  291. case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED:
  292. pal = &(palette_sequential_singlehue_red[0][0]);
  293. break;
  294. case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED_LIGHTER:
  295. pal = &(palette_sequential_singlehue_red_lighter[0][0]);
  296. break;
  297. case COLOR_SCHEME_PRIMARY:
  298. pal = &(palette_primary[0][0]);
  299. break;
  300. case COLOR_SCHEME_ADAM_BLEND:
  301. pal = &(palette_adam_blend[0][0]);
  302. break;
  303. case COLOR_SCHEME_ADAM:
  304. pal = &(palette_adam[0][0]);
  305. max_color = 11;
  306. break;
  307. case COLOR_SCHEME_NONE:
  308. pal = NULL;
  309. break;
  310. default:
  311. pal = &(palette_pastel[0][0]);
  312. break;
  313. }
  314. if (!g) return NULL;
  315. nnodes = agnnodes (g);
  316. nedges = agnedges (g);
  317. *nn = nnodes;
  318. /* Assign node ids */
  319. i = 0;
  320. for (n = agfstnode (g); n; n = agnxtnode (g, n))
  321. ND_id(n) = i++;
  322. /* form matrix */
  323. int* I = gv_calloc(nedges, sizeof(int));
  324. int* J = gv_calloc(nedges, sizeof(int));
  325. double* val = gv_calloc(nedges, sizeof(double));
  326. sym = agattr(g, AGEDGE, "weight", NULL);
  327. clust_sym = agattr(g, AGNODE, "cluster", NULL);
  328. clust_clr_sym = agattr(g, AGNODE, "clustercolor", NULL);
  329. i = 0;
  330. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  331. row = ND_id(n);
  332. for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
  333. I[i] = row;
  334. J[i] = ND_id(aghead(e));
  335. if (sym) {
  336. if (sscanf (agxget(e,sym), "%lf", &v) != 1)
  337. v = 1;
  338. }
  339. else
  340. v = 1;
  341. val[i] = v;
  342. i++;
  343. }
  344. }
  345. A = SparseMatrix_from_coordinate_arrays(nedges, nnodes, nnodes, I, J, val,
  346. type, sizeof(double));
  347. /* get clustering info */
  348. *clusters = gv_calloc(nnodes, sizeof(int));
  349. nc = 1;
  350. MIN_GRPS = 0;
  351. /* if useClusters, the nodes in each top-level cluster subgraph are assigned to
  352. * clusters 2, 3, .... Any nodes not in a cluster subgraph are tossed into cluster 1.
  353. */
  354. if (useClusters) {
  355. Agraph_t* sg;
  356. int gid = 1;
  357. memset (*clusters, 0, sizeof(int)*nnodes);
  358. for (sg = agfstsubg(g); sg; sg = agnxtsubg(sg)) {
  359. if (!startswith(agnameof(sg), "cluster")) continue;
  360. gid++;
  361. for (n = agfstnode(sg); n; n = agnxtnode (sg, n)) {
  362. i = ND_id(n);
  363. if ((*clusters)[i])
  364. fprintf (stderr, "Warning: node %s appears in multiple clusters.\n", agnameof(n));
  365. else
  366. (*clusters)[i] = gid;
  367. }
  368. }
  369. for (n = agfstnode(g); n; n = agnxtnode (g, n)) {
  370. i = ND_id(n);
  371. if ((*clusters)[i] == 0)
  372. (*clusters)[i] = 1;
  373. }
  374. MIN_GRPS = 1;
  375. nc = gid;
  376. }
  377. else if (clust_sym) {
  378. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  379. i = ND_id(n);
  380. if ((sscanf(agxget(n,clust_sym), "%d", &ic)>0)) {
  381. (*clusters)[i] = ic;
  382. nc = MAX(nc, ic);
  383. if (first){
  384. MIN_GRPS = ic;
  385. first = false;
  386. } else {
  387. MIN_GRPS = MIN(MIN_GRPS, ic);
  388. }
  389. } else {
  390. noclusterinfo = true;
  391. break;
  392. }
  393. }
  394. }
  395. else
  396. noclusterinfo = true;
  397. MAX_GRPS = nc;
  398. if (noclusterinfo) {
  399. double modularity;
  400. if (!clust_sym) clust_sym = agattr(g,AGNODE,"cluster","-1");
  401. if (clustering_scheme == CLUSTERING_MQ){
  402. mq_clustering(A, maxcluster,
  403. &nc, clusters, &modularity);
  404. } else if (clustering_scheme == CLUSTERING_MODULARITY){
  405. modularity_clustering(A, false, maxcluster,
  406. &nc, clusters, &modularity);
  407. } else {
  408. UNREACHABLE();
  409. }
  410. for (i = 0; i < nnodes; i++) (*clusters)[i]++;/* make into 1 based */
  411. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  412. i = ND_id(n);
  413. snprintf(scluster, sizeof(scluster), "%d", (*clusters)[i]);
  414. agxset(n,clust_sym,scluster);
  415. }
  416. MIN_GRPS = 1;
  417. MAX_GRPS = nc;
  418. if (Verbose){
  419. fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
  420. }
  421. }
  422. *label_sizes = gv_calloc(dim * nnodes, sizeof(double));
  423. if (pal || (!noclusterinfo && clust_clr_sym)){
  424. *rgb_r = gv_calloc(1 + MAX_GRPS, sizeof(float));
  425. *rgb_g = gv_calloc(1 + MAX_GRPS, sizeof(float));
  426. *rgb_b = gv_calloc(1 + MAX_GRPS, sizeof(float));
  427. } else {
  428. *rgb_r = NULL;
  429. *rgb_g = NULL;
  430. *rgb_b = NULL;
  431. }
  432. *fsz = gv_calloc(nnodes, sizeof(float));
  433. *labels = gv_calloc(nnodes, sizeof(char*));
  434. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  435. gvcolor_t color;
  436. double sz;
  437. i = ND_id(n);
  438. if (agget(n, "width") && agget(n, "height")){
  439. sscanf(agget(n, "width"), "%lf", &sz);
  440. (*label_sizes)[i*2] = POINTS(sz*0.5);
  441. sscanf(agget(n, "height"), "%lf", &sz);
  442. (*label_sizes)[i*2+1] = POINTS(sz*0.5);
  443. } else {
  444. (*label_sizes)[i*2] = POINTS(0.75/2);
  445. (*label_sizes)[i*2+1] = POINTS(0.5*2);
  446. }
  447. if (agget(n, "fontsize")){
  448. sscanf(agget(n, "fontsize"), "%f", &ff);
  449. (*fsz)[i] = ff;
  450. } else {
  451. (*fsz)[i] = 14;
  452. }
  453. if (agget(n, "label") && strcmp(agget(n, "label"), "") != 0 && strcmp(agget(n, "label"), "\\N") != 0){
  454. char *lbs = agget(n, "label");
  455. (*labels)[i] = strdup(lbs);
  456. } else {
  457. (*labels)[i] = strdup(agnameof(n));
  458. }
  459. j = (*clusters)[i];
  460. if (MAX_GRPS-MIN_GRPS < max_color) {
  461. j = (j-MIN_GRPS)*((int)((max_color-1)/MAX((MAX_GRPS-MIN_GRPS),1)));
  462. } else {
  463. j = (j-MIN_GRPS)%max_color;
  464. }
  465. if (pal){
  466. (*rgb_r)[(*clusters)[i]] = pal[3*j+0];
  467. (*rgb_g)[(*clusters)[i]] = pal[3*j+1];
  468. (*rgb_b)[(*clusters)[i]] = pal[3*j+2];
  469. }
  470. if (!noclusterinfo && clust_clr_sym && (colorxlate(agxget(n,clust_clr_sym),&color,RGBA_DOUBLE) == COLOR_OK)) {
  471. (*rgb_r)[(*clusters)[i]] = color.u.RGBA[0];
  472. (*rgb_g)[(*clusters)[i]] = color.u.RGBA[1];
  473. (*rgb_b)[(*clusters)[i]] = color.u.RGBA[2];
  474. }
  475. const char *cc = agget(n, "clustercolor");
  476. if (!noclusterinfo && agget(n, "cluster") && cc && strlen(cc) >= 7 && pal) {
  477. (*rgb_r)[(*clusters)[i]] = hexcol2rgb(cc+1);
  478. (*rgb_g)[(*clusters)[i]] = hexcol2rgb(cc+3);
  479. (*rgb_b)[(*clusters)[i]] = hexcol2rgb(cc+5);
  480. }
  481. }
  482. if (x){
  483. bool has_position = false;
  484. *x = gv_calloc(dim * nnodes, sizeof(double));
  485. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  486. double xx,yy;
  487. i = ND_id(n);
  488. if (agget(n, "pos")){
  489. has_position = true;
  490. sscanf(agget(n, "pos"), "%lf,%lf", &xx, &yy);
  491. (*x)[i*dim] = xx;
  492. (*x)[i*dim+1] = yy;
  493. } else {
  494. fprintf(stderr,"WARNING: pos field missing for node %d, set to origin\n",i);
  495. (*x)[i*dim] = 0;
  496. (*x)[i*dim+1] = 0;
  497. }
  498. }
  499. if (!has_position){
  500. free(*x);
  501. *x = NULL;
  502. }
  503. }
  504. free(I);
  505. free(J);
  506. free(val);
  507. return A;
  508. }
  509. void attached_clustering(Agraph_t* g, int maxcluster, int clustering_scheme){
  510. SparseMatrix A = 0;
  511. Agnode_t* n;
  512. Agedge_t* e;
  513. Agsym_t *sym, *clust_sym;
  514. int nnodes;
  515. int nedges;
  516. int i, row,nc;
  517. double v;
  518. int type = MATRIX_TYPE_REAL;
  519. size_t sz = sizeof(double);
  520. if (!g) return;
  521. nnodes = agnnodes (g);
  522. nedges = agnedges (g);
  523. /* Assign node ids */
  524. i = 0;
  525. for (n = agfstnode (g); n; n = agnxtnode (g, n))
  526. ND_id(n) = i++;
  527. /* form matrix */
  528. int* I = gv_calloc(nedges, sizeof(int));
  529. int* J = gv_calloc(nedges, sizeof(int));
  530. double* val = gv_calloc(nedges, sizeof(double));
  531. sym = agattr(g, AGEDGE, "weight", NULL);
  532. clust_sym = agattr(g, AGNODE, "cluster", NULL);
  533. i = 0;
  534. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  535. row = ND_id(n);
  536. for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
  537. I[i] = row;
  538. J[i] = ND_id(aghead(e));
  539. if (sym) {
  540. if (sscanf (agxget(e,sym), "%lf", &v) != 1)
  541. v = 1;
  542. }
  543. else
  544. v = 1;
  545. val[i] = v;
  546. i++;
  547. }
  548. }
  549. A = SparseMatrix_from_coordinate_arrays(nedges, nnodes, nnodes, I, J, val, type, sz);
  550. int *clusters = gv_calloc(nnodes, sizeof(int));
  551. {
  552. double modularity;
  553. if (!clust_sym) clust_sym = agattr(g,AGNODE,"cluster","-1");
  554. if (clustering_scheme == CLUSTERING_MQ){
  555. mq_clustering(A, maxcluster,
  556. &nc, &clusters, &modularity);
  557. } else if (clustering_scheme == CLUSTERING_MODULARITY){
  558. modularity_clustering(A, false, maxcluster,
  559. &nc, &clusters, &modularity);
  560. } else {
  561. UNREACHABLE();
  562. }
  563. for (i = 0; i < nnodes; i++) (clusters)[i]++;/* make into 1 based */
  564. for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
  565. i = ND_id(n);
  566. agxbuf value_buffer = {0};
  567. agxbprint(&value_buffer, "%d", clusters[i]);
  568. agxset(n, clust_sym, agxbuse(&value_buffer));
  569. agxbfree(&value_buffer);
  570. }
  571. if (Verbose){
  572. fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
  573. }
  574. }
  575. free(I);
  576. free(J);
  577. free(val);
  578. free(clusters);
  579. SparseMatrix_delete(A);
  580. }
  581. void initDotIO (Agraph_t *g)
  582. {
  583. aginit(g, AGNODE, "info", sizeof(Agnodeinfo_t), true);
  584. }
  585. void setDotNodeID (Agnode_t* n, int v)
  586. {
  587. ND_id(n) = v;
  588. }
  589. int getDotNodeID (Agnode_t* n)
  590. {
  591. return ND_id(n);
  592. }