PGROUTING  3.2
pgr_deadEndContraction.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_deadend.hpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Rohith Reddy
10 Mail:
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 #ifndef INCLUDE_CONTRACTION_PGR_DEADENDCONTRACTION_HPP_
31 #define INCLUDE_CONTRACTION_PGR_DEADENDCONTRACTION_HPP_
32 #pragma once
33 
34 
35 #include <queue>
36 #include <functional>
37 #include <vector>
40 
41 namespace pgrouting {
42 namespace contraction {
43 
44 template < class G >
45 class Pgr_deadend {
46  private:
47  using V = typename G::V;
48  using E = typename G::E;
49 
50  public:
51  Pgr_deadend() = default;
52 
54  Identifiers<V> forbidden_vertices) {
55  forbiddenVertices = forbidden_vertices;
56  }
57 
58 
59  void calculateVertices(G &graph) {
60  for (const auto v : boost::make_iterator_range(vertices(graph.graph))) {
61  if (is_dead_end(graph, v) && !forbiddenVertices.has(v)) {
62  deadendVertices += v;
63  }
64  }
65  }
66 
67  bool is_dead_end(G &graph, V v) {
68  if (graph.is_undirected()) {
69  return graph.find_adjacent_vertices(v).size() == 1;
70  }
71 
72  pgassert(graph.is_directed());
73 
74  return graph.find_adjacent_vertices(v).size() == 1
75  || (graph.in_degree(v) > 0 && graph.out_degree(v) == 0);
76  }
77 
78  void doContraction(G &graph) {
79  calculateVertices(graph);
80 
81  while (!deadendVertices.empty()) {
82  V v = deadendVertices.front();
83  deadendVertices -= v;
84  pgassert(is_dead_end(graph, v));
85 
86  Identifiers<V> local;
87  for (auto u : graph.find_adjacent_vertices(v)) {
88  /*
89  * u{v1} --{v2}-> v{v3}
90  *
91  * u{v1 + v + v2 + v3} v{}
92  */
93  auto v2(graph.get_min_cost_edge(u, v));
94  graph[u].contracted_vertices() += std::get<1>(v2);
95  graph[u].contracted_vertices() += graph[v].id;
96  graph[u].contracted_vertices() += graph[v].contracted_vertices();
97 
98  deadendVertices -= v;
99  local += u;
100  }
101 
102  graph[v].contracted_vertices().clear();
103  boost::clear_vertex(v, graph.graph);
104 
105  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
106  CHECK_FOR_INTERRUPTS();
107 
108  for (const auto u : local) {
109  if (is_dead_end(graph, u) && !forbiddenVertices.has(u)) {
110  deadendVertices += u;
111  } else {
112  deadendVertices -= u;
113  }
114  }
115  }
116  }
117 
118  private:
121 };
122 
123 } // namespace contraction
124 } // namespace pgrouting
125 
126 #endif // INCLUDE_CONTRACTION_PGR_DEADENDCONTRACTION_HPP_
interruption.h
pgrouting::contraction::Pgr_deadend::calculateVertices
void calculateVertices(G &graph)
Definition: pgr_deadEndContraction.hpp:59
pgrouting::contraction::Pgr_deadend::forbiddenVertices
Identifiers< V > forbiddenVertices
Definition: pgr_deadEndContraction.hpp:120
pgrouting::contraction::Pgr_deadend::doContraction
void doContraction(G &graph)
Definition: pgr_deadEndContraction.hpp:78
pgrouting::contraction::Pgr_deadend::is_dead_end
bool is_dead_end(G &graph, V v)
Definition: pgr_deadEndContraction.hpp:67
pgrouting::contraction::Pgr_deadend::deadendVertices
Identifiers< V > deadendVertices
Definition: pgr_deadEndContraction.hpp:119
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
Identifiers::clear
void clear()
Definition: identifiers.hpp:84
pgrouting::contraction::Pgr_deadend
Definition: pgr_deadEndContraction.hpp:45
pgrouting::alphashape::E
boost::graph_traits< BG >::edge_descriptor E
Definition: pgr_alphaShape.h:57
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:79
pgrouting::contraction::Pgr_deadend::E
typename G::E E
Definition: pgr_deadEndContraction.hpp:48
pgrouting::contraction::Pgr_deadend::setForbiddenVertices
void setForbiddenVertices(Identifiers< V > forbidden_vertices)
Definition: pgr_deadEndContraction.hpp:53
Identifiers::front
T front() const
Definition: identifiers.hpp:80
Identifiers::has
bool has(const T other) const
true ids() has element
Definition: identifiers.hpp:98
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::contraction::Pgr_deadend::V
typename G::V V
Definition: pgr_deadEndContraction.hpp:47
pgrouting::alphashape::V
boost::graph_traits< BG >::vertex_descriptor V
Definition: pgr_alphaShape.h:58
pgrouting::contraction::Pgr_deadend::Pgr_deadend
Pgr_deadend()=default
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
identifiers.hpp
Identifiers< V >