PGROUTING  3.2
pgrouting::functions::Pgr_kruskal< G > Class Template Reference

#include "pgr_kruskal.hpp"

Inheritance diagram for pgrouting::functions::Pgr_kruskal< G >:
Collaboration diagram for pgrouting::functions::Pgr_kruskal< G >:

Public Member Functions

std::vector< pgr_mst_rtkruskal (G &graph)
 
std::vector< pgr_mst_rtkruskalBFS (G &graph, std::vector< int64_t > roots, int64_t max_depth)
 
std::vector< pgr_mst_rtkruskalDD (G &graph, std::vector< int64_t > roots, double distance)
 
std::vector< pgr_mst_rtkruskalDFS (G &graph, std::vector< int64_t > roots, int64_t max_depth)
 

Protected Types

typedef G::E_i E_i
 

Protected Member Functions

std::vector< pgr_mst_rtbfs_order (const G &graph)
 
void clear ()
 
std::vector< pgr_mst_rtdfs_order (const G &graph)
 
std::vector< pgr_mst_rtmst (const G &graph)
 
std::vector< pgr_mst_rtmstBFS (const G &graph, std::vector< int64_t > roots, int64_t max_depth)
 
std::vector< pgr_mst_rtmstDD (const G &graph, std::vector< int64_t > roots, double distance)
 
std::vector< pgr_mst_rtmstDFS (const G &graph, std::vector< int64_t > roots, int64_t max_depth)
 
std::vector< pgr_mst_rtno_order (const G &graph)
 

Protected Attributes

std::vector< size_t > m_components
 m_components[v]: More...
 
double m_distance
 
bool m_get_component
 
int64_t m_max_depth
 
std::vector< int64_t > m_roots
 
struct pgrouting::functions::Pgr_mst::InSpanning m_spanning_tree
 
std::string m_suffix
 Stores which function is being executed. More...
 
std::vector< int64_t > m_tree_id
 m_tree_id[v]: More...
 

Private Types

typedef G::B_G B_G
 
typedef G::E E
 
typedef G::V V
 

Private Member Functions

std::vector< pgr_mst_rtbfs_ordering (const G &graph)
 
void calculate_component (const G &graph)
 
std::vector< pgr_mst_rtdfs_forest (const G &graph)
 
std::vector< pgr_mst_rtdfs_ordering (const G &graph)
 
void generate_mst (const G &graph)
 
template<typename T >
std::vector< pgr_mst_rtget_results (T order, int64_t p_root, const G &graph)
 
bool no_neg_costs (const G &graph)
 
std::vector< pgr_mst_rtno_ordering (const G &graph)
 

Detailed Description

template<class G>
class pgrouting::functions::Pgr_kruskal< G >

Definition at line 41 of file pgr_kruskal.hpp.

Member Typedef Documentation

◆ B_G

template<class G >
typedef G::B_G pgrouting::functions::Pgr_kruskal< G >::B_G
private

Definition at line 61 of file pgr_kruskal.hpp.

◆ E

template<class G >
typedef G::E pgrouting::functions::Pgr_kruskal< G >::E
private

Definition at line 63 of file pgr_kruskal.hpp.

◆ E_i

template<class G >
typedef G::E_i pgrouting::functions::Pgr_mst< G >::E_i
protectedinherited

Definition at line 53 of file pgr_mst.hpp.

◆ V

template<class G >
typedef G::V pgrouting::functions::Pgr_kruskal< G >::V
private

Definition at line 62 of file pgr_kruskal.hpp.

Member Function Documentation

◆ bfs_order()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::bfs_order ( const G &  graph)
inlineprotectedinherited

Definition at line 76 of file pgr_mst.hpp.

76  {
77  return bfs_ordering(graph);
78  }

References pgrouting::functions::Pgr_mst< G >::bfs_ordering().

Referenced by pgrouting::functions::Pgr_mst< G >::mstBFS().

◆ bfs_ordering()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::bfs_ordering ( const G &  graph)
inlineprivateinherited

Definition at line 322 of file pgr_mst.hpp.

322  {
323  std::vector<pgr_mst_rt> results;
324  /*
325  * order by bfs
326  */
327  boost::filtered_graph<B_G, InSpanning, boost::keep_all>
328  mst(graph.graph, m_spanning_tree, {});
329 
330  calculate_component(graph);
331 
332  std::vector<int64_t> roots;
333  if (!m_roots.empty()) {
334  roots = m_roots;
335  } else {
336  roots = m_tree_id;
337  }
338 
339  using bfs_visitor = visitors::Edges_order_bfs_visitor<E>;
340  for (auto root : roots) {
341  std::vector<E> visited_order;
342  if (graph.has_vertex(root)) {
343  boost::breadth_first_search(mst,
344  graph.get_V(root),
345  visitor(bfs_visitor(visited_order)));
346 
347  auto tree_results = get_results(visited_order, root, graph);
348  results.insert(results.end(), tree_results.begin(), tree_results.end());
349  } else {
350  results.push_back({root, 0, root, -1, 0.0, 0.0});
351  }
352  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
353  CHECK_FOR_INTERRUPTS();
354  }
355 
356  return results;
357  }

References pgrouting::functions::Pgr_mst< G >::calculate_component(), pgrouting::functions::Pgr_mst< G >::get_results(), pgrouting::functions::Pgr_mst< G >::m_roots, pgrouting::functions::Pgr_mst< G >::m_spanning_tree, pgrouting::functions::Pgr_mst< G >::m_tree_id, and pgrouting::functions::Pgr_mst< G >::mst().

Referenced by pgrouting::functions::Pgr_mst< G >::bfs_order().

◆ calculate_component()

template<class G >
void pgrouting::functions::Pgr_mst< G >::calculate_component ( const G &  graph)
inlineprivateinherited

Definition at line 225 of file pgr_mst.hpp.

225  {
226  if (!m_get_component) return;
227 
228  m_components.resize(num_vertices(graph.graph));
229 
230  /*
231  * Calculate connected components
232  *
233  * Number of components of graph: num_comps components
234  */
235  auto num_comps = boost::connected_components(
236  graph.graph,
237  &m_components[0]);
238 
239  m_tree_id.resize(num_comps, 0);
240 
241  for (const auto v : boost::make_iterator_range(vertices(graph.graph))) {
242  m_tree_id[m_components[v]] =
243  (m_tree_id[m_components[v]] == 0
244  || m_tree_id[m_components[v]] >= graph[v].id) ?
245  graph[v].id : m_tree_id[m_components[v]];
246  }
247  }

References pgrouting::functions::Pgr_mst< G >::m_components, pgrouting::functions::Pgr_mst< G >::m_get_component, and pgrouting::functions::Pgr_mst< G >::m_tree_id.

Referenced by pgrouting::functions::Pgr_mst< G >::bfs_ordering().

◆ clear()

template<class G >
void pgrouting::functions::Pgr_mst< G >::clear ( )
inlineprotectedinherited

◆ dfs_forest()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::dfs_forest ( const G &  graph)
inlineprivateinherited

Definition at line 255 of file pgr_mst.hpp.

255  {
256  boost::filtered_graph<B_G, InSpanning, boost::keep_all>
257  mstGraph(graph.graph, m_spanning_tree, {});
258  std::vector<E> visited_order;
259 
260  using dfs_visitor = visitors::Edges_order_dfs_visitor<E>;
261  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
262  CHECK_FOR_INTERRUPTS();
263  try {
264  boost::depth_first_search(mstGraph, visitor(dfs_visitor(visited_order)));
265  } catch (boost::exception const& ex) {
266  (void)ex;
267  throw;
268  } catch (std::exception &e) {
269  (void)e;
270  throw;
271  } catch (...) {
272  throw;
273  }
274 
275  return get_results(visited_order, 0, graph);
276  }

References pgrouting::functions::Pgr_mst< G >::get_results(), and pgrouting::functions::Pgr_mst< G >::m_spanning_tree.

Referenced by pgrouting::functions::Pgr_mst< G >::dfs_ordering().

◆ dfs_order()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::dfs_order ( const G &  graph)
inlineprotectedinherited

◆ dfs_ordering()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::dfs_ordering ( const G &  graph)
inlineprivateinherited

Definition at line 280 of file pgr_mst.hpp.

280  {
281  boost::filtered_graph<B_G, InSpanning, boost::keep_all>
282  mstGraph(graph.graph, m_spanning_tree, {});
283 
284  if (m_roots.empty()) {
285  return dfs_forest(graph);
286  } else {
287  std::vector<pgr_mst_rt> results;
288  for (const auto root : m_roots) {
289  std::vector<E> visited_order;
290 
291  using dfs_visitor = visitors::Dfs_visitor_with_root<V, E>;
292  if (graph.has_vertex(root)) {
293  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
294  CHECK_FOR_INTERRUPTS();
295  try {
296  boost::depth_first_search(
297  mstGraph,
298  visitor(dfs_visitor(graph.get_V(root), visited_order))
299  .root_vertex(graph.get_V(root)));
300  } catch(found_goals &) {
301  {}
302  } catch (boost::exception const& ex) {
303  (void)ex;
304  throw;
305  } catch (std::exception &e) {
306  (void)e;
307  throw;
308  } catch (...) {
309  throw;
310  }
311  auto result = get_results(visited_order, root, graph);
312  results.insert(results.end(), result.begin(), result.end());
313  } else {
314  results.push_back({root, 0, root, -1, 0.0, 0.0});
315  }
316  }
317  return results;
318  }
319  }

References pgrouting::functions::Pgr_mst< G >::dfs_forest(), pgrouting::functions::Pgr_mst< G >::get_results(), pgrouting::functions::Pgr_mst< G >::m_roots, and pgrouting::functions::Pgr_mst< G >::m_spanning_tree.

Referenced by pgrouting::functions::Pgr_mst< G >::dfs_order().

◆ generate_mst()

template<class G >
void pgrouting::functions::Pgr_kruskal< G >::generate_mst ( const G &  graph)
privatevirtual

Implements pgrouting::functions::Pgr_mst< G >.

Definition at line 72 of file pgr_kruskal.hpp.

72  {
73  this->clear();
74  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
75  CHECK_FOR_INTERRUPTS();
76  boost::kruskal_minimum_spanning_tree(
77  graph.graph,
78  std::inserter(this->m_spanning_tree.edges, this->m_spanning_tree.edges.begin()),
79  boost::weight_map(get(&G::G_T_E::cost, graph.graph)));
80 }

◆ get_results()

template<class G >
template<typename T >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::get_results ( order,
int64_t  p_root,
const G &  graph 
)
inlineprivateinherited

Definition at line 170 of file pgr_mst.hpp.

173  {
174  std::vector<pgr_mst_rt> results;
175 
176  std::vector<double> agg_cost(graph.num_vertices(), 0);
177  std::vector<int64_t> depth(graph.num_vertices(), 0);
178  int64_t root(p_root);
179 
180  for (const auto edge : order) {
181  auto u = graph.source(edge);
182  auto v = graph.target(edge);
183  if (depth[u] == 0 && depth[v] != 0) {
184  std::swap(u, v);
185  }
186 
187  auto component = m_get_component? m_tree_id[m_components[u]] : 0;
188  if (m_suffix != "" && depth[u] == 0 && depth[v] == 0) {
189  if (!m_roots.empty() && graph[u].id != root) std::swap(u, v);
190  if (m_roots.empty() && graph[u].id != component) std::swap(u, v);
191  if (!p_root && graph[u].id > graph[v].id) std::swap(u, v);
192 
193  root = p_root? p_root: graph[u].id;
194  depth[u] = -1;
195  results.push_back({
196  root,
197  0,
198  graph[u].id,
199  -1,
200  0.0,
201  0.0 });
202  }
203 
204  agg_cost[v] = agg_cost[u] + graph[edge].cost;
205  depth[v] = depth[u] == -1? 1 : depth[u] + 1;
206 
207  if ((m_suffix == "")
208  || ((m_suffix == "BFS") && (m_max_depth >= depth[v]))
209  || ((m_suffix == "DFS") && m_max_depth >= depth[v])
210  || ((m_suffix == "DD") && m_distance >= agg_cost[v])) {
211  results.push_back({
212  root,
213  m_suffix != ""? depth[v] : 0,
214  graph[v].id,
215  graph[edge].id,
216  graph[edge].cost,
217  m_suffix != ""? agg_cost[v] : 0.0
218  });
219  }
220  }
221  return results;
222  }

References pgrouting::functions::Pgr_mst< G >::m_components, pgrouting::functions::Pgr_mst< G >::m_distance, pgrouting::functions::Pgr_mst< G >::m_get_component, pgrouting::functions::Pgr_mst< G >::m_max_depth, pgrouting::functions::Pgr_mst< G >::m_roots, pgrouting::functions::Pgr_mst< G >::m_suffix, and pgrouting::functions::Pgr_mst< G >::m_tree_id.

Referenced by pgrouting::functions::Pgr_mst< G >::bfs_ordering(), pgrouting::functions::Pgr_mst< G >::dfs_forest(), pgrouting::functions::Pgr_mst< G >::dfs_ordering(), and pgrouting::functions::Pgr_mst< G >::no_ordering().

◆ kruskal()

template<class G >
std::vector< pgr_mst_rt > pgrouting::functions::Pgr_kruskal< G >::kruskal ( G &  graph)

Definition at line 85 of file pgr_kruskal.hpp.

86  {
87  return this->mst(graph);
88 }

◆ kruskalBFS()

template<class G >
std::vector< pgr_mst_rt > pgrouting::functions::Pgr_kruskal< G >::kruskalBFS ( G &  graph,
std::vector< int64_t >  roots,
int64_t  max_depth 
)

Definition at line 93 of file pgr_kruskal.hpp.

96  {
97  return this->mstBFS(graph, roots, max_depth);
98 }

◆ kruskalDD()

template<class G >
std::vector< pgr_mst_rt > pgrouting::functions::Pgr_kruskal< G >::kruskalDD ( G &  graph,
std::vector< int64_t >  roots,
double  distance 
)

Definition at line 111 of file pgr_kruskal.hpp.

114  {
115  return this->mstDD(graph, roots, distance);
116 }

◆ kruskalDFS()

template<class G >
std::vector< pgr_mst_rt > pgrouting::functions::Pgr_kruskal< G >::kruskalDFS ( G &  graph,
std::vector< int64_t >  roots,
int64_t  max_depth 
)

Definition at line 102 of file pgr_kruskal.hpp.

105  {
106  return this->mstDFS(graph, roots, max_depth);
107 }

◆ mst()

◆ mstBFS()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::mstBFS ( const G &  graph,
std::vector< int64_t >  roots,
int64_t  max_depth 
)
inlineprotectedinherited

◆ mstDD()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::mstDD ( const G &  graph,
std::vector< int64_t >  roots,
double  distance 
)
inlineprotectedinherited

◆ mstDFS()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::mstDFS ( const G &  graph,
std::vector< int64_t >  roots,
int64_t  max_depth 
)
inlineprotectedinherited

◆ no_neg_costs()

template<class G >
bool pgrouting::functions::Pgr_mst< G >::no_neg_costs ( const G &  graph)
inlineprivateinherited

Definition at line 359 of file pgr_mst.hpp.

359  {
360  E_i ei, ei_end;
361  for (boost::tie(ei, ei_end) = edges(graph.graph); ei != ei_end; ++ei)
362  pgassert(graph[*ei].cost > 0);
363  return true;
364  }

References pgassert.

Referenced by pgrouting::functions::Pgr_mst< G >::mst().

◆ no_order()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::no_order ( const G &  graph)
inlineprotectedinherited

Definition at line 66 of file pgr_mst.hpp.

66  {
67  return no_ordering(graph);
68  }

References pgrouting::functions::Pgr_mst< G >::no_ordering().

Referenced by pgrouting::functions::Pgr_mst< G >::mst().

◆ no_ordering()

template<class G >
std::vector<pgr_mst_rt> pgrouting::functions::Pgr_mst< G >::no_ordering ( const G &  graph)
inlineprivateinherited

Member Data Documentation

◆ m_components

template<class G >
std::vector<size_t> pgrouting::functions::Pgr_mst< G >::m_components
protectedinherited

m_components[v]:

  • is empty (when m_get_component = 0)
  • has the component number of vertex v (when m_get_component != 0)

Definition at line 152 of file pgr_mst.hpp.

Referenced by pgrouting::functions::Pgr_mst< G >::calculate_component(), pgrouting::functions::Pgr_mst< G >::clear(), and pgrouting::functions::Pgr_mst< G >::get_results().

◆ m_distance

◆ m_get_component

◆ m_max_depth

◆ m_roots

◆ m_spanning_tree

◆ m_suffix

template<class G >
std::string pgrouting::functions::Pgr_mst< G >::m_suffix
protectedinherited

◆ m_tree_id

template<class G >
std::vector<int64_t> pgrouting::functions::Pgr_mst< G >::m_tree_id
protectedinherited

m_tree_id[v]:

  • is empty (when m_get_component = 0)
  • has the component number of vertex v (when m_get_component = 1)
  • has the min vertex id that belongs to the component (when m_get_component = 2)

Definition at line 166 of file pgr_mst.hpp.

Referenced by pgrouting::functions::Pgr_mst< G >::bfs_ordering(), pgrouting::functions::Pgr_mst< G >::calculate_component(), pgrouting::functions::Pgr_mst< G >::clear(), and pgrouting::functions::Pgr_mst< G >::get_results().


The documentation for this class was generated from the following file:
pgrouting::functions::Pgr_mst::m_roots
std::vector< int64_t > m_roots
Definition: pgr_mst.hpp:136
pgrouting::functions::Pgr_mst::m_spanning_tree
struct pgrouting::functions::Pgr_mst::InSpanning m_spanning_tree
pgrouting::details::clean_vids
std::vector< int64_t > clean_vids(std::vector< int64_t > vids)
Definition: details.cpp:33
pgrouting::functions::Pgr_mst::m_max_depth
int64_t m_max_depth
Definition: pgr_mst.hpp:138
pgrouting::functions::Pgr_mst::InSpanning::clear
void clear()
Definition: pgr_mst.hpp:144
pgrouting::functions::Pgr_mst::m_get_component
bool m_get_component
Definition: pgr_mst.hpp:137
pgrouting::functions::Pgr_mst::bfs_ordering
std::vector< pgr_mst_rt > bfs_ordering(const G &graph)
Definition: pgr_mst.hpp:322
edge
Definition: trsp.h:41
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::functions::Pgr_mst::get_results
std::vector< pgr_mst_rt > get_results(T order, int64_t p_root, const G &graph)
Definition: pgr_mst.hpp:170
pgrouting::functions::Pgr_mst::m_components
std::vector< size_t > m_components
m_components[v]:
Definition: pgr_mst.hpp:152
pgrouting::functions::Pgr_mst::InSpanning::edges
std::set< E > edges
Definition: pgr_mst.hpp:142
pgrouting::functions::Pgr_mst::no_ordering
std::vector< pgr_mst_rt > no_ordering(const G &graph)
Definition: pgr_mst.hpp:250
pgrouting::functions::Pgr_mst::bfs_order
std::vector< pgr_mst_rt > bfs_order(const G &graph)
Definition: pgr_mst.hpp:76
pgrouting::functions::Pgr_mst::m_distance
double m_distance
Definition: pgr_mst.hpp:139
pgrouting::functions::Pgr_mst::E_i
G::E_i E_i
Definition: pgr_mst.hpp:53
pgrouting::functions::Pgr_mst::no_neg_costs
bool no_neg_costs(const G &graph)
Definition: pgr_mst.hpp:359
pgrouting::functions::Pgr_mst::m_suffix
std::string m_suffix
Stores which function is being executed.
Definition: pgr_mst.hpp:159
pgrouting::functions::Pgr_mst::generate_mst
virtual void generate_mst(const G &graph)=0
pgrouting::functions::Pgr_mst::mstBFS
std::vector< pgr_mst_rt > mstBFS(const G &graph, std::vector< int64_t > roots, int64_t max_depth)
Definition: pgr_mst.hpp:93
pgrouting::functions::Pgr_mst::clear
void clear()
Definition: pgr_mst.hpp:59
pgrouting::functions::Pgr_mst::m_tree_id
std::vector< int64_t > m_tree_id
m_tree_id[v]:
Definition: pgr_mst.hpp:166
pgrouting::functions::Pgr_mst::dfs_order
std::vector< pgr_mst_rt > dfs_order(const G &graph)
Definition: pgr_mst.hpp:71
pgrouting::functions::Pgr_mst::dfs_ordering
std::vector< pgr_mst_rt > dfs_ordering(const G &graph)
Definition: pgr_mst.hpp:280
pgrouting::functions::Pgr_mst::mstDFS
std::vector< pgr_mst_rt > mstDFS(const G &graph, std::vector< int64_t > roots, int64_t max_depth)
Definition: pgr_mst.hpp:107
pgrouting::functions::Pgr_mst::calculate_component
void calculate_component(const G &graph)
Definition: pgr_mst.hpp:225
pgrouting::functions::Pgr_mst::no_order
std::vector< pgr_mst_rt > no_order(const G &graph)
Definition: pgr_mst.hpp:66
pgrouting::functions::Pgr_mst::dfs_forest
std::vector< pgr_mst_rt > dfs_forest(const G &graph)
Definition: pgr_mst.hpp:255
pgrouting::functions::Pgr_mst::mstDD
std::vector< pgr_mst_rt > mstDD(const G &graph, std::vector< int64_t > roots, double distance)
Definition: pgr_mst.hpp:121
pgrouting::functions::Pgr_mst::mst
std::vector< pgr_mst_rt > mst(const G &graph)
Definition: pgr_mst.hpp:80