spanningtree.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*!
  2. \file vector/neta/spanningtree.c
  3. \brief Network Analysis library - spanning tree
  4. Computes minimum spanning tree in the network.
  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. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <grass/gis.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. #include <grass/dgl/graph.h>
  16. struct union_find
  17. {
  18. int *parent;
  19. };
  20. static int uf_initialize(struct union_find *uf, int size)
  21. {
  22. int i;
  23. uf->parent = (int *)G_calloc(size, sizeof(int));
  24. if (!uf->parent)
  25. return 0;
  26. for (i = 0; i < size; i++)
  27. uf->parent[i] = i;
  28. return 1;
  29. }
  30. static void uf_release(struct union_find *uf)
  31. {
  32. G_free(uf->parent);
  33. }
  34. static int uf_find(struct union_find *uf, int v)
  35. {
  36. int cur = v, tmp;
  37. while (uf->parent[cur] != cur)
  38. cur = uf->parent[cur];
  39. while (uf->parent[v] != v) {
  40. tmp = uf->parent[v];
  41. uf->parent[v] = cur;
  42. v = tmp;
  43. }
  44. return cur;
  45. }
  46. /*TODO: union by rank */
  47. static void uf_union(struct union_find *uf, int u, int v)
  48. {
  49. int parent_u = uf_find(uf, u);
  50. int parent_v = uf_find(uf, v);
  51. if (parent_u != parent_v)
  52. uf->parent[parent_u] = parent_v;
  53. }
  54. typedef struct
  55. {
  56. dglInt32_t cost;
  57. dglInt32_t *edge;
  58. } edge_cost_pair;
  59. static int cmp_edge(const void *pa, const void *pb)
  60. {
  61. return ((edge_cost_pair *) pa)->cost - ((edge_cost_pair *) pb)->cost;
  62. }
  63. /*!
  64. \brief Get number of edges in the spanning forest
  65. \param graph input graph
  66. \param[out] list of edges
  67. \return number of edges
  68. \return -1 on failure
  69. */
  70. int NetA_spanning_tree(dglGraph_s * graph, struct ilist *tree_list)
  71. {
  72. int nnodes, edges, nedges, i, index;
  73. edge_cost_pair *perm; /*permutaion of edges in ascending order */
  74. struct union_find uf;
  75. dglEdgesetTraverser_s et;
  76. nnodes = dglGet_NodeCount(graph);
  77. nedges = dglGet_EdgeCount(graph);
  78. perm = (edge_cost_pair *) G_calloc(nedges, sizeof(edge_cost_pair));
  79. if (!perm || !uf_initialize(&uf, nnodes + 1)) {
  80. G_fatal_error(_("Out of memory"));
  81. return -1;
  82. }
  83. /*for some obscure reasons, dglGetEdge always returns NULL. Therefore this complicated enumeration of the edges... */
  84. index = 0;
  85. G_message(_("Computing minimum spanning tree..."));
  86. G_percent_reset();
  87. for (i = 1; i <= nnodes; i++) {
  88. G_percent(i, nnodes + nedges, 1);
  89. dglInt32_t *edge;
  90. dglEdgeset_T_Initialize(&et, graph,
  91. dglNodeGet_OutEdgeset(graph,
  92. dglGetNode(graph,
  93. (dglInt32_t)
  94. i)));
  95. for (edge = dglEdgeset_T_First(&et); edge;
  96. edge = dglEdgeset_T_Next(&et))
  97. if (dglEdgeGet_Id(graph, edge) > 0) {
  98. perm[index].edge = edge;
  99. perm[index].cost = dglEdgeGet_Cost(graph, edge);
  100. index++;
  101. }
  102. dglEdgeset_T_Release(&et);
  103. }
  104. edges = 0;
  105. qsort((void *)perm, index, sizeof(edge_cost_pair), cmp_edge);
  106. for (i = 0; i < index; i++) {
  107. G_percent(i + nnodes, nnodes + nedges, 1);
  108. dglInt32_t head =
  109. dglNodeGet_Id(graph, dglEdgeGet_Head(graph, perm[i].edge));
  110. dglInt32_t tail =
  111. dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, perm[i].edge));
  112. if (uf_find(&uf, head) != uf_find(&uf, tail)) {
  113. uf_union(&uf, head, tail);
  114. edges++;
  115. Vect_list_append(tree_list, dglEdgeGet_Id(graph, perm[i].edge));
  116. }
  117. }
  118. G_free(perm);
  119. uf_release(&uf);
  120. return edges;
  121. }