pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
pgr_allpairs.hpp
1 /*PGR-GNU*****************************************************************
2 File: pgr_allpairs.hpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 Mail: vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 // http://www.cs.rpi.edu/~musser/archive/2005/gsd/restricted/FloydWarshall/FloydWarshall.pdf
26 
27 #ifndef SRC_ALLPAIRS_SRC_PGR_ALLPAIRS_HPP_
28 #define SRC_ALLPAIRS_SRC_PGR_ALLPAIRS_HPP_
29 
30 
31 #include <deque>
32 #include <vector>
33 #include <set>
34 #include <limits>
35 
36 #include <boost/config.hpp>
37 
38 #include <boost/graph/adjacency_list.hpp>
39 #include <boost/property_map/property_map.hpp>
40 #include <boost/graph/johnson_all_pairs_shortest.hpp>
41 #include <boost/graph/floyd_warshall_shortest.hpp>
42 
43 
44 extern "C" {
45 #include "./../../common/src/pgr_types.h"
46 }
47 
48 #include "../../common/src/memory_func.hpp"
49 #include "./../../common/src/basePath_SSEC.hpp"
50 #include "./../../common/src/baseGraph.hpp"
51 
52 template < class G > class Pgr_allpairs;
53 
54 // user's functions
55 template < class G >
56 void
57 pgr_johnson(G &graph, std::vector< Matrix_cell_t> &rows) {
58  Pgr_allpairs< G > fn_johnson;
59  fn_johnson.johnson(graph, rows);
60 }
61 
62 template < class G >
63 void
64 pgr_floydWarshall(G &graph, std::vector< Matrix_cell_t> &rows) {
65  Pgr_allpairs< G > fn_floydWarshall;
66  fn_floydWarshall.floydWarshall(graph, rows);
67 }
68 
69 // for postgres
70 template < class G >
71 void
72 pgr_johnson(
73  G &graph,
74  size_t &result_tuple_count,
75  Matrix_cell_t **postgres_rows) {
76  Pgr_allpairs< G > fn_johnson;
77  fn_johnson.johnson(graph, result_tuple_count, postgres_rows);
78 }
79 
80 
81 template < class G >
82 void
83 pgr_floydWarshall(
84  G &graph,
85  size_t &result_tuple_count,
86  Matrix_cell_t **postgres_rows) {
87  Pgr_allpairs< G > fn_floydWarshall;
88  fn_floydWarshall.floydWarshall(graph, result_tuple_count, postgres_rows);
89 }
90 
91 
92 // template class
93 template < class G >
94 class Pgr_allpairs {
95  // defualt constructors and destructors
96  /*
97  Matrix_cell_t description:
98  int64_t from_vid;
99  int64_t to_vid;
100  float8 cost;
101  */
102  public:
103  void floydWarshall(
104  G &graph,
105  size_t &result_tuple_count,
106  Matrix_cell_t **postgres_rows);
107 
108 
109  void floydWarshall(
110  G &graph,
111  std::vector< Matrix_cell_t> &rows);
112 
113  void johnson(
114  G &graph,
115  size_t &result_tuple_count,
116  Matrix_cell_t **postgres_rows);
117 
118 
119  void johnson(
120  G &graph,
121  std::vector< Matrix_cell_t> &rows);
122 
123  private:
124  void make_matrix(
125  size_t v_size,
126  std::vector< std::vector<double>> &matrix) const;
127 
128  void make_result(
129  const G &graph,
130  const std::vector< std::vector<double> > &matrix,
131  size_t &result_tuple_count,
132  Matrix_cell_t **postgres_rows) const;
133 
134  size_t count_rows(
135  const G &graph,
136  const std::vector< std::vector<double> > &matrix) const;
137 
138  void make_result(
139  G &graph,
140  std::vector< std::vector<double> > &matrix,
141  std::vector< Matrix_cell_t> &rows);
142 
143  template <typename T>
144  struct inf_plus {
145  T operator()(const T& a, const T& b) const {
146  T inf = std::numeric_limits<T>::max();
147  if (a == inf || b == inf)
148  return inf;
149  return a + b;
150  }
151  };
152 };
153 
154 /*
155  * PUBLIC FUNCTIONS
156  */
157 
158 template < class G >
160  G &graph,
161  size_t &result_tuple_count,
162  Matrix_cell_t **postgres_rows) {
163  std::vector< std::vector<double>> matrix;
164  make_matrix(graph.num_vertices(), matrix);
165  inf_plus<double> combine;
166  boost::floyd_warshall_all_pairs_shortest_paths(
167  graph.graph,
168  matrix,
169  weight_map(get(&boost_edge_t::cost, graph.graph)).
170  distance_combine(combine).
171  distance_inf(std::numeric_limits<double>::max()).
172  distance_zero(0));
173 
174  make_result(graph, matrix, result_tuple_count, postgres_rows);
175 }
176 
177 
178 template < class G >
180  G &graph,
181  std::vector< Matrix_cell_t> &rows) {
182  std::vector< std::vector<double>> matrix;
183  make_matrix(boost::num_vertices(graph.graph), matrix);
184  inf_plus<double> combine;
185  boost::floyd_warshall_all_pairs_shortest_paths(
186  graph.graph,
187  matrix,
188  weight_map(get(&boost_edge_t::cost, graph.graph)).
189  distance_combine(combine).
190  distance_inf(std::numeric_limits<double>::max()).
191  distance_zero(0));
192 
193  make_result(graph, matrix, rows);
194 }
195 
196 template < class G >
198  G &graph,
199  size_t &result_tuple_count,
200  Matrix_cell_t **postgres_rows) {
201  std::vector< std::vector<double>> matrix;
202  make_matrix(graph.num_vertices(), matrix);
203  inf_plus<double> combine;
204  boost::johnson_all_pairs_shortest_paths(
205  graph.graph,
206  matrix,
207  weight_map(get(&boost_edge_t::cost, graph.graph)).
208  distance_combine(combine).
209  distance_inf(std::numeric_limits<double>::max()).
210  distance_zero(0));
211 
212  make_result(graph, matrix, result_tuple_count, postgres_rows);
213 }
214 
215 
216 template < class G >
218  G &graph,
219  std::vector< Matrix_cell_t> &rows) {
220  std::vector< std::vector<double>> matrix;
221  make_matrix(boost::num_vertices(graph.graph), matrix);
222  inf_plus<double> combine;
223  boost::johnson_all_pairs_shortest_paths(
224  graph.graph,
225  matrix,
226  weight_map(get(&boost_edge_t::cost, graph.graph)).
227  distance_combine(combine).
228  distance_inf(std::numeric_limits<double>::max()).
229  distance_zero(0));
230 
231  make_result(graph, matrix, rows);
232 }
233 
234 
235 
236 
237 /*
238  * PRIVATE FUNCTIONS
239  */
240 
241 template < class G >
242 void
244  size_t v_size,
245  std::vector< std::vector<double>> &matrix) const {
246  matrix.resize(v_size);
247  for (size_t i=0; i < v_size; i++)
248  matrix[i].resize(v_size);
249 }
250 
251 template < class G >
252 size_t
254  const G &graph,
255  const std::vector< std::vector<double> > &matrix) const {
256  size_t result_tuple_count = 0;
257  for (size_t i = 0; i < graph.num_vertices(); i++) {
258  for (size_t j = 0; j < graph.num_vertices(); j++) {
259  if (i == j) continue;
260  if (matrix[i][j] != std::numeric_limits<double>::max()) {
261  result_tuple_count++;
262  } // if
263  } // for j
264  } // for i
265  return result_tuple_count;
266 }
267 
268 // for postgres
269 template < class G >
270 void
272  const G &graph,
273  const std::vector< std::vector<double> > &matrix,
274  size_t &result_tuple_count,
275  Matrix_cell_t **postgres_rows) const {
276  result_tuple_count = count_rows(graph, matrix);
277  *postgres_rows = get_memory(result_tuple_count, (*postgres_rows));
278 
279 
280  size_t seq = 0;
281  for (size_t i = 0; i < graph.num_vertices(); i++) {
282  for (size_t j = 0; j < graph.num_vertices(); j++) {
283  if (i == j) continue;
284  if (matrix[i][j] != std::numeric_limits<double>::max()) {
285  (*postgres_rows)[seq].from_vid = graph.graph[i].id;
286  (*postgres_rows)[seq].to_vid = graph.graph[j].id;
287  (*postgres_rows)[seq].cost = matrix[i][j];
288  seq++;
289  } // if
290  } // for j
291  } // for i
292 }
293 
294 
295 template < class G >
296 void
298  G &graph,
299  std::vector< std::vector<double> > &matrix,
300  std::vector< Matrix_cell_t> &rows) {
301  size_t count = count_rows(graph, matrix);
302  rows.resize(count);
303  size_t seq = 0;
304 
305  for (size_t i = 0; i < graph.num_vertices(); i++) {
306  for (size_t j = 0; j < graph.num_vertices(); j++) {
307  if (matrix[i][j] != std::numeric_limits<double>::max()) {
308  rows[seq] =
309  {graph.graph[i].id, graph.graph[j].id, matrix[i][j]};
310  seq++;
311  } // if
312  } // for j
313  } // for i
314 }
315 
316 #endif // SRC_ALLPAIRS_SRC_PGR_ALLPAIRS_HPP_