PGROUTING  3.1
pgr_maximumcardinalitymatching.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 Copyright (c) 2016 Andrea Nardelli
7 Mail: nrd.nardelli@gmail.com
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 
112  std::vector<V> mate_map(boost::num_vertices(boost_graph));
113  std::vector<pgr_basic_edge_t> matched_vertices;
115 
116  V_it vi, vi_end;
117  E e;
118  bool exists;
119  if (boost::is_directed(boost_graph)) {
120  std::vector<bool> already_matched(num_vertices(boost_graph), false);
121  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
122  vi != vi_end;
123  ++vi) {
124  /*
125  * For each vertex I check:
126  * 1) It is matched with a non-null vertex
127  * 2) An edge exists from this vertex to his mate
128  * 3) The vertices have not been matched already
129  * (this last point prevents having double output with reversed
130  * source and target)
131  */
132  boost::tie(e, exists) = boost::edge(*vi, mate_map[*vi], boost_graph);
133  if (((uint64_t)mate_map[*vi]
134  != boost::graph_traits<G>::null_vertex())
135  && exists && !already_matched[*vi]
136  && !already_matched[mate_map[*vi]]) {
137  already_matched[*vi] = true;
138  already_matched[mate_map[*vi]] = true;
139  pgr_basic_edge_t matched_couple;
140  matched_couple.source = get_vertex_id(*vi);
141  matched_couple.target = get_vertex_id(mate_map[*vi]);
142  matched_couple.edge_id = get_edge_id(e);
143  matched_vertices.push_back(matched_couple);
144  }
145  }
146  } else {
147  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
148  vi != vi_end;
149  ++vi) {
150  boost::tie(e, exists) =
151  boost::edge(*vi, mate_map[*vi], boost_graph);
152  if (((uint64_t)mate_map[*vi]
153  != boost::graph_traits<G>::null_vertex())
154  && (*vi < (uint64_t)mate_map[*vi])) {
155  pgr_basic_edge_t matched_couple;
156  matched_couple.source = get_vertex_id(*vi);
157  matched_couple.target = get_vertex_id(mate_map[*vi]);
158  matched_couple.edge_id = get_edge_id(e);
159  matched_vertices.push_back(matched_couple);
160  }
161  }
162  }
163  return matched_vertices;
164  }
165 
166  void maximum_cardinality_matching(std::vector<V> &mate_map) {
167  edmonds_maximum_cardinality_matching(boost_graph,
168  &mate_map[0]);
169  }
170 };
171 
172 } // namespace flow
173 } // namespace pgrouting
174 
175 #endif // INCLUDE_MAX_FLOW_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
PgrCardinalityGraph(pgr_basic_edge_t *data_edges, size_t total_tuples)
boost::graph_traits< G >::vertex_iterator V_it
boost::adjacency_list< boost::listS, boost::vecS, boost::directedS > BasicDirectedGraph
boost::graph_traits< G >::vertex_descriptor V
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS > BasicUndirectedGraph
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
void maximum_cardinality_matching(std::vector< V > &mate_map)
Book keeping class for swapping orders between vehicles.
std::vector< pgr_basic_edge_t > get_matched_vertices()
boost::graph_traits< G >::edge_descriptor E
boost::graph_traits< G >::edge_iterator E_it