PGROUTING  3.2
binaryBreadthFirstSearch_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: binaryBreadthFirstSearch_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2019 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2019 Gudesa Venkata Sai Akhil
11 
12 
13 ------
14 
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 
29 ********************************************************************PGR-GNU*/
30 
32 
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 #include <algorithm>
37 #include <string>
38 #include <set>
39 
41 
42 #include "cpp_common/pgr_alloc.hpp"
43 #include "cpp_common/pgr_assert.h"
44 
45 template < class G >
46 std::deque< Path >
48  G &graph,
49  std::vector <pgr_combination_t> &combinations,
50  std::vector < int64_t > sources,
51  std::vector < int64_t > targets) {
52  std::sort(sources.begin(), sources.end());
53  sources.erase(
54  std::unique(sources.begin(), sources.end()),
55  sources.end());
56 
57  std::sort(targets.begin(), targets.end());
58  targets.erase(
59  std::unique(targets.begin(), targets.end()),
60  targets.end());
61 
63  auto paths = combinations.empty() ?
64  fn_binaryBreadthFirstSearch.binaryBreadthFirstSearch(graph, sources, targets)
65  : fn_binaryBreadthFirstSearch.binaryBreadthFirstSearch(graph, combinations);
66 
67  return paths;
68 }
69 
70 const size_t MAX_UNIQUE_EDGE_COSTS = 2;
71 const char COST_ERR_MSG[] = "Graph Condition Failed: Graph should have atmost two distinct non-negative edge costs! "
72  "If there are exactly two distinct edge costs, one of them must equal zero!";
73 template < class G >
74 bool
75 costCheck(G &graph) {
76  typedef typename G::E E;
77  typedef typename G::E_i E_i;
78 
79  auto edges = boost::edges(graph.graph);
80  E e;
81  E_i out_i;
82  E_i out_end;
83  std::set<double> cost_set;
84  for (boost::tie(out_i, out_end) = edges;
85  out_i != out_end; ++out_i) {
86  e = *out_i;
87  cost_set.insert(graph[e].cost);
88 
89  if (cost_set.size() > MAX_UNIQUE_EDGE_COSTS) {
90  return false;
91  }
92  }
93 
94  if (cost_set.size() == 2) {
95  if (*cost_set.begin() != 0.0) {
96  return false;
97  }
98  }
99 
100  return true;
101 }
102 
103 
104 
105 void
107  pgr_edge_t *data_edges,
108  size_t total_edges,
109  pgr_combination_t *combinations,
110  size_t total_combinations,
111  int64_t *start_vidsArr,
112  size_t size_start_vidsArr,
113  int64_t *end_vidsArr,
114  size_t size_end_vidsArr,
115  bool directed,
116 
117  General_path_element_t **return_tuples,
118  size_t *return_count,
119  char ** log_msg,
120  char ** notice_msg,
121  char ** err_msg) {
122  std::ostringstream log;
123  std::ostringstream err;
124  std::ostringstream notice;
125 
126  try {
127  pgassert(total_edges != 0);
128  pgassert(!(*log_msg));
129  pgassert(!(*notice_msg));
130  pgassert(!(*err_msg));
131  pgassert(!(*return_tuples));
132  pgassert(*return_count == 0);
133  pgassert(data_edges);
134  pgassert((start_vidsArr && end_vidsArr) || combinations);
135  pgassert((size_start_vidsArr && size_end_vidsArr) || total_combinations);
136 
137  graphType gType = directed? DIRECTED: UNDIRECTED;
138 
139  // log << "Inserting vertices into a c++ vector structure";
140  std::vector<int64_t>
141  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
142  std::vector< int64_t >
143  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
144  std::vector< pgr_combination_t >
145  combinations_vector(combinations, combinations + total_combinations);
146 
147  std::deque< Path >paths;
148  if (directed) {
149  // log << "\nWorking with directed Graph";
150  pgrouting::DirectedGraph digraph(gType);
151  digraph.insert_edges(data_edges, total_edges);
152 
153  if (!(costCheck(digraph))) {
154  err << COST_ERR_MSG;
155  *err_msg = pgr_msg(err.str().c_str());
156  return;
157  }
159  digraph,
160  combinations_vector,
161  start_vertices,
162  end_vertices);
163 
164  } else {
165  // log << "\nWorking with Undirected Graph";
166  pgrouting::UndirectedGraph undigraph(gType);
167  undigraph.insert_edges(data_edges, total_edges);
168 
169  if (!(costCheck(undigraph))) {
170  err << COST_ERR_MSG;
171  *err_msg = pgr_msg(err.str().c_str());
172  return;
173  }
174 
176  undigraph,
177  combinations_vector,
178  start_vertices,
179  end_vertices);
180  }
181 
182  size_t count(0);
183  count = count_tuples(paths);
184 
185  if (count == 0) {
186  (*return_tuples) = NULL;
187  (*return_count) = 0;
188  notice <<
189  "No paths found";
190  *log_msg = pgr_msg(notice.str().c_str());
191  return;
192  }
193 
194  (*return_tuples) = pgr_alloc(count, (*return_tuples));
195  log << "\nConverting a set of paths into the tuples";
196  (*return_count) = (collapse_paths(return_tuples, paths));
197 
198  *log_msg = log.str().empty()?
199  *log_msg :
200  pgr_msg(log.str().c_str());
201  *notice_msg = notice.str().empty()?
202  *notice_msg :
203  pgr_msg(notice.str().c_str());
204  } catch (AssertFailedException &except) {
205  (*return_tuples) = pgr_free(*return_tuples);
206  (*return_count) = 0;
207  err << except.what();
208  *err_msg = pgr_msg(err.str().c_str());
209  *log_msg = pgr_msg(log.str().c_str());
210  } catch (std::exception &except) {
211  (*return_tuples) = pgr_free(*return_tuples);
212  (*return_count) = 0;
213  err << except.what();
214  *err_msg = pgr_msg(err.str().c_str());
215  *log_msg = pgr_msg(log.str().c_str());
216  } catch(...) {
217  (*return_tuples) = pgr_free(*return_tuples);
218  (*return_count) = 0;
219  err << "Caught unknown exception!";
220  *err_msg = pgr_msg(err.str().c_str());
221  *log_msg = pgr_msg(log.str().c_str());
222  }
223 }
do_pgr_binaryBreadthFirstSearch
void do_pgr_binaryBreadthFirstSearch(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: binaryBreadthFirstSearch_driver.cpp:106
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
pgrouting::functions::Pgr_binaryBreadthFirstSearch
Definition: pgr_binaryBreadthFirstSearch.hpp:44
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::functions::Pgr_binaryBreadthFirstSearch::binaryBreadthFirstSearch
std::deque< Path > binaryBreadthFirstSearch(G &graph, std::vector< int64_t > start_vertex, std::vector< int64_t > end_vertex)
Definition: pgr_binaryBreadthFirstSearch.hpp:52
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_binaryBreadthFirstSearch
std::deque< Path > pgr_binaryBreadthFirstSearch(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets)
Definition: binaryBreadthFirstSearch_driver.cpp:47
pgr_alloc.hpp
pgrouting::alphashape::E
boost::graph_traits< BG >::edge_descriptor E
Definition: pgr_alphaShape.h:57
pgr_combination_t
Definition: pgr_combination_t.h:43
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgr_binaryBreadthFirstSearch.hpp
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
binaryBreadthFirstSearch_driver.h
MAX_UNIQUE_EDGE_COSTS
const size_t MAX_UNIQUE_EDGE_COSTS
Definition: binaryBreadthFirstSearch_driver.cpp:70
COST_ERR_MSG
const char COST_ERR_MSG[]
Definition: binaryBreadthFirstSearch_driver.cpp:71
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
costCheck
bool costCheck(G &graph)
Definition: binaryBreadthFirstSearch_driver.cpp:75
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139