PGROUTING  3.2
pgr_depthFirstSearch.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_depthFirstSearch.hpp
3 
4 Copyright (c) 2020 pgRouting developers
6 
7 Copyright (c) 2020 Ashish Kumar
9 
10 ------
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
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 ********************************************************************PGR-GNU*/
23 
24 #ifndef INCLUDE_TRAVERSAL_PGR_DEPTHFIRSTSEARCH_HPP_
25 #define INCLUDE_TRAVERSAL_PGR_DEPTHFIRSTSEARCH_HPP_
26 #pragma once
27 
28 
29 #include <visitors/dfs_visitor.hpp>
30 #include <boost/graph/depth_first_search.hpp>
31 #include <boost/graph/undirected_dfs.hpp>
32 
33 #include <vector>
34 #include <map>
35 
38 
39 
48 namespace pgrouting {
49 namespace functions {
50 
51 //*************************************************************
52 
53 template < class G >
55  public:
56  typedef typename G::V V;
57  typedef typename G::E E;
58 
80  std::vector < pgr_mst_rt > depthFirstSearch(
81  G &graph,
82  std::vector < int64_t > roots,
83  bool directed,
84  int64_t max_depth) {
85  std::vector < pgr_mst_rt > results;
86 
87  for (auto root : roots) {
88  std::vector < E > visited_order;
89  results.push_back({root, 0, root, -1, 0.0, 0.0});
90 
91  if (graph.has_vertex(root)) {
92  auto v_root(graph.get_V(root));
93 
94  depthFirstSearch_single_vertex(graph, v_root, visited_order, directed, max_depth);
95 
96  auto result = get_results(visited_order, root, max_depth, graph);
97  results.insert(results.end(), result.begin(), result.end());
98  }
99  }
100 
101  return results;
102  }
103 
105 
106  private:
123  G &graph,
124  V root,
125  std::vector < E > &visited_order,
126  bool directed,
127  int64_t max_depth) {
128  using dfs_visitor = visitors::Dfs_visitor < V, E, G >;
129 
130  // Exterior property storage containers
131  std::vector < boost::default_color_type > colors(boost::num_vertices(graph.graph));
132  std::map < E, boost::default_color_type > edge_color;
133 
134  auto i_map = boost::get(boost::vertex_index, graph.graph);
135 
136  // Iterator property maps which record the color of each vertex and edge, respectively
137  auto vertex_color_map = boost::make_iterator_property_map(colors.begin(), i_map);
138  auto edge_color_map = boost::make_assoc_property_map(edge_color);
139 
140  auto vis = dfs_visitor(root, visited_order, max_depth, colors, graph);
141 
142  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
143  CHECK_FOR_INTERRUPTS();
144 
145  try {
146  if (directed) {
147  boost::depth_first_search(graph.graph, vis, vertex_color_map, root);
148  } else {
149  boost::undirected_dfs(graph.graph, vis, vertex_color_map, edge_color_map, root);
150  }
151  } catch(found_goals &) {
152  {}
153  } catch (boost::exception const& ex) {
154  (void)ex;
155  throw;
156  } catch (std::exception &e) {
157  (void)e;
158  throw;
159  } catch (...) {
160  throw;
161  }
162  return true;
163  }
164 
178  template < typename T >
179  std::vector < pgr_mst_rt > get_results(
180  T visited_order,
181  int64_t root,
182  int64_t max_depth,
183  const G &graph) {
184  std::vector < pgr_mst_rt > results;
185 
186  std::vector < double > agg_cost(graph.num_vertices(), 0);
187  std::vector < int64_t > depth(graph.num_vertices(), 0);
188 
189  for (const auto edge : visited_order) {
190  auto u = graph.source(edge);
191  auto v = graph.target(edge);
192 
193  agg_cost[v] = agg_cost[u] + graph[edge].cost;
194  depth[v] = depth[u] + 1;
195 
196  if (max_depth >= depth[v]) {
197  results.push_back({
198  root,
199  depth[v],
200  graph[v].id,
201  graph[edge].id,
202  graph[edge].cost,
203  agg_cost[v]
204  });
205  }
206  }
207  return results;
208  }
209 };
210 } // namespace functions
211 } // namespace pgrouting
212 
213 #endif // INCLUDE_TRAVERSAL_PGR_DEPTHFIRSTSEARCH_HPP_
interruption.h
pgr_base_graph.hpp
pgrouting::functions::Pgr_depthFirstSearch::depthFirstSearch_single_vertex
bool depthFirstSearch_single_vertex(G &graph, V root, std::vector< E > &visited_order, bool directed, int64_t max_depth)
Calls the Boost function.
Definition: pgr_depthFirstSearch.hpp:122
edge
Definition: trsp.h:41
pgrouting::functions::Pgr_depthFirstSearch::E
G::E E
Definition: pgr_depthFirstSearch.hpp:57
pgrouting::alphashape::E
boost::graph_traits< BG >::edge_descriptor E
Definition: pgr_alphaShape.h:57
pgrouting::functions::Pgr_depthFirstSearch
Definition: pgr_depthFirstSearch.hpp:54
pgrouting::functions::Pgr_depthFirstSearch::V
G::V V
Definition: pgr_depthFirstSearch.hpp:56
pgrouting::functions::Pgr_depthFirstSearch::depthFirstSearch
std::vector< pgr_mst_rt > depthFirstSearch(G &graph, std::vector< int64_t > roots, bool directed, int64_t max_depth)
depthFirstSearch function
Definition: pgr_depthFirstSearch.hpp:80
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::visitors::Dfs_visitor
Definition: dfs_visitor.hpp:45
dfs_visitor.hpp
pgrouting::found_goals
exception for visitor termination
Definition: found_goals.hpp:33
pgrouting::alphashape::V
boost::graph_traits< BG >::vertex_descriptor V
Definition: pgr_alphaShape.h:58
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::functions::Pgr_depthFirstSearch::get_results
std::vector< pgr_mst_rt > get_results(T visited_order, int64_t root, int64_t max_depth, const G &graph)
to get the results
Definition: pgr_depthFirstSearch.hpp:179