PGROUTING  3.2
pgr_sequentialVertexColoring.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_sequentialVertexColoring.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_COLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_
25 #define INCLUDE_COLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_
26 #pragma once
27 
28 
29 #include <boost/property_map/property_map.hpp>
30 #include <boost/graph/graph_traits.hpp>
31 #include <boost/property_map/vector_property_map.hpp>
32 #include <boost/type_traits.hpp>
33 #include <boost/graph/adjacency_list.hpp>
34 #include <boost/graph/sequential_vertex_coloring.hpp>
35 
36 #include <algorithm>
37 #include <vector>
38 #include <map>
39 
42 
43 
52 namespace pgrouting {
53 namespace functions {
54 
55 //*************************************************************
56 
57 template < class G >
59  public:
60  typedef typename G::V V;
61  typedef typename G::E E;
62  typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS > Graph;
63  typedef boost::graph_traits < Graph > ::vertices_size_type vertices_size_type;
64 
81  std::vector < pgr_vertex_color_rt > sequentialVertexColoring(G &graph) {
82  std::vector < pgr_vertex_color_rt > results;
83 
84  auto i_map = boost::get(boost::vertex_index, graph.graph);
85 
86  // vector which will store the color of all the vertices in the graph
87  std::vector < vertices_size_type > colors(boost::num_vertices(graph.graph));
88 
89  // An iterator property map which records the color of each vertex
90  auto color_map = boost::make_iterator_property_map(colors.begin(), i_map);
91 
92  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
93  CHECK_FOR_INTERRUPTS();
94 
95  try {
96  boost::sequential_vertex_coloring(graph.graph, color_map);
97  } catch (boost::exception const& ex) {
98  (void)ex;
99  throw;
100  } catch (std::exception &e) {
101  (void)e;
102  throw;
103  } catch (...) {
104  throw;
105  }
106 
107  results = get_results(colors, graph);
108 
109  return results;
110  }
111 
113 
114  private:
124  std::vector < pgr_vertex_color_rt > get_results(
125  std::vector < vertices_size_type > &colors,
126  const G &graph) {
127  std::vector < pgr_vertex_color_rt > results;
128 
129  typename boost::graph_traits < Graph > ::vertex_iterator v, vend;
130 
131  for (boost::tie(v, vend) = vertices(graph.graph); v != vend; ++v) {
132  int64_t node = graph[*v].id;
133  auto color = colors[*v];
134  results.push_back({ node, static_cast<int64_t>(color + 1) });
135  }
136 
137  // ordering the results in an increasing order of the node id
138  std::sort(results.begin(), results.end(),
139  [](const pgr_vertex_color_rt row1, const pgr_vertex_color_rt row2) {
140  return row1.node < row2.node;
141  });
142 
143  return results;
144  }
145 };
146 } // namespace functions
147 } // namespace pgrouting
148 
149 #endif // INCLUDE_COLORING_PGR_SEQUENTIALVERTEXCOLORING_HPP_
interruption.h
pgrouting::functions::Pgr_sequentialVertexColoring::vertices_size_type
boost::graph_traits< Graph >::vertices_size_type vertices_size_type
Definition: pgr_sequentialVertexColoring.hpp:63
pgr_base_graph.hpp
pgrouting::functions::Pgr_sequentialVertexColoring::E
G::E E
Definition: pgr_sequentialVertexColoring.hpp:61
pgrouting::functions::Pgr_sequentialVertexColoring::Graph
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS > Graph
Definition: pgr_sequentialVertexColoring.hpp:62
pgrouting::functions::Pgr_sequentialVertexColoring
Definition: pgr_sequentialVertexColoring.hpp:58
pgrouting::alphashape::E
boost::graph_traits< BG >::edge_descriptor E
Definition: pgr_alphaShape.h:57
pgrouting::functions::Pgr_sequentialVertexColoring::sequentialVertexColoring
std::vector< pgr_vertex_color_rt > sequentialVertexColoring(G &graph)
sequentialVertexColoring function
Definition: pgr_sequentialVertexColoring.hpp:81
pgrouting::functions::Pgr_sequentialVertexColoring::get_results
std::vector< pgr_vertex_color_rt > get_results(std::vector< vertices_size_type > &colors, const G &graph)
to get the results
Definition: pgr_sequentialVertexColoring.hpp:124
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
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
pgr_vertex_color_rt
Definition: pgr_vertex_color_rt.h:36
pgrouting::functions::Pgr_sequentialVertexColoring::V
G::V V
Definition: pgr_sequentialVertexColoring.hpp:60