gc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * @file
  3. * @brief count graph components
  4. */
  5. /*************************************************************************
  6. * Copyright (c) 2011 AT&T Intellectual Property
  7. * All rights reserved. This program and the accompanying materials
  8. * are made available under the terms of the Eclipse Public License v1.0
  9. * which accompanies this distribution, and is available at
  10. * https://www.eclipse.org/legal/epl-v10.html
  11. *
  12. * Contributors: Details at https://graphviz.org
  13. *************************************************************************/
  14. /*
  15. * Written by Emden Gansner
  16. */
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <cgraph/cgraph.h>
  22. #include <cgraph/cghdr.h>
  23. #include <cgraph/ingraphs.h>
  24. #include <cgraph/list.h>
  25. #include <common/render.h>
  26. #include <common/utils.h>
  27. #include <util/exit.h>
  28. typedef struct {
  29. Agrec_t h;
  30. int dfs_mark;
  31. } nodeinfo_t;
  32. #define ND_dfs_mark(n) (((nodeinfo_t*)(n->base.data))->dfs_mark)
  33. #include <getopt.h>
  34. #define NODES 1
  35. #define EDGES 2
  36. #define CC 4
  37. #define CL 8
  38. #define DIRECTED 1
  39. #define UNDIRECTED 2
  40. static int tot_edges;
  41. static int tot_nodes;
  42. static int tot_cc;
  43. static int tot_cl;
  44. static int n_graphs;
  45. static int n_indent;
  46. static int recurse;
  47. static int silent;
  48. static int verbose;
  49. static int gtype;
  50. static int flags;
  51. static char *fname;
  52. static char **Inputs;
  53. static FILE *outfile;
  54. static char *useString = "Usage: gc [-necCaDUrsv?] <files>\n\
  55. -n - print number of nodes\n\
  56. -e - print number of edges\n\
  57. -c - print number of connected components\n\
  58. -C - print number of clusters\n\
  59. -a - print all counts\n\
  60. -D - only directed graphs\n\
  61. -U - only undirected graphs\n\
  62. -r - recursively analyze subgraphs\n\
  63. -s - silent\n\
  64. -v - verbose\n\
  65. -? - print usage\n\
  66. By default, gc prints nodes and edges\n\
  67. If no files are specified, stdin is used\n";
  68. static void usage(int v)
  69. {
  70. printf("%s",useString);
  71. graphviz_exit(v);
  72. }
  73. static void init(int argc, char *argv[])
  74. {
  75. int c;
  76. opterr = 0;
  77. while ((c = getopt(argc, argv, "necCaDUrsv?")) != -1) {
  78. switch (c) {
  79. case 'e':
  80. flags |= EDGES;
  81. break;
  82. case 'n':
  83. flags |= NODES;
  84. break;
  85. case 'c':
  86. flags |= CC;
  87. break;
  88. case 'C':
  89. flags |= CL;
  90. tot_cl = 0;
  91. break;
  92. case 'a':
  93. flags = NODES | EDGES | CC | CL;
  94. break;
  95. case 'r':
  96. recurse = 1;
  97. break;
  98. case 's':
  99. silent = 1;
  100. break;
  101. case 'v':
  102. verbose = 1;
  103. break;
  104. case 'D':
  105. gtype = DIRECTED;
  106. break;
  107. case 'U':
  108. gtype = UNDIRECTED;
  109. break;
  110. case '?':
  111. if (optopt == '\0' || optopt == '?')
  112. usage(0);
  113. else {
  114. fprintf(stderr, "gc: option -%c unrecognized\n",
  115. optopt);
  116. usage(1);
  117. }
  118. break;
  119. default:
  120. fprintf(stderr, "gc: unexpected error\n");
  121. graphviz_exit(EXIT_FAILURE);
  122. }
  123. }
  124. argv += optind;
  125. argc -= optind;
  126. if (argc)
  127. Inputs = argv;
  128. if (flags == 0)
  129. flags = NODES | EDGES;
  130. if (gtype == 0)
  131. gtype = DIRECTED | UNDIRECTED;
  132. outfile = stdout;
  133. }
  134. DEFINE_LIST(node_stack, Agnode_t *)
  135. static node_stack_t Stk;
  136. static void push(Agnode_t * np)
  137. {
  138. ND_dfs_mark(np) = -1;
  139. node_stack_push_back(&Stk, np);
  140. }
  141. static Agnode_t *pop(void)
  142. {
  143. if (node_stack_is_empty(&Stk)) {
  144. return 0;
  145. }
  146. return node_stack_pop_back(&Stk);
  147. }
  148. static void cc_dfs(Agraph_t * g, Agnode_t * n)
  149. {
  150. Agedge_t *e;
  151. Agnode_t *nxt;
  152. push(n);
  153. while ((n = pop())) {
  154. ND_dfs_mark(n) = 1;
  155. for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
  156. if (n == agtail(e))
  157. nxt = aghead(e);
  158. else
  159. nxt = agtail(e);
  160. if (ND_dfs_mark(nxt) == 0)
  161. push(nxt);
  162. }
  163. }
  164. }
  165. static void cntCluster(Agraph_t * g, Agobj_t * sg, void *arg)
  166. {
  167. (void)g;
  168. if (AGTYPE(sg) == AGRAPH && is_a_cluster((Agraph_t *)sg))
  169. *(int *) (arg) += 1;
  170. }
  171. static int cc_decompose(Agraph_t * g)
  172. {
  173. int c_cnt = 0;
  174. Agnode_t *n;
  175. c_cnt = 0;
  176. for (n = agfstnode(g); n; n = agnxtnode(g, n))
  177. ND_dfs_mark(n) = 0;
  178. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  179. if (ND_dfs_mark(n))
  180. continue;
  181. c_cnt++;
  182. cc_dfs(g, n);
  183. }
  184. return c_cnt;
  185. }
  186. static void ipr(long num)
  187. {
  188. printf(" %7ld", num);
  189. }
  190. static void
  191. wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
  192. {
  193. int i;
  194. if (silent)
  195. return;
  196. for (i = 0; i < n_indent; i++)
  197. fputs(" ", outfile);
  198. if (flags & NODES)
  199. ipr(nnodes);
  200. if (flags & EDGES)
  201. ipr(nedges);
  202. if (flags & CC)
  203. ipr(ncc);
  204. if (flags & CL)
  205. ipr(ncl);
  206. if (fname)
  207. printf(" %s (%s)\n", gname, filename);
  208. else
  209. printf(" %s\n", gname);
  210. }
  211. static void emit(Agraph_t * g, int root, int cl_count)
  212. {
  213. int n_edges = agnedges(g);
  214. int n_nodes = agnnodes(g);
  215. int n_cc = 0;
  216. int n_cl = 0;
  217. char *file = 0;
  218. if (flags & CC)
  219. n_cc = cc_decompose(g);
  220. if (flags & CL)
  221. n_cl = cl_count;
  222. if (root)
  223. file = fname;
  224. wcp(n_nodes, n_edges, n_cc, n_cl, agnameof(g), file);
  225. if (root) {
  226. n_graphs++;
  227. tot_edges += n_edges;
  228. tot_nodes += n_nodes;
  229. tot_cc += n_cc;
  230. tot_cl += n_cl;
  231. }
  232. }
  233. #define GTYPE(g) (agisdirected(g)?DIRECTED:UNDIRECTED)
  234. static int eval(Agraph_t * g, int root)
  235. {
  236. Agraph_t *subg;
  237. int cl_count = 0;
  238. if (root && !(GTYPE(g) & gtype))
  239. return 1;
  240. if (root) {
  241. aginit(g, AGNODE, "nodeinfo", sizeof(nodeinfo_t), true);
  242. }
  243. if ((flags & CL) && root)
  244. agapply(g, (Agobj_t *) g, cntCluster, &cl_count, false);
  245. emit(g, root, cl_count);
  246. if (recurse) {
  247. n_indent++;
  248. for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
  249. eval(subg, 0);
  250. n_indent--;
  251. }
  252. return 0;
  253. }
  254. int main(int argc, char *argv[])
  255. {
  256. Agraph_t *g;
  257. Agraph_t *prev = NULL;
  258. ingraph_state ig;
  259. int rv = 0;
  260. init(argc, argv);
  261. newIngraph(&ig, Inputs);
  262. while ((g = nextGraph(&ig)) != 0) {
  263. if (prev)
  264. agclose(prev);
  265. prev = g;
  266. fname = fileName(&ig);
  267. if (verbose)
  268. fprintf(stderr, "Process graph %s in file %s\n", agnameof(g),
  269. fname);
  270. rv |= eval(g, 1);
  271. }
  272. if (n_graphs > 1)
  273. wcp(tot_nodes, tot_edges, tot_cc, tot_cl, "total", 0);
  274. node_stack_free(&Stk);
  275. graphviz_exit(rv);
  276. }