File: path.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (319 lines) | stat: -rw-r--r-- 9,019 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*!
   \file vector/neta/path.c

   \brief Network Analysis library - shortest path

   Shortest paths from a set of nodes.

   (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author Daniel Bundala (Google Summer of Code 2009)
   \author Markus Metz
 */

#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include <grass/dgl/graph.h>
#include <grass/neta.h>

/*!
   \brief Computes shortest paths to every node from nodes in "from".

   Array "dst" contains the cost of the path or -1 if the node is not
   reachable. Prev contains edges from predecessor along the shortest
   path.

   \param graph input graph
   \param from list of 'from' positions
   \param[out] dst array of costs to reach nodes
   \param[out] prev array of edges from predecessor along the shortest path

   \return 0 on success
   \return -1 on failure
 */
int NetA_distance_from_points(dglGraph_s *graph, struct ilist *from, int *dst,
                              dglInt32_t **prev)
{
    int i, nnodes;
    dglHeap_s heap;
    int have_node_costs;
    dglInt32_t ncost;

    nnodes = dglGet_NodeCount(graph);
    dglEdgesetTraverser_s et;

    /* initialize costs and edge list */
    for (i = 1; i <= nnodes; i++) {
        dst[i] = -1;
        prev[i] = NULL;
    }

    ncost = 0;
    have_node_costs = dglGet_NodeAttrSize(graph);

    dglHeapInit(&heap);

    for (i = 0; i < from->n_values; i++) {
        int v = from->value[i];

        if (dst[v] == 0)
            continue; /* ignore duplicates */
        dst[v] = 0;   /* make sure all from nodes are processed first */
        dglHeapData_u heap_data;

        heap_data.ul = v;
        dglHeapInsertMin(&heap, 0, ' ', heap_data);
    }
    while (1) {
        dglInt32_t v, dist;
        dglHeapNode_s heap_node;
        dglHeapData_u heap_data;
        dglInt32_t *edge;
        dglInt32_t *node;

        if (!dglHeapExtractMin(&heap, &heap_node))
            break;
        v = heap_node.value.ul;
        dist = heap_node.key;
        if (dst[v] < dist)
            continue;

        node = dglGetNode(graph, v);

        if (have_node_costs && prev[v]) {
            memcpy(&ncost, dglNodeGet_Attr(graph, node), sizeof(ncost));
            if (ncost > 0)
                dist += ncost;
            /* do not go through closed nodes */
            if (ncost < 0)
                continue;
        }

        dglEdgeset_T_Initialize(&et, graph, dglNodeGet_OutEdgeset(graph, node));

        for (edge = dglEdgeset_T_First(&et); edge;
             edge = dglEdgeset_T_Next(&et)) {
            dglInt32_t *to = dglEdgeGet_Tail(graph, edge);
            dglInt32_t to_id = dglNodeGet_Id(graph, to);
            dglInt32_t d = dglEdgeGet_Cost(graph, edge);

            if (dst[to_id] < 0 || dst[to_id] > dist + d) {
                dst[to_id] = dist + d;
                prev[to_id] = edge;
                heap_data.ul = to_id;
                dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
            }
        }

        dglEdgeset_T_Release(&et);
    }

    dglHeapFree(&heap, NULL);

    return 0;
}

/*!
   \brief Computes shortest paths from every node to nodes in "to".

   Array "dst" contains the cost of the path or -1 if the node is not
   reachable. Nxt contains edges from successor along the shortest
   path. This method does reverse search starting with "to" nodes and
   going backward.

   \param graph input graph
   \param to list of 'to' positions
   \param[out] dst array of costs to reach nodes
   \param[out] nxt array of edges from successor along the shortest path

   \return 0 on success
   \return -1 on failure
 */
int NetA_distance_to_points(dglGraph_s *graph, struct ilist *to, int *dst,
                            dglInt32_t **nxt)
{
    int i, nnodes;
    dglHeap_s heap;
    dglEdgesetTraverser_s et;
    int have_node_costs;
    dglInt32_t ncost;

    nnodes = dglGet_NodeCount(graph);

    /* initialize costs and edge list */
    for (i = 1; i <= nnodes; i++) {
        dst[i] = -1;
        nxt[i] = NULL;
    }

    if (graph->Version < 2) {
        G_warning("Directed graph must be version 2 or 3 for "
                  "NetA_distance_to_points()");
        return -1;
    }

    ncost = 0;
    have_node_costs = dglGet_NodeAttrSize(graph);

    dglHeapInit(&heap);

    for (i = 0; i < to->n_values; i++) {
        int v = to->value[i];

        if (dst[v] == 0)
            continue; /* ignore duplicates */
        dst[v] = 0;   /* make sure all to nodes are processed first */
        dglHeapData_u heap_data;

        heap_data.ul = v;
        dglHeapInsertMin(&heap, 0, ' ', heap_data);
    }
    while (1) {
        dglInt32_t v, dist;
        dglHeapNode_s heap_node;
        dglHeapData_u heap_data;
        dglInt32_t *edge;
        dglInt32_t *node;

        if (!dglHeapExtractMin(&heap, &heap_node))
            break;
        v = heap_node.value.ul;
        dist = heap_node.key;
        if (dst[v] < dist)
            continue;

        node = dglGetNode(graph, v);

        if (have_node_costs && nxt[v]) {
            memcpy(&ncost, dglNodeGet_Attr(graph, node), sizeof(ncost));
            if (ncost > 0)
                dist += ncost;
            /* do not go through closed nodes */
            if (ncost < 0)
                continue;
        }

        dglEdgeset_T_Initialize(&et, graph, dglNodeGet_InEdgeset(graph, node));

        for (edge = dglEdgeset_T_First(&et); edge;
             edge = dglEdgeset_T_Next(&et)) {
            dglInt32_t *from = dglEdgeGet_Head(graph, edge);
            dglInt32_t from_id = dglNodeGet_Id(graph, from);
            dglInt32_t d = dglEdgeGet_Cost(graph, edge);

            if (dst[from_id] < 0 || dst[from_id] > dist + d) {
                dst[from_id] = dist + d;
                nxt[from_id] = edge;
                heap_data.ul = from_id;
                dglHeapInsertMin(&heap, dist + d, ' ', heap_data);
            }
        }

        dglEdgeset_T_Release(&et);
    }

    dglHeapFree(&heap, NULL);

    return 0;
}

/*!
   \brief Find a path (minimum number of edges) from 'from' to 'to'
   using only edges flagged as valid in 'edges'. Edge costs are not
   considered. Closed nodes are not traversed.

   Precisely, edge with id I is used if edges[abs(i)] == 1. List
   stores the indices of lines on the path. The method returns the
   number of edges or -1 if no path exists.

   \param graph input graph
   \param from 'from' position
   \param to 'to' position
   \param edges array of edges indicating whether an edge should be used
   \param[out] list list of edges

   \return number of edges
   \return -1 on failure
 */
int NetA_find_path(dglGraph_s *graph, int from, int to, int *edges,
                   struct ilist *list)
{
    dglInt32_t **prev, *queue;
    dglEdgesetTraverser_s et;
    char *vis;
    int begin, end, cur, nnodes;
    int have_node_costs;
    dglInt32_t ncost;

    nnodes = dglGet_NodeCount(graph);
    prev = (dglInt32_t **)G_calloc(nnodes + 1, sizeof(dglInt32_t *));
    queue = (dglInt32_t *)G_calloc(nnodes + 1, sizeof(dglInt32_t));
    vis = (char *)G_calloc(nnodes + 1, sizeof(char));
    if (!prev || !queue || !vis) {
        G_fatal_error(_("Out of memory"));
        return -1;
    }
    Vect_reset_list(list);

    ncost = 0;
    have_node_costs = dglGet_NodeAttrSize(graph);

    begin = 0;
    end = 1;
    vis[from] = 'y';
    queue[0] = from;
    prev[from] = NULL;
    while (begin != end) {
        dglInt32_t vertex = queue[begin++];
        dglInt32_t *edge = NULL, *node;

        if (vertex == to)
            break;

        /* do not go through closed nodes */
        if (have_node_costs && prev[vertex]) {
            memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)),
                   sizeof(ncost));
            if (ncost < 0)
                continue;
        }

        node = dglGetNode(graph, vertex);

        dglEdgeset_T_Initialize(&et, graph, dglNodeGet_OutEdgeset(graph, node));
        for (edge = dglEdgeset_T_First(&et); edge;
             edge = dglEdgeset_T_Next(&et)) {
            dglInt32_t edge_id = labs(dglEdgeGet_Id(graph, edge));
            dglInt32_t node_id =
                dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
            if (edges[edge_id] && !vis[node_id]) {
                vis[node_id] = 'y';
                prev[node_id] = edge;
                queue[end++] = node_id;
            }
        }
        dglEdgeset_T_Release(&et);
    }
    G_free(queue);
    if (!vis[to]) {
        G_free(prev);
        G_free(vis);
        return -1;
    }

    cur = to;
    while (prev[cur] != NULL) {
        Vect_list_append(list, labs(dglEdgeGet_Id(graph, prev[cur])));
        cur = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, prev[cur]));
    }

    G_free(prev);
    G_free(vis);
    return list->n_values;
}