spanningtree.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (((edge_cost_pair *) pa)->cost < ((edge_cost_pair *) pb)->cost)
  62. return -1;
  63. return (((edge_cost_pair *) pa)->cost > ((edge_cost_pair *) pb)->cost);
  64. }
  65. /*!
  66. \brief Get number of edges in the spanning forest
  67. \param graph input graph
  68. \param[out] list of edges
  69. \return number of edges
  70. \return -1 on failure
  71. */
  72. int NetA_spanning_tree(dglGraph_s * graph, struct ilist *tree_list)
  73. {
  74. int nnodes, edges, nedges, i, index;
  75. edge_cost_pair *perm; /*permutation of edges in ascending order */
  76. struct union_find uf;
  77. dglEdgesetTraverser_s et;
  78. /* TODO: consider closed nodes / node costs */
  79. nnodes = dglGet_NodeCount(graph);
  80. nedges = dglGet_EdgeCount(graph);
  81. perm = (edge_cost_pair *) G_calloc(nedges, sizeof(edge_cost_pair));
  82. if (!perm || !uf_initialize(&uf, nnodes + 1)) {
  83. G_fatal_error(_("Out of memory"));
  84. return -1;
  85. }
  86. /* dglGetEdge is only supported with graphs version > 1. Therefore this complicated enumeration of the edges... */
  87. index = 0;
  88. G_message(_("Computing minimum spanning tree..."));
  89. G_percent_reset();
  90. for (i = 1; i <= nnodes; i++) {
  91. G_percent(i, nnodes + nedges, 1);
  92. dglInt32_t *edge;
  93. dglEdgeset_T_Initialize(&et, graph,
  94. dglNodeGet_OutEdgeset(graph,
  95. dglGetNode(graph,
  96. (dglInt32_t)
  97. i)));
  98. for (edge = dglEdgeset_T_First(&et); edge;
  99. edge = dglEdgeset_T_Next(&et))
  100. if (dglEdgeGet_Id(graph, edge) > 0) {
  101. perm[index].edge = edge;
  102. perm[index].cost = dglEdgeGet_Cost(graph, edge);
  103. index++;
  104. }
  105. dglEdgeset_T_Release(&et);
  106. }
  107. edges = 0;
  108. qsort((void *)perm, index, sizeof(edge_cost_pair), cmp_edge);
  109. for (i = 0; i < index; i++) {
  110. G_percent(i + nnodes, nnodes + nedges, 1);
  111. dglInt32_t head =
  112. dglNodeGet_Id(graph, dglEdgeGet_Head(graph, perm[i].edge));
  113. dglInt32_t tail =
  114. dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, perm[i].edge));
  115. if (uf_find(&uf, head) != uf_find(&uf, tail)) {
  116. uf_union(&uf, head, tail);
  117. edges++;
  118. Vect_list_append(tree_list, dglEdgeGet_Id(graph, perm[i].edge));
  119. }
  120. }
  121. G_percent(index, index, 1);
  122. G_free(perm);
  123. uf_release(&uf);
  124. return edges;
  125. }