path.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 shortest 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] == 0)
  45. continue; /* ignore duplicates */
  46. dst[v] = 0; /* make sure all from nodes are processed first */
  47. dglHeapData_u heap_data;
  48. heap_data.ul = v;
  49. dglHeapInsertMin(&heap, 0, ' ', 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. for (edge = dglEdgeset_T_First(&et); edge;
  66. edge = dglEdgeset_T_Next(&et)) {
  67. dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
  68. dglInt32_t to_id = dglNodeGet_Id(graph, to);
  69. dglInt32_t d = dglEdgeGet_Cost(graph, edge);
  70. if (dst[to_id] < 0 || dst[to_id] > dist + d) {
  71. dst[to_id] = dist + d;
  72. prev[to_id] = edge;
  73. heap_data.ul = to_id;
  74. dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
  75. }
  76. }
  77. dglEdgeset_T_Release(&et);
  78. }
  79. dglHeapFree(&heap, NULL);
  80. return 0;
  81. }
  82. /*!
  83. \brief Computes shortest paths from every node to nodes in "to".
  84. Array "dst" contains the length of the path or -1 if the node is not
  85. reachable. Nxt contains edges from successor along the shortest
  86. path.
  87. \param graph input graph
  88. \param from list of 'from' positions
  89. \param dst list of 'to' positions
  90. \param[out] nxt list of edges from successor along the shortest path
  91. \return 0 on success
  92. \return -1 on failure
  93. */
  94. int NetA_distance_to_points(dglGraph_s *graph, struct ilist *to,
  95. int *dst, dglInt32_t **nxt)
  96. {
  97. int i, nnodes;
  98. dglHeap_s heap;
  99. dglEdgesetTraverser_s et;
  100. nnodes = dglGet_NodeCount(graph);
  101. /* initialize costs and edge list */
  102. for (i = 1; i <= nnodes; i++) {
  103. dst[i] = -1;
  104. nxt[i] = NULL;
  105. }
  106. if (graph->Version < 2) {
  107. G_warning("Directed graph must be version 2 or 3 for NetA_distance_to_points()");
  108. return -1;
  109. }
  110. dglHeapInit(&heap);
  111. for (i = 0; i < to->n_values; i++) {
  112. int v = to->value[i];
  113. if (dst[v] == 0)
  114. continue; /* ignore duplicates */
  115. dst[v] = 0; /* make sure all to nodes are processed first */
  116. dglHeapData_u heap_data;
  117. heap_data.ul = v;
  118. dglHeapInsertMin(&heap, 0, ' ', heap_data);
  119. }
  120. while (1) {
  121. dglInt32_t v, dist;
  122. dglHeapNode_s heap_node;
  123. dglHeapData_u heap_data;
  124. if (!dglHeapExtractMin(&heap, &heap_node))
  125. break;
  126. v = heap_node.value.ul;
  127. dist = heap_node.key;
  128. if (dst[v] < dist)
  129. continue;
  130. dglInt32_t *edge;
  131. dglEdgeset_T_Initialize(&et, graph,
  132. dglNodeGet_InEdgeset(graph,
  133. dglGetNode(graph, v)));
  134. for (edge = dglEdgeset_T_First(&et); edge;
  135. edge = dglEdgeset_T_Next(&et)) {
  136. dglInt32_t *from = dglEdgeGet_Head(graph, edge);
  137. dglInt32_t from_id = dglNodeGet_Id(graph, from);
  138. dglInt32_t d = dglEdgeGet_Cost(graph, edge);
  139. if (dst[from_id] < 0 || dst[from_id] > dist + d) {
  140. dst[from_id] = dist + d;
  141. nxt[from_id] = edge;
  142. heap_data.ul = from_id;
  143. dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
  144. }
  145. }
  146. dglEdgeset_T_Release(&et);
  147. }
  148. dglHeapFree(&heap, NULL);
  149. return 0;
  150. }
  151. /*!
  152. \brief Find a path (minimum number of edges) from 'from' to 'to' using only edges in 'edges'.
  153. Precisely, edge with id I is used if edges[abs(i)] == 1. List
  154. stores the indices of lines on the path. Method return number of
  155. edges or -1 if no path exist.
  156. \param graph input graph
  157. \param from 'from' position
  158. \param to 'to' position
  159. \param edges list of available edges
  160. \param[out] list list of edges
  161. \return number of edges
  162. \return -1 on failure
  163. */
  164. int NetA_find_path(dglGraph_s * graph, int from, int to, int *edges,
  165. struct ilist *list)
  166. {
  167. dglInt32_t **prev, *queue;
  168. dglEdgesetTraverser_s et;
  169. char *vis;
  170. int begin, end, cur, nnodes;
  171. nnodes = dglGet_NodeCount(graph);
  172. prev = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
  173. queue = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  174. vis = (char *)G_calloc(nnodes + 1, sizeof(char));
  175. if (!prev || !queue || !vis) {
  176. G_fatal_error(_("Out of memory"));
  177. return -1;
  178. }
  179. Vect_reset_list(list);
  180. begin = 0;
  181. end = 1;
  182. vis[from] = 'y';
  183. queue[0] = from;
  184. prev[from] = NULL;
  185. while (begin != end) {
  186. dglInt32_t vertex = queue[begin++];
  187. if (vertex == to)
  188. break;
  189. dglInt32_t *edge, *node = dglGetNode(graph, vertex);
  190. dglEdgeset_T_Initialize(&et, graph,
  191. dglNodeGet_OutEdgeset(graph, node));
  192. for (edge = dglEdgeset_T_First(&et); edge;
  193. edge = dglEdgeset_T_Next(&et)) {
  194. dglInt32_t id = abs(dglEdgeGet_Id(graph, edge));
  195. dglInt32_t to =
  196. dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
  197. if (edges[id] && !vis[to]) {
  198. vis[to] = 'y';
  199. prev[to] = edge;
  200. queue[end++] = to;
  201. }
  202. }
  203. dglEdgeset_T_Release(&et);
  204. }
  205. G_free(queue);
  206. if (!vis[to]) {
  207. G_free(prev);
  208. G_free(vis);
  209. return -1;
  210. }
  211. cur = to;
  212. while (prev[cur] != NULL) {
  213. Vect_list_append(list, abs(dglEdgeGet_Id(graph, prev[cur])));
  214. cur = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, prev[cur]));
  215. }
  216. G_free(prev);
  217. G_free(vis);
  218. return list->n_values;
  219. }