path.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. for (i = 1; i <= nnodes; i++) {
  37. dst[i] = -1;
  38. prev[i] = NULL;
  39. }
  40. dglHeapInit(&heap);
  41. for (i = 0; i < from->n_values; i++) {
  42. int v = from->value[i];
  43. if (dst[v] == 0)
  44. continue; /*ingore duplicates */
  45. dst[v] = 0;
  46. dglHeapData_u heap_data;
  47. heap_data.ul = v;
  48. dglHeapInsertMin(&heap, 0, ' ', heap_data);
  49. }
  50. while (1) {
  51. dglInt32_t v, dist;
  52. dglHeapNode_s heap_node;
  53. dglHeapData_u heap_data;
  54. if (!dglHeapExtractMin(&heap, &heap_node))
  55. break;
  56. v = heap_node.value.ul;
  57. dist = heap_node.key;
  58. if (dst[v] < dist)
  59. continue;
  60. dglInt32_t *edge;
  61. dglEdgeset_T_Initialize(&et, graph,
  62. dglNodeGet_OutEdgeset(graph,
  63. dglGetNode(graph, v)));
  64. for (edge = dglEdgeset_T_First(&et); edge;
  65. edge = dglEdgeset_T_Next(&et)) {
  66. dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
  67. dglInt32_t to_id = dglNodeGet_Id(graph, to);
  68. dglInt32_t d = dglEdgeGet_Cost(graph, edge);
  69. if (dst[to_id] == -1 || dst[to_id] > dist + d) {
  70. dst[to_id] = dist + d;
  71. prev[to_id] = edge;
  72. heap_data.ul = to_id;
  73. dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
  74. }
  75. }
  76. dglEdgeset_T_Release(&et);
  77. }
  78. dglHeapFree(&heap, NULL);
  79. return 0;
  80. }
  81. /*!
  82. \brief Find a path (minimum number of edges) from 'from' to 'to' using only edges in 'edges'.
  83. Precisely, edge with id I is used iff edges[abs(i)] == 1. List
  84. stores the indices of lines on the path. Method return number of
  85. edges or -1 if no path exist.
  86. \param graph input graph
  87. \param from 'from' position
  88. \param to 'to' position
  89. \param edges list of available edges
  90. \param[out] list list of edges
  91. \return number of edges
  92. \return -1 on failure
  93. */
  94. int NetA_find_path(dglGraph_s * graph, int from, int to, int *edges,
  95. struct ilist *list)
  96. {
  97. dglInt32_t **prev, *queue;
  98. dglEdgesetTraverser_s et;
  99. char *vis;
  100. int begin, end, cur, nnodes;
  101. nnodes = dglGet_NodeCount(graph);
  102. prev = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
  103. queue = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
  104. vis = (char *)G_calloc(nnodes + 1, sizeof(char));
  105. if (!prev || !queue || !vis) {
  106. G_fatal_error(_("Out of memory"));
  107. return -1;
  108. }
  109. Vect_reset_list(list);
  110. begin = 0;
  111. end = 1;
  112. vis[from] = 'y';
  113. queue[0] = from;
  114. prev[from] = NULL;
  115. while (begin != end) {
  116. dglInt32_t vertex = queue[begin++];
  117. if (vertex == to)
  118. break;
  119. dglInt32_t *edge, *node = dglGetNode(graph, vertex);
  120. dglEdgeset_T_Initialize(&et, graph,
  121. dglNodeGet_OutEdgeset(graph, node));
  122. for (edge = dglEdgeset_T_First(&et); edge;
  123. edge = dglEdgeset_T_Next(&et)) {
  124. dglInt32_t id = abs(dglEdgeGet_Id(graph, edge));
  125. dglInt32_t to =
  126. dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
  127. if (edges[id] && !vis[to]) {
  128. vis[to] = 'y';
  129. prev[to] = edge;
  130. queue[end++] = to;
  131. }
  132. }
  133. dglEdgeset_T_Release(&et);
  134. }
  135. G_free(queue);
  136. if (!vis[to]) {
  137. G_free(prev);
  138. G_free(vis);
  139. return -1;
  140. }
  141. cur = to;
  142. while (prev[cur] != NULL) {
  143. Vect_list_append(list, abs(dglEdgeGet_Id(graph, prev[cur])));
  144. cur = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, prev[cur]));
  145. }
  146. G_free(prev);
  147. G_free(vis);
  148. return list->n_values;
  149. }