components.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*!
  2. \file vector/neta/components.c
  3. \brief Network Analysis library - graph componets
  4. Computes strongly and weakly connected components.
  5. (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Daniel Bundala (Google Summer of Code 2009)
  9. \author Markus Metz
  10. */
  11. /* example:
  12. *
  13. * X -->-- X ---- X --<-- X ---- X
  14. * N1 N2 N3 N4 N5
  15. *
  16. * -->--, --<-- one-way
  17. * ---- both ways
  18. *
  19. * weakly connected:
  20. * all 5 nodes, even though there is no direct path from N1 to N4, 5
  21. * but N1 connects to N2, 3, and N4, 5 also connect to N2, 3
  22. *
  23. * strongly connected:
  24. * no path from N2 to N1, no path from N3 to N4
  25. * component 1: N1
  26. * component 2: N2, 3
  27. * Component3: N4, 5
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <grass/gis.h>
  32. #include <grass/vector.h>
  33. #include <grass/glocale.h>
  34. #include <grass/dgl/graph.h>
  35. /*!
  36. \brief Computes weakly connected components
  37. \param graph input graph
  38. \param[out] component array of component ids
  39. \return number of components
  40. \return -1 on failure
  41. */
  42. int NetA_weakly_connected_components(dglGraph_s * graph, int *component)
  43. {
  44. int nnodes, i;
  45. dglInt32_t *stack;
  46. int stack_size, components;
  47. dglInt32_t *cur_node;
  48. dglNodeTraverser_s nt;
  49. int have_node_costs;
  50. dglInt32_t ncost;
  51. if (graph->Version < 2) {
  52. G_warning("Directed graph must be version 2 or 3 for NetA_weakly_connected_components()");
  53. return -1;
  54. }
  55. components = 0;
  56. nnodes = dglGet_NodeCount(graph);
  57. stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  58. if (!stack) {
  59. G_fatal_error(_("Out of memory"));
  60. return -1;
  61. }
  62. for (i = 1; i <= nnodes; i++)
  63. component[i] = 0;
  64. ncost = 0;
  65. have_node_costs = dglGet_NodeAttrSize(graph);
  66. dglNode_T_Initialize(&nt, graph);
  67. for (cur_node = dglNode_T_First(&nt); cur_node;
  68. cur_node = dglNode_T_Next(&nt)) {
  69. dglInt32_t cur_node_id = dglNodeGet_Id(graph, cur_node);
  70. if (!component[cur_node_id]) {
  71. stack[0] = cur_node_id;
  72. stack_size = 1;
  73. component[cur_node_id] = ++components;
  74. while (stack_size) {
  75. dglInt32_t *node, *edgeset, *edge;
  76. dglEdgesetTraverser_s et;
  77. node = dglGetNode(graph, stack[--stack_size]);
  78. edgeset = dglNodeGet_OutEdgeset(graph, node);
  79. dglEdgeset_T_Initialize(&et, graph, edgeset);
  80. for (edge = dglEdgeset_T_First(&et); edge;
  81. edge = dglEdgeset_T_Next(&et)) {
  82. dglInt32_t to;
  83. to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
  84. if (!component[to]) {
  85. component[to] = components;
  86. /* do not go through closed nodes */
  87. if (have_node_costs) {
  88. memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)),
  89. sizeof(ncost));
  90. }
  91. if (ncost >= 0)
  92. stack[stack_size++] = to;
  93. }
  94. }
  95. dglEdgeset_T_Release(&et);
  96. edgeset = dglNodeGet_InEdgeset(graph, node);
  97. dglEdgeset_T_Initialize(&et, graph, edgeset);
  98. for (edge = dglEdgeset_T_First(&et); edge;
  99. edge = dglEdgeset_T_Next(&et)) {
  100. dglInt32_t to;
  101. to = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, edge));
  102. if (!component[to]) {
  103. component[to] = components;
  104. /* do not go through closed nodes */
  105. if (have_node_costs) {
  106. memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)),
  107. sizeof(ncost));
  108. }
  109. if (ncost >= 0)
  110. stack[stack_size++] = to;
  111. }
  112. }
  113. dglEdgeset_T_Release(&et);
  114. }
  115. }
  116. }
  117. dglNode_T_Release(&nt);
  118. G_free(stack);
  119. return components;
  120. }
  121. /*!
  122. \brief Computes strongly connected components with Kosaraju's
  123. two-pass algorithm
  124. \param graph input graph
  125. \param[out] component array of component ids
  126. \return number of components
  127. \return -1 on failure
  128. */
  129. int NetA_strongly_connected_components(dglGraph_s * graph, int *component)
  130. {
  131. int nnodes, i;
  132. dglInt32_t *stack, *order;
  133. int *processed;
  134. int stack_size, order_size, components;
  135. dglInt32_t *cur_node;
  136. dglNodeTraverser_s nt;
  137. int have_node_costs;
  138. dglInt32_t ncost;
  139. if (graph->Version < 2) {
  140. G_warning("Directed graph must be version 2 or 3 for NetA_strongly_connected_components()");
  141. return -1;
  142. }
  143. components = 0;
  144. nnodes = dglGet_NodeCount(graph);
  145. stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  146. order = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  147. processed = (int *)G_calloc(nnodes + 1, sizeof(int));
  148. if (!stack || !order || !processed) {
  149. G_fatal_error(_("Out of memory"));
  150. return -1;
  151. }
  152. for (i = 1; i <= nnodes; i++) {
  153. component[i] = 0;
  154. }
  155. ncost = 0;
  156. have_node_costs = dglGet_NodeAttrSize(graph);
  157. order_size = 0;
  158. dglNode_T_Initialize(&nt, graph);
  159. for (cur_node = dglNode_T_First(&nt); cur_node;
  160. cur_node = dglNode_T_Next(&nt)) {
  161. dglInt32_t cur_node_id = dglNodeGet_Id(graph, cur_node);
  162. if (!component[cur_node_id]) {
  163. component[cur_node_id] = --components;
  164. stack[0] = cur_node_id;
  165. stack_size = 1;
  166. while (stack_size) {
  167. dglInt32_t *node, *edgeset, *edge;
  168. dglEdgesetTraverser_s et;
  169. dglInt32_t node_id = stack[stack_size - 1];
  170. if (processed[node_id]) {
  171. stack_size--;
  172. order[order_size++] = node_id;
  173. continue;
  174. }
  175. processed[node_id] = 1;
  176. node = dglGetNode(graph, node_id);
  177. edgeset = dglNodeGet_OutEdgeset(graph, node);
  178. dglEdgeset_T_Initialize(&et, graph, edgeset);
  179. for (edge = dglEdgeset_T_First(&et); edge;
  180. edge = dglEdgeset_T_Next(&et)) {
  181. dglInt32_t to;
  182. to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
  183. if (!component[to]) {
  184. component[to] = components;
  185. /* do not go through closed nodes */
  186. if (have_node_costs) {
  187. memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)),
  188. sizeof(ncost));
  189. }
  190. if (ncost < 0)
  191. processed[to] = 1;
  192. stack[stack_size++] = to;
  193. }
  194. }
  195. dglEdgeset_T_Release(&et);
  196. }
  197. }
  198. }
  199. dglNode_T_Release(&nt);
  200. components = 0;
  201. dglNode_T_Initialize(&nt, graph);
  202. while (order_size) {
  203. dglInt32_t cur_node_id = order[--order_size];
  204. int cur_comp = component[cur_node_id];
  205. if (cur_comp < 1) {
  206. component[cur_node_id] = ++components;
  207. stack[0] = cur_node_id;
  208. stack_size = 1;
  209. while (stack_size) {
  210. dglInt32_t *node, *edgeset, *edge;
  211. dglEdgesetTraverser_s et;
  212. dglInt32_t node_id = stack[--stack_size];
  213. node = dglGetNode(graph, node_id);
  214. edgeset = dglNodeGet_InEdgeset(graph, node);
  215. dglEdgeset_T_Initialize(&et, graph, edgeset);
  216. for (edge = dglEdgeset_T_First(&et); edge;
  217. edge = dglEdgeset_T_Next(&et)) {
  218. dglInt32_t to;
  219. to = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, edge));
  220. if (component[to] == cur_comp) {
  221. component[to] = components;
  222. /* do not go through closed nodes */
  223. if (have_node_costs) {
  224. memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Head(graph, edge)),
  225. sizeof(ncost));
  226. }
  227. if (ncost >= 0)
  228. stack[stack_size++] = to;
  229. }
  230. }
  231. dglEdgeset_T_Release(&et);
  232. }
  233. }
  234. }
  235. dglNode_T_Release(&nt);
  236. G_free(stack);
  237. G_free(order);
  238. G_free(processed);
  239. return components;
  240. }