path.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. \file vector/neta/path.c
  3. \brief Network Analysis library - shortest path
  4. Shortest paths from a set of nodes.
  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. #include <grass/neta.h>
  17. /*!
  18. \brief Computes shortests paths to every node from nodes in "from".
  19. Array "dst" contains the length of the path or -1 if the node is not
  20. reachable. Prev contains edges from predecessor along the shortest
  21. path.
  22. \param graph input graph
  23. \param from list of 'from' positions
  24. \param dst list of 'to' positions
  25. \param[out] prev list of edges from predecessor along the shortest path
  26. \return 0 on success
  27. \return -1 on failure
  28. */
  29. int NetA_distance_from_points(dglGraph_s *graph, struct ilist *from,
  30. int *dst, dglInt32_t **prev)
  31. {
  32. int i, nnodes;
  33. dglHeap_s heap;
  34. nnodes = dglGet_NodeCount(graph);
  35. dglEdgesetTraverser_s et;
  36. /* initialize costs and edge list */
  37. for (i = 1; i <= nnodes; i++) {
  38. dst[i] = -1;
  39. prev[i] = NULL;
  40. }
  41. dglHeapInit(&heap);
  42. for (i = 0; i < from->n_values; i++) {
  43. int v = from->value[i];
  44. if (dst[v] == -2)
  45. continue; /*ingore duplicates */
  46. dst[v] = -2; /* make sure all from nodes are processed first */
  47. dglHeapData_u heap_data;
  48. heap_data.ul = v;
  49. dglHeapInsertMin(&heap, -2, ' ', heap_data);
  50. }
  51. while (1) {
  52. dglInt32_t v, dist;
  53. dglHeapNode_s heap_node;
  54. dglHeapData_u heap_data;
  55. if (!dglHeapExtractMin(&heap, &heap_node))
  56. break;
  57. v = heap_node.value.ul;
  58. dist = heap_node.key;
  59. if (dst[v] < dist)
  60. continue;
  61. dglInt32_t *edge;
  62. dglEdgeset_T_Initialize(&et, graph,
  63. dglNodeGet_OutEdgeset(graph,
  64. dglGetNode(graph, v)));
  65. if (dglGet_NodeAttrSize(graph) > 0) {
  66. dglInt32_t ncost;
  67. memcpy(&ncost, dglNodeGet_Attr(graph, dglGetNode(graph, v)),
  68. sizeof(ncost));
  69. /* do not route paths through closed nodes */
  70. if (ncost < 0 && dist >= 0)
  71. continue;
  72. dist += ncost;
  73. }
  74. for (edge = dglEdgeset_T_First(&et); edge;
  75. edge = dglEdgeset_T_Next(&et)) {
  76. dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
  77. dglInt32_t to_id = dglNodeGet_Id(graph, to);
  78. dglInt32_t d = dglEdgeGet_Cost(graph, edge);
  79. if (dst[to_id] < 0 || dst[to_id] > dist + d) {
  80. dst[to_id] = dist + d;
  81. prev[to_id] = edge;
  82. heap_data.ul = to_id;
  83. dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
  84. }
  85. }
  86. dglEdgeset_T_Release(&et);
  87. }
  88. dglHeapFree(&heap, NULL);
  89. return 0;
  90. }
  91. /*!
  92. \brief Find a path (minimum number of edges) from 'from' to 'to' using only edges in 'edges'.
  93. Precisely, edge with id I is used if edges[abs(i)] == 1. List
  94. stores the indices of lines on the path. Method return number of
  95. edges or -1 if no path exist.
  96. \param graph input graph
  97. \param from 'from' position
  98. \param to 'to' position
  99. \param edges list of available edges
  100. \param[out] list list of edges
  101. \return number of edges
  102. \return -1 on failure
  103. */
  104. int NetA_find_path(dglGraph_s * graph, int from, int to, int *edges,
  105. struct ilist *list)
  106. {
  107. dglInt32_t **prev, *queue;
  108. dglEdgesetTraverser_s et;
  109. char *vis;
  110. int begin, end, cur, nnodes;
  111. nnodes = dglGet_NodeCount(graph);
  112. prev = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
  113. queue = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  114. vis = (char *)G_calloc(nnodes + 1, sizeof(char));
  115. if (!prev || !queue || !vis) {
  116. G_fatal_error(_("Out of memory"));
  117. return -1;
  118. }
  119. Vect_reset_list(list);
  120. begin = 0;
  121. end = 1;
  122. vis[from] = 'y';
  123. queue[0] = from;
  124. prev[from] = NULL;
  125. while (begin != end) {
  126. dglInt32_t vertex = queue[begin++];
  127. if (vertex == to)
  128. break;
  129. dglInt32_t *edge, *node = dglGetNode(graph, vertex);
  130. if (dglGet_NodeAttrSize(graph) > 0) {
  131. dglInt32_t ncost;
  132. memcpy(&ncost, dglNodeGet_Attr(graph, node), sizeof(ncost));
  133. /* do not route paths through closed nodes */
  134. if (ncost < 0 && vertex != from)
  135. continue;
  136. }
  137. dglEdgeset_T_Initialize(&et, graph,
  138. dglNodeGet_OutEdgeset(graph, node));
  139. for (edge = dglEdgeset_T_First(&et); edge;
  140. edge = dglEdgeset_T_Next(&et)) {
  141. dglInt32_t id = abs(dglEdgeGet_Id(graph, edge));
  142. dglInt32_t to =
  143. dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
  144. if (edges[id] && !vis[to]) {
  145. vis[to] = 'y';
  146. prev[to] = edge;
  147. queue[end++] = to;
  148. }
  149. }
  150. dglEdgeset_T_Release(&et);
  151. }
  152. G_free(queue);
  153. if (!vis[to]) {
  154. G_free(prev);
  155. G_free(vis);
  156. return -1;
  157. }
  158. cur = to;
  159. while (prev[cur] != NULL) {
  160. Vect_list_append(list, abs(dglEdgeGet_Id(graph, prev[cur])));
  161. cur = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, prev[cur]));
  162. }
  163. G_free(prev);
  164. G_free(vis);
  165. return list->n_values;
  166. }