cluster.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "config.h"
  11. #include "../tools/openFile.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #define STANDALONE
  15. #include <sparse/general.h>
  16. #include <sparse/QuadTree.h>
  17. #include <time.h>
  18. #include <sparse/SparseMatrix.h>
  19. #include <getopt.h>
  20. #include <string.h>
  21. #include "make_map.h"
  22. #include <sfdpgen/spring_electrical.h>
  23. #include <sfdpgen/post_process.h>
  24. #include <neatogen/overlap.h>
  25. #include <sparse/clustering.h>
  26. #include <cgraph/ingraphs.h>
  27. #include <sparse/DotIO.h>
  28. #include <sparse/colorutil.h>
  29. #include <util/unreachable.h>
  30. typedef struct {
  31. FILE* outfp;
  32. char** infiles;
  33. int maxcluster;
  34. int clustering_method;
  35. } opts_t;
  36. static const char usestr[] =
  37. " -C k - generate no more than k clusters (0)\n\
  38. 0 : no limit\n\
  39. -c k - use clustering method k (0)\n\
  40. 0 : use modularity\n\
  41. 1 : use modularity quality\n\
  42. -o <outfile> - output file (stdout)\n\
  43. -v - verbose mode\n\
  44. -? - print usage\n";
  45. static void usage(char* cmd, int eval)
  46. {
  47. fprintf(stderr, "Usage: %s <options> graphfile\n", cmd);
  48. fputs (usestr, stderr);
  49. graphviz_exit(eval);
  50. }
  51. static void init(int argc, char *argv[], opts_t* opts) {
  52. char* cmd = argv[0];
  53. int c;
  54. int v;
  55. opts->maxcluster = 0;
  56. opts->outfp = stdout;
  57. Verbose = 0;
  58. opts->clustering_method = CLUSTERING_MODULARITY;
  59. while ((c = getopt(argc, argv, ":vC:c:o:?")) != -1) {
  60. switch (c) {
  61. case 'c':
  62. if (sscanf(optarg, "%d", &v) == 0 || v < 0) {
  63. usage(cmd,1);
  64. }
  65. else opts->clustering_method = v;
  66. break;
  67. case 'C':
  68. if (sscanf(optarg, "%d", &v) == 0 || v < 0) {
  69. usage(cmd,1);
  70. }
  71. else opts->maxcluster = v;
  72. break;
  73. case 'o':
  74. opts->outfp = openFile(cmd, optarg, "w");
  75. break;
  76. case 'v':
  77. Verbose = 1;
  78. break;
  79. case '?':
  80. if (optopt == '\0' || optopt == '?')
  81. usage(cmd, 0);
  82. else {
  83. fprintf(stderr, " option -%c unrecognized\n",
  84. optopt);
  85. usage(cmd, 1);
  86. }
  87. break;
  88. default:
  89. UNREACHABLE();
  90. }
  91. }
  92. argv += optind;
  93. argc -= optind;
  94. if (argc)
  95. opts->infiles = argv;
  96. else
  97. opts->infiles = NULL;
  98. }
  99. static void clusterGraph (Agraph_t* g, int maxcluster, int clustering_method){
  100. initDotIO(g);
  101. attached_clustering(g, maxcluster, clustering_method);
  102. }
  103. int main(int argc, char *argv[])
  104. {
  105. Agraph_t *g = 0, *prevg = 0;
  106. ingraph_state ig;
  107. opts_t opts;
  108. init(argc, argv, &opts);
  109. newIngraph(&ig, opts.infiles);
  110. while ((g = nextGraph (&ig)) != 0) {
  111. if (prevg) agclose (prevg);
  112. clusterGraph (g, opts.maxcluster, opts.clustering_method);
  113. agwrite(g, opts.outfp);
  114. prevg = g;
  115. }
  116. graphviz_exit(0);
  117. }