PGROUTING  3.2
pgr_contractionGraph.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_contractionGraph.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_CONTRACTIONGRAPH_HPP_
31 #define INCLUDE_CONTRACTION_PGR_CONTRACTIONGRAPH_HPP_
32 #pragma once
33 
34 #include <boost/graph/iteration_macros.hpp>
35 
36 #include <limits>
37 #include <algorithm>
38 #include <vector>
39 #include <iostream>
40 #include <tuple>
41 
43 #include "cpp_common/ch_vertex.h"
44 #include "cpp_common/ch_edge.h"
45 
46 
47 namespace pgrouting {
48 namespace graph {
49 
50 template <class G>
51 class Pgr_contractionGraph : public Pgr_base_graph<G, CH_vertex, CH_edge> {
52  public:
53  typedef typename boost::graph_traits < G >::vertex_descriptor V;
54  typedef typename boost::graph_traits < G >::edge_descriptor E;
55  typedef typename boost::graph_traits < G >::out_edge_iterator EO_i;
56  typedef typename boost::graph_traits < G >::in_edge_iterator EI_i;
57 
62  : Pgr_base_graph<G, CH_vertex, CH_edge >(gtype) {
63  }
64 
70  EO_i out, out_end;
71  EI_i in, in_end;
72  Identifiers<V> adjacent_vertices;
73 
74  for (boost::tie(out, out_end) = out_edges(v, this->graph);
75  out != out_end; ++out) {
76  adjacent_vertices += this->adjacent(v, *out);
77  }
78  for (boost::tie(in, in_end) = in_edges(v, this->graph);
79  in != in_end; ++in) {
80  adjacent_vertices += this->adjacent(v, *in);
81  }
82  return adjacent_vertices;
83  }
84 
85 
91  std::tuple<double, Identifiers<int64_t>, bool>
93  E min_edge;
94  Identifiers<int64_t> contracted_vertices;
95  double min_cost = (std::numeric_limits<double>::max)();
96  bool found = false;
97 
98  if (this->is_directed()) {
99  BGL_FORALL_OUTEDGES_T(u, e, this->graph, G) {
100  if (this->target(e) == v) {
101  contracted_vertices += this->graph[e].contracted_vertices();
102  if (this->graph[e].cost < min_cost) {
103  min_cost = this->graph[e].cost;
104  min_edge = e;
105  found = true;
106  }
107  }
108  }
109  return std::make_tuple(min_cost, contracted_vertices, found);
110  }
111 
112  pgassert(this->is_undirected());
113  BGL_FORALL_OUTEDGES_T(u, e, this->graph, G) {
114  if (this->adjacent(u, e) == v) {
115  contracted_vertices += this->graph[e].contracted_vertices();
116  if (this->graph[e].cost < min_cost) {
117  min_cost = this->graph[e].cost;
118  min_edge = e;
119  found = true;
120  }
121  }
122  }
123  return std::make_tuple(min_cost, contracted_vertices, found);
124  }
125 
126 
130  friend
131  std::ostream& operator <<(
132  std::ostream &os,
133  const Pgr_contractionGraph &g) {
134  EO_i out, out_end;
135  for (auto vi = vertices(g.graph).first;
136  vi != vertices(g.graph).second;
137  ++vi) {
138  if ((*vi) >= g.num_vertices()) break;
139  os << g.graph[*vi].id << "(" << (*vi) << ")"
140  << g.graph[*vi].contracted_vertices() << std::endl;
141  os << " out_edges_of(" << g.graph[*vi].id << "):";
142  for (boost::tie(out, out_end) = out_edges(*vi, g.graph);
143  out != out_end; ++out) {
144  os << ' ' << g.graph[*out].id
145  << "=(" << g.graph[g.source(*out)].id
146  << ", " << g.graph[g.target(*out)].id << ") = "
147  << g.graph[*out].cost <<"\t";
148  }
149  os << std::endl;
150  }
151  return os;
152  }
153 
154 
170  void add_shortcut(const CH_edge &edge, V u, V v) {
171  bool inserted;
172  E e;
173  if (edge.cost < 0) return;
174 
175  boost::tie(e, inserted) = boost::add_edge(u, v, this->graph);
176 
177  this->graph[e]= edge;
178  }
179 
180 
181  bool has_u_v_w(V u, V v, V w) const {
182  return boost::edge(u, v, this->graph).second && boost::edge(v, w, this->graph).second;
183  }
184 
209  V u,
210  V v,
211  V w) {
212  if (u == v || v == w || u == w) return false;
213  pgassert(u != v);
214  pgassert(v != w);
215  pgassert(u != w);
216  if (this->is_undirected()) {
217  /*
218  * u - v - w
219  */
220  return has_u_v_w(u, v, w);
221  }
222 
223  pgassert(this->is_directed());
224  return
225  /*
226  * u <-> v <-> w
227  */
228  (has_u_v_w(u, v, w) && has_u_v_w(w, v, u))
229  /*
230  * u -> v -> w
231  */
232  ||
233  (has_u_v_w(u, v, w) && !(boost::edge(v, u, this->graph).second || boost::edge(w, v, this->graph).second))
234  /*
235  * u <- v <- w
236  */
237  ||
238  (has_u_v_w(w, v, u) && !(boost::edge(v, w, this->graph).second || boost::edge(u, v, this->graph).second));
239  }
240 
241  bool is_linear(V v) {
242  // Checking adjacent vertices constraint
243  auto adjacent_vertices = find_adjacent_vertices(v);
244 
245  if (adjacent_vertices.size() == 2) {
246  // Checking u - v - w
247  V u = adjacent_vertices.front();
248  adjacent_vertices.pop_front();
249  V w = adjacent_vertices.front();
250  adjacent_vertices.pop_front();
251  if (is_shortcut_possible(u, v, w)) {
252  return true;
253  }
254  return false;
255  }
256  return false;
257  }
258 };
259 
260 } // namespace graph
261 } // namespace pgrouting
262 
263 #endif // INCLUDE_CONTRACTION_PGR_CONTRACTIONGRAPH_HPP_
pgrouting::graph::Pgr_base_graph< G, CH_vertex, CH_edge >::graph
G graph
The graph.
Definition: pgr_base_graph.hpp:260
pgrouting::graph::Pgr_contractionGraph::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_contractionGraph.hpp:53
pgr_base_graph.hpp
edge::cost
float8 cost
Definition: trsp.h:45
ch_edge.h
pgrouting::graph::Pgr_contractionGraph::E
boost::graph_traits< G >::edge_descriptor E
Definition: pgr_contractionGraph.hpp:54
pgrouting::graph::Pgr_base_graph< G, CH_vertex, CH_edge >::target
V target(E e_idx) const
Definition: pgr_base_graph.hpp:561
pgrouting::graph::Pgr_contractionGraph
Definition: pgr_contractionGraph.hpp:51
pgrouting::graph::Pgr_contractionGraph::is_linear
bool is_linear(V v)
Definition: pgr_contractionGraph.hpp:241
edge
Definition: trsp.h:41
pgrouting::CH_vertex
Definition: ch_vertex.h:40
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::graph::Pgr_contractionGraph::is_shortcut_possible
bool is_shortcut_possible(V u, V v, V w)
Possibility of a shortcut from left vertex to right vertex v* should be a linear vertex u <-> v -> w:...
Definition: pgr_contractionGraph.hpp:208
pgrouting::graph::Pgr_base_graph::source
V source(E e_idx) const
Definition: pgr_base_graph.hpp:560
pgrouting::graph::Pgr_base_graph< G, CH_vertex, CH_edge >::is_directed
bool is_directed() const
Definition: pgr_base_graph.hpp:543
pgrouting::graph::Pgr_contractionGraph::add_shortcut
void add_shortcut(const CH_edge &edge, V u, V v)
add_shortuct to the graph during contraction
Definition: pgr_contractionGraph.hpp:170
pgrouting::graph::Pgr_base_graph::num_vertices
size_t num_vertices() const
Definition: pgr_base_graph.hpp:703
graphType
graphType
Definition: graph_enum.h:30
pgrouting::graph::Pgr_contractionGraph::EO_i
boost::graph_traits< G >::out_edge_iterator EO_i
Definition: pgr_contractionGraph.hpp:55
pgrouting::graph::Pgr_base_graph< G, CH_vertex, CH_edge >::is_undirected
bool is_undirected() const
Definition: pgr_base_graph.hpp:544
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::graph::Pgr_contractionGraph::get_min_cost_edge
std::tuple< double, Identifiers< int64_t >, bool > get_min_cost_edge(V u, V v)
get the edge with minimum cost between two vertices
Definition: pgr_contractionGraph.hpp:92
pgrouting::graph::Pgr_contractionGraph::find_adjacent_vertices
Identifiers< V > find_adjacent_vertices(V v) const
get the vertex descriptors of adjacent vertices of v
Definition: pgr_contractionGraph.hpp:69
pgrouting::graph::Pgr_contractionGraph::EI_i
boost::graph_traits< G >::in_edge_iterator EI_i
Definition: pgr_contractionGraph.hpp:56
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::graph::Pgr_contractionGraph::has_u_v_w
bool has_u_v_w(V u, V v, V w) const
Definition: pgr_contractionGraph.hpp:181
pgrouting::graph::Pgr_contractionGraph::operator<<
friend std::ostream & operator<<(std::ostream &os, const Pgr_contractionGraph &g)
print the graph with contracted vertices of all vertices and edges
Definition: pgr_contractionGraph.hpp:131
ch_vertex.h
pgrouting::CH_edge
Definition: ch_edge.h:41
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
Identifiers< V >
pgrouting::graph::Pgr_base_graph< G, CH_vertex, CH_edge >::adjacent
V adjacent(V v_idx, E e_idx) const
Definition: pgr_base_graph.hpp:562