decomp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Decompose finds the connected components of a graph.
  12. * It searches the temporary edges and ignores non-root nodes.
  13. * The roots of the search are the real nodes of the graph,
  14. * but any virtual nodes discovered are also included in the
  15. * component.
  16. */
  17. #include <cgraph/list.h>
  18. #include <dotgen/dot.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <util/alloc.h>
  22. static node_t *Last_node;
  23. static size_t Cmark;
  24. static void
  25. begin_component(graph_t* g)
  26. {
  27. Last_node = GD_nlist(g) = NULL;
  28. }
  29. static void
  30. add_to_component(graph_t* g, node_t * n)
  31. {
  32. ND_mark(n) = Cmark;
  33. if (Last_node) {
  34. ND_prev(n) = Last_node;
  35. ND_next(Last_node) = n;
  36. } else {
  37. ND_prev(n) = NULL;
  38. GD_nlist(g) = n;
  39. }
  40. Last_node = n;
  41. ND_next(n) = NULL;
  42. }
  43. static void
  44. end_component(graph_t* g)
  45. {
  46. size_t i = GD_comp(g).size++;
  47. GD_comp(g).list = gv_recalloc(GD_comp(g).list, GD_comp(g).size - 1,
  48. GD_comp(g).size, sizeof(node_t *));
  49. GD_comp(g).list[i] = GD_nlist(g);
  50. }
  51. DEFINE_LIST(node_stack, node_t *)
  52. static void push(node_stack_t *sp, node_t *np) {
  53. ND_mark(np) = Cmark + 1;
  54. node_stack_push_back(sp, np);
  55. }
  56. static node_t *pop(node_stack_t *sp) {
  57. if (node_stack_is_empty(sp)) {
  58. return NULL;
  59. }
  60. return node_stack_pop_back(sp);
  61. }
  62. /* search_component:
  63. * iterative dfs for components.
  64. * We process the edges in reverse order of the recursive version to maintain
  65. * the processing order of the nodes.
  66. * Since are using a stack, we need to indicate nodes on the stack. Nodes unprocessed
  67. * in this call to decompose will have mark < Cmark; processed nodes will have mark=Cmark;
  68. * so we use mark = Cmark+1 to indicate nodes on the stack.
  69. */
  70. static void search_component(node_stack_t *stk, graph_t *g, node_t *n) {
  71. int c;
  72. elist vec[4];
  73. node_t *other;
  74. edge_t *e;
  75. edge_t **ep;
  76. push(stk, n);
  77. while ((n = pop(stk))) {
  78. if (ND_mark(n) == Cmark) continue;
  79. add_to_component(g, n);
  80. vec[0] = ND_out(n);
  81. vec[1] = ND_in(n);
  82. vec[2] = ND_flat_out(n);
  83. vec[3] = ND_flat_in(n);
  84. for (c = 3; c >= 0; c--) {
  85. if (vec[c].list && vec[c].size != 0) {
  86. size_t i;
  87. for (i = vec[c].size - 1, ep = vec[c].list + i; i != SIZE_MAX; i--, ep--) {
  88. e = *ep;
  89. if ((other = aghead(e)) == n)
  90. other = agtail(e);
  91. if ((ND_mark(other) != Cmark) && (other == UF_find(other)))
  92. push(stk, other);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. void decompose(graph_t * g, int pass)
  99. {
  100. graph_t *subg;
  101. node_t *n, *v;
  102. node_stack_t stk = {0};
  103. if (++Cmark == 0)
  104. Cmark = 1;
  105. GD_comp(g).size = 0;
  106. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  107. v = n;
  108. if ((pass > 0) && (subg = ND_clust(v)))
  109. v = GD_rankleader(subg)[ND_rank(v)];
  110. else if (v != UF_find(v))
  111. continue;
  112. if (ND_mark(v) != Cmark) {
  113. begin_component(g);
  114. search_component(&stk, g, v);
  115. end_component(g);
  116. }
  117. }
  118. node_stack_free(&stk);
  119. }