PGROUTING  2.6
pgr_allpairs.hpp
Go to the documentation of this file.
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 INCLUDE_ALLPAIRS_PGR_ALLPAIRS_HPP_
28 #define INCLUDE_ALLPAIRS_PGR_ALLPAIRS_HPP_
29 #pragma once
30 
31 
32 #include <boost/config.hpp>
33 #include <boost/graph/adjacency_list.hpp>
34 #include <boost/property_map/property_map.hpp>
35 #include <boost/graph/johnson_all_pairs_shortest.hpp>
36 #include <boost/graph/floyd_warshall_shortest.hpp>
37 
38 
39 #include <deque>
40 #include <vector>
41 #include <set>
42 #include <limits>
43 
44 
47 
48 // TODO(vicky) don't keep it here
49 #include "cpp_common/pgr_alloc.hpp"
50 
51 template < class G > class Pgr_allpairs;
52 
53 // user's functions
54 template < class G >
55 void
56 pgr_johnson(G &graph, std::vector< Matrix_cell_t> &rows) {
57  Pgr_allpairs< G > fn_johnson;
58  fn_johnson.johnson(graph, rows);
59 }
60 
61 template < class G >
62 void
63 pgr_floydWarshall(G &graph, std::vector< Matrix_cell_t> &rows) {
64  Pgr_allpairs< G > fn_floydWarshall;
65  fn_floydWarshall.floydWarshall(graph, rows);
66 }
67 
68 // for postgres
69 template < class G >
70 void
72  G &graph,
73  size_t &result_tuple_count,
74  Matrix_cell_t **postgres_rows) {
75  Pgr_allpairs< G > fn_johnson;
76  fn_johnson.johnson(graph, result_tuple_count, postgres_rows);
77 }
78 
79 
80 template < class G >
81 void
83  G &graph,
84  size_t &result_tuple_count,
85  Matrix_cell_t **postgres_rows) {
86  Pgr_allpairs< G > fn_floydWarshall;
87  fn_floydWarshall.floydWarshall(graph, result_tuple_count, postgres_rows);
88 }
89 
90 
91 // template class
92 template < class G >
93 class Pgr_allpairs {
94  public:
96  G &graph,
97  size_t &result_tuple_count,
98  Matrix_cell_t **postgres_rows) {
99  std::vector< std::vector<double>> matrix;
100  make_matrix(graph.num_vertices(), matrix);
101  inf_plus<double> combine;
102  boost::floyd_warshall_all_pairs_shortest_paths(
103  graph.graph,
104  matrix,
105  weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
106  distance_combine(combine).
107  distance_inf((std::numeric_limits<double>::max)()).
108  distance_zero(0));
109 
110  make_result(graph, matrix, result_tuple_count, postgres_rows);
111  }
112 
114  G &graph,
115  std::vector< Matrix_cell_t> &rows) {
116  std::vector< std::vector<double>> matrix;
117  make_matrix(graph.num_vertices(), matrix);
118  inf_plus<double> combine;
119  boost::floyd_warshall_all_pairs_shortest_paths(
120  graph.graph,
121  matrix,
122  weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
123  distance_combine(combine).
124  distance_inf((std::numeric_limits<double>::max)()).
125  distance_zero(0));
126 
127  make_result(graph, matrix, rows);
128  }
129 
130  void johnson(
131  G &graph,
132  size_t &result_tuple_count,
133  Matrix_cell_t **postgres_rows) {
134  std::vector< std::vector<double>> matrix;
135  make_matrix(graph.num_vertices(), matrix);
136  inf_plus<double> combine;
137  boost::johnson_all_pairs_shortest_paths(
138  graph.graph,
139  matrix,
140  weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
141  distance_combine(combine).
142  distance_inf((std::numeric_limits<double>::max)()).
143  distance_zero(0));
144 
145  make_result(graph, matrix, result_tuple_count, postgres_rows);
146  }
147 
148 
149  void johnson(
150  G &graph,
151  std::vector< Matrix_cell_t> &rows) {
152  std::vector< std::vector<double>> matrix;
153  make_matrix(graph.num_vertices(), matrix);
154  inf_plus<double> combine;
155  boost::johnson_all_pairs_shortest_paths(
156  graph.graph,
157  matrix,
158  weight_map(get(&pgrouting::Basic_edge::cost, graph.graph)).
159  distance_combine(combine).
160  distance_inf((std::numeric_limits<double>::max)()).
161  distance_zero(0));
162 
163  make_result(graph, matrix, rows);
164  }
165 
166  private:
168  size_t v_size,
169  std::vector< std::vector<double>> &matrix) const {
170  // TODO(vicky) in one step
171  matrix.resize(v_size);
172  for (size_t i=0; i < v_size; i++)
173  matrix[i].resize(v_size);
174  }
175 
177  const G &graph,
178  const std::vector< std::vector<double> > &matrix,
179  size_t &result_tuple_count,
180  Matrix_cell_t **postgres_rows) const {
181  result_tuple_count = count_rows(graph, matrix);
182  *postgres_rows = pgr_alloc(result_tuple_count, (*postgres_rows));
183 
184 
185  size_t seq = 0;
186  for (typename G::V v_i = 0; v_i < graph.num_vertices(); v_i++) {
187  for (typename G::V v_j = 0; v_j < graph.num_vertices(); v_j++) {
188  if (v_i == v_j) continue;
189  if (matrix[v_i][v_j] != (std::numeric_limits<double>::max)()) {
190  (*postgres_rows)[seq].from_vid = graph[v_i].id;
191  (*postgres_rows)[seq].to_vid = graph[v_j].id;
192  (*postgres_rows)[seq].cost = matrix[v_i][v_j];
193  seq++;
194  } // if
195  } // for j
196  } // for i
197  }
198 
199 
200  size_t count_rows(
201  const G &graph,
202  const std::vector< std::vector<double> > &matrix) const {
203  size_t result_tuple_count = 0;
204  for (size_t i = 0; i < graph.num_vertices(); i++) {
205  for (size_t j = 0; j < graph.num_vertices(); j++) {
206  if (i == j) continue;
207  if (matrix[i][j] != (std::numeric_limits<double>::max)()) {
208  result_tuple_count++;
209  } // if
210  } // for j
211  } // for i
212  return result_tuple_count;
213  }
214 
216  G &graph,
217  std::vector< std::vector<double> > &matrix,
218  std::vector< Matrix_cell_t> &rows) {
219  size_t count = count_rows(graph, matrix);
220  rows.resize(count);
221  size_t seq = 0;
222 
223  for (typename G::V v_i = 0; v_i < graph.num_vertices(); v_i++) {
224  for (typename G::V v_j = 0; v_j < graph.num_vertices(); v_j++) {
225  if (matrix[v_i][v_j] != (std::numeric_limits<double>::max)()) {
226  rows[seq] =
227  {graph[v_i].id, graph[v_j].id, matrix[v_i][v_j]};
228  seq++;
229  } // if
230  } // for j
231  } // for i
232  }
233 
234  template <typename T>
235  struct inf_plus {
236  T operator()(const T& a, const T& b) const {
237  T inf = (std::numeric_limits<T>::max)();
238  if (a == inf || b == inf)
239  return inf;
240  return a + b;
241  }
242  };
243 };
244 
245 
246 #endif // INCLUDE_ALLPAIRS_PGR_ALLPAIRS_HPP_
size_t count_rows(const G &graph, const std::vector< std::vector< double > > &matrix) const
void johnson(G &graph, size_t &result_tuple_count, Matrix_cell_t **postgres_rows)
void make_matrix(size_t v_size, std::vector< std::vector< double >> &matrix) const
void johnson(G &graph, std::vector< Matrix_cell_t > &rows)
T operator()(const T &a, const T &b) const
void floydWarshall(G &graph, size_t &result_tuple_count, Matrix_cell_t **postgres_rows)
void floydWarshall(G &graph, std::vector< Matrix_cell_t > &rows)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:68
void pgr_floydWarshall(G &graph, std::vector< Matrix_cell_t > &rows)
void make_result(const G &graph, const std::vector< std::vector< double > > &matrix, size_t &result_tuple_count, Matrix_cell_t **postgres_rows) const
void make_result(G &graph, std::vector< std::vector< double > > &matrix, std::vector< Matrix_cell_t > &rows)
void pgr_johnson(G &graph, std::vector< Matrix_cell_t > &rows)