GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
allpairs.c
Go to the documentation of this file.
1 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <grass/gis.h>
20 #include <grass/Vect.h>
21 #include <grass/glocale.h>
22 #include <grass/dgl/graph.h>
23 
38 int NetA_allpairs(dglGraph_s * graph, dglInt32_t ** dist)
39 {
40  int nnodes, i, j, k, indices;
43  dglInt32_t *node;
44 
45  nnodes = dglGet_NodeCount(graph);
46  dglInt32_t *node_indices;
47 
48  node_indices = (dglInt32_t *) G_calloc(nnodes, sizeof(dglInt32_t));
49  if (!node_indices) {
50  G_fatal_error(_("Out of memory"));
51  return -1;
52  }
53  G_message(_("Computing all pairs shortest paths..."));
55  for (i = 0; i <= nnodes; i++)
56  for (j = 0; j <= nnodes; j++)
57  dist[i][j] = -1;
58  dglNode_T_Initialize(&nt, graph);
59  indices = 0;
60  for (node = dglNode_T_First(&nt); node; node = dglNode_T_Next(&nt)) {
61  dglInt32_t node_id = dglNodeGet_Id(graph, node);
62 
63  node_indices[indices++] = node_id;
64  dglInt32_t *edge;
65 
66  dglEdgeset_T_Initialize(&et, graph,
67  dglNodeGet_OutEdgeset(graph, node));
68  for (edge = dglEdgeset_T_First(&et); edge;
69  edge = dglEdgeset_T_Next(&et))
70  if (dglEdgeGet_Id(graph, edge) < 0) /*ignore backward edges */
71  dist[node_id][dglNodeGet_Id
72  (graph, dglEdgeGet_Tail(graph, edge))] =
73  dglEdgeGet_Cost(graph, edge);
75  }
76  dglNode_T_Release(&nt);
77  for (k = 0; k < indices; k++) {
78  dglInt32_t k_index = node_indices[k];
79 
80  G_percent(k + 1, indices, 1);
81  for (i = 0; i < indices; i++) {
82  dglInt32_t i_index = node_indices[i];
83 
84  if (dist[i_index][k_index] == -1)
85  continue; /*no reason to proceed along infinite path */
86  for (j = 0; j < indices; j++) {
87  dglInt32_t j_index = node_indices[j];
88 
89  if (dist[k_index][j_index] != -1 &&
90  (dist[i_index][k_index] + dist[k_index][j_index] <
91  dist[i_index][j_index] ||
92  dist[i_index][j_index] == -1)) {
93  dist[i_index][j_index] =
94  dist[i_index][k_index] + dist[k_index][j_index];
95  }
96  }
97  }
98  }
99 
100  G_free(node_indices);
101  return 0;
102 }
dglInt32_t * dglEdgeGet_Tail(dglGraph_s *pGraph, dglInt32_t *pnEdge)
Definition: dglib/graph.c:494
dglInt32_t dglNodeGet_Id(dglGraph_s *pGraph, dglInt32_t *pnNode)
Definition: dglib/graph.c:213
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int dglEdgeset_T_Initialize(dglEdgesetTraverser_s *pT, dglGraph_s *pGraph, dglInt32_t *pnEdgeset)
Definition: dglib/graph.c:1499
int dglNode_T_Initialize(dglNodeTraverser_s *pT, dglGraph_s *pGraph)
Definition: dglib/graph.c:1356
void dglNode_T_Release(dglNodeTraverser_s *pT)
Definition: dglib/graph.c:1371
dglInt32_t dglEdgeGet_Id(dglGraph_s *pGraph, dglInt32_t *pnEdge)
Definition: dglib/graph.c:438
int G_percent(long n, long d, int s)
Print percent complete messages.
Definition: percent.c:63
void dglEdgeset_T_Release(dglEdgesetTraverser_s *pT)
Definition: dglib/graph.c:1515
int G_percent_reset(void)
Reset G_percent() to 0%; do not add newline.
Definition: percent.c:152
dglInt32_t * dglEdgeset_T_First(dglEdgesetTraverser_s *pT)
Definition: dglib/graph.c:1519
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
long dglInt32_t
Definition: type.h:37
dglInt32_t * dglNode_T_First(dglNodeTraverser_s *pT)
Definition: dglib/graph.c:1387
dglInt32_t * dglNodeGet_OutEdgeset(dglGraph_s *pGraph, dglInt32_t *pnNode)
Definition: dglib/graph.c:170
dglInt32_t dglEdgeGet_Cost(dglGraph_s *pGraph, dglInt32_t *pnEdge)
Definition: dglib/graph.c:418
int dglGet_NodeCount(dglGraph_s *pgraph)
Definition: dglib/graph.c:1241
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
dglInt32_t * dglNode_T_Next(dglNodeTraverser_s *pT)
Definition: dglib/graph.c:1402
int NetA_allpairs(dglGraph_s *graph, dglInt32_t **dist)
Stores the directed distance between every pair.
Definition: allpairs.c:38
dglInt32_t * dglEdgeset_T_Next(dglEdgesetTraverser_s *pT)
Definition: dglib/graph.c:1534