PGROUTING  3.2
pgr_maximumcardinalitymatching.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
5 
6 Copyright (c) 2016 Andrea Nardelli
8 
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 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 
25  ********************************************************************PGR-GNU*/
26 
27 #ifndef INCLUDE_MAX_FLOW_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
28 #define INCLUDE_MAX_FLOW_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
29 #pragma once
30 
31 
32 #include <boost/config.hpp>
33 #include <boost/graph/adjacency_list.hpp>
34 #include <boost/graph/max_cardinality_matching.hpp>
35 
36 
37 #include <map>
38 #include <vector>
39 #include <utility>
40 #include <set>
41 
42 
43 
44 
45 namespace pgrouting {
46 
47 typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS>
49 typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS>
51 
52 namespace flow {
53 
54 template<class G>
56  public:
58 
59  typedef typename boost::graph_traits<G>::vertex_descriptor V;
60  typedef typename boost::graph_traits<G>::edge_descriptor E;
61  typedef typename boost::graph_traits<G>::vertex_iterator V_it;
62  typedef typename boost::graph_traits<G>::edge_iterator E_it;
63 
64  std::map<int64_t, V> id_to_V;
65  std::map<V, int64_t> V_to_id;
66  std::map<E, int64_t> E_to_id;
67 
68  V get_boost_vertex(int64_t id) {
69  return id_to_V[id];
70  }
71 
72  int64_t get_vertex_id(V v) {
73  return V_to_id[v];
74  }
75 
76  int64_t get_edge_id(E e) {
77  return E_to_id[e];
78  }
79 
80  PgrCardinalityGraph(pgr_basic_edge_t *data_edges, size_t total_tuples) {
81  std::set<int64_t> vertices;
82  for (size_t i = 0; i < total_tuples; ++i) {
83  vertices.insert(data_edges[i].source);
84  vertices.insert(data_edges[i].target);
85  }
86  for (int64_t id : vertices) {
87  V v = add_vertex(boost_graph);
88  id_to_V.insert(std::pair<int64_t, V>(id, v));
89  V_to_id.insert(std::pair<V, int64_t>(v, id));
90  }
91  bool added;
92 
93  for (size_t i = 0; i < total_tuples; ++i) {
94  V v1 = get_boost_vertex(data_edges[i].source);
95  V v2 = get_boost_vertex(data_edges[i].target);
96  E e1;
97  E e2;
98  if (data_edges[i].going) {
99  boost::tie(e1, added) = boost::add_edge(v1, v2, boost_graph);
100  E_to_id.insert(std::pair<E, int64_t>(e1, data_edges[i].id));
101  }
102  if (data_edges[i].coming) {
103  boost::tie(e2, added) = boost::add_edge(v2, v1, boost_graph);
104  E_to_id.insert(std::pair<E, int64_t>(e2, data_edges[i].id));
105  }
106  }
107  }
108 
109  std::vector<pgr_basic_edge_t>
111  std::vector<V> mate_map(boost::num_vertices(boost_graph));
112  std::vector<pgr_basic_edge_t> matched_vertices;
114 
115  V_it vi, vi_end;
116  E e;
117  bool exists;
118  if (boost::is_directed(boost_graph)) {
119  std::vector<bool> already_matched(num_vertices(boost_graph), false);
120  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
121  vi != vi_end;
122  ++vi) {
123  /*
124  * For each vertex I check:
125  * 1) It is matched with a non-null vertex
126  * 2) An edge exists from this vertex to his mate
127  * 3) The vertices have not been matched already
128  * (this last point prevents having double output with reversed
129  * source and target)
130  */
131  boost::tie(e, exists) = boost::edge(*vi, mate_map[*vi], boost_graph);
132  if (((uint64_t)mate_map[*vi]
133  != boost::graph_traits<G>::null_vertex())
134  && exists && !already_matched[*vi]
135  && !already_matched[mate_map[*vi]]) {
136  already_matched[*vi] = true;
137  already_matched[mate_map[*vi]] = true;
138  pgr_basic_edge_t matched_couple;
139  matched_couple.source = get_vertex_id(*vi);
140  matched_couple.target = get_vertex_id(mate_map[*vi]);
141  matched_couple.edge_id = get_edge_id(e);
142  matched_vertices.push_back(matched_couple);
143  }
144  }
145  } else {
146  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
147  vi != vi_end;
148  ++vi) {
149  boost::tie(e, exists) =
150  boost::edge(*vi, mate_map[*vi], boost_graph);
151  if (((uint64_t)mate_map[*vi]
152  != boost::graph_traits<G>::null_vertex())
153  && (*vi < (uint64_t)mate_map[*vi])) {
154  pgr_basic_edge_t matched_couple;
155  matched_couple.source = get_vertex_id(*vi);
156  matched_couple.target = get_vertex_id(mate_map[*vi]);
157  matched_couple.edge_id = get_edge_id(e);
158  matched_vertices.push_back(matched_couple);
159  }
160  }
161  }
162  return matched_vertices;
163  }
164 
165  void maximum_cardinality_matching(std::vector<V> &mate_map) {
166  edmonds_maximum_cardinality_matching(boost_graph,
167  &mate_map[0]);
168  }
169 };
170 
171 } // namespace flow
172 } // namespace pgrouting
173 
174 #endif // INCLUDE_MAX_FLOW_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
pgrouting::flow::PgrCardinalityGraph::get_boost_vertex
V get_boost_vertex(int64_t id)
Definition: pgr_maximumcardinalitymatching.hpp:68
pgrouting::flow::PgrCardinalityGraph::maximum_cardinality_matching
void maximum_cardinality_matching(std::vector< V > &mate_map)
Definition: pgr_maximumcardinalitymatching.hpp:165
pgrouting::BasicUndirectedGraph
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS > BasicUndirectedGraph
Definition: pgr_maximumcardinalitymatching.hpp:48
pgr_basic_edge_t::source
int64_t source
Definition: pgr_basic_edge_t.h:40
pgr_basic_edge_t::edge_id
int64_t edge_id
Definition: pgr_basic_edge_t.h:44
pgrouting::flow::PgrCardinalityGraph::get_matched_vertices
std::vector< pgr_basic_edge_t > get_matched_vertices()
Definition: pgr_maximumcardinalitymatching.hpp:110
pgrouting::flow::PgrCardinalityGraph::E_to_id
std::map< E, int64_t > E_to_id
Definition: pgr_maximumcardinalitymatching.hpp:66
pgr_basic_edge_t::target
int64_t target
Definition: pgr_basic_edge_t.h:41
pgrouting::BasicDirectedGraph
boost::adjacency_list< boost::listS, boost::vecS, boost::directedS > BasicDirectedGraph
Definition: pgr_maximumcardinalitymatching.hpp:50
pgrouting::flow::PgrCardinalityGraph::E
boost::graph_traits< G >::edge_descriptor E
Definition: pgr_maximumcardinalitymatching.hpp:60
pgrouting::flow::PgrCardinalityGraph::E_it
boost::graph_traits< G >::edge_iterator E_it
Definition: pgr_maximumcardinalitymatching.hpp:62
pgrouting::flow::PgrCardinalityGraph::boost_graph
G boost_graph
Definition: pgr_maximumcardinalitymatching.hpp:57
pgr_basic_edge_t
Definition: pgr_basic_edge_t.h:38
pgrouting::flow::PgrCardinalityGraph::get_vertex_id
int64_t get_vertex_id(V v)
Definition: pgr_maximumcardinalitymatching.hpp:72
pgrouting::flow::PgrCardinalityGraph::PgrCardinalityGraph
PgrCardinalityGraph(pgr_basic_edge_t *data_edges, size_t total_tuples)
Definition: pgr_maximumcardinalitymatching.hpp:80
pgrouting::flow::PgrCardinalityGraph::V_to_id
std::map< V, int64_t > V_to_id
Definition: pgr_maximumcardinalitymatching.hpp:65
pgrouting::flow::PgrCardinalityGraph::get_edge_id
int64_t get_edge_id(E e)
Definition: pgr_maximumcardinalitymatching.hpp:76
pgrouting::flow::PgrCardinalityGraph::V_it
boost::graph_traits< G >::vertex_iterator V_it
Definition: pgr_maximumcardinalitymatching.hpp:61
pgrouting::flow::PgrCardinalityGraph
Definition: pgr_maximumcardinalitymatching.hpp:55
pgrouting::flow::PgrCardinalityGraph::V
boost::graph_traits< G >::vertex_descriptor V
Definition: pgr_maximumcardinalitymatching.hpp:59
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::flow::PgrCardinalityGraph::id_to_V
std::map< int64_t, V > id_to_V
Definition: pgr_maximumcardinalitymatching.hpp:64
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56