PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 SRC_MAX_FLOW_SRC_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
28 #define SRC_MAX_FLOW_SRC_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 
81  size_t total_tuples) {
82  std::set<int64_t> vertices;
83  for (size_t i = 0; i < total_tuples; ++i) {
84  vertices.insert(data_edges[i].source);
85  vertices.insert(data_edges[i].target);
86  }
87  for (int64_t id : vertices) {
88  V v = add_vertex(boost_graph);
89  id_to_V.insert(std::pair<int64_t, V>(id, v));
90  V_to_id.insert(std::pair<V, int64_t>(v, id));
91  }
92  bool added;
93 
94  for (size_t i = 0; i < total_tuples; ++i) {
95  V v1 = get_boost_vertex(data_edges[i].source);
96  V v2 = get_boost_vertex(data_edges[i].target);
97  E e1;
98  E e2;
99  if (data_edges[i].going) {
100  boost::tie(e1, added) = boost::add_edge(v1, v2, boost_graph);
101  E_to_id.insert(std::pair<E, int64_t>(e1, data_edges[i].id));
102  }
103  if (data_edges[i].coming) {
104  boost::tie(e2, added) = boost::add_edge(v2, v1, boost_graph);
105  E_to_id.insert(std::pair<E, int64_t>(e2, data_edges[i].id));
106  }
107  }
108  }
109 
110  void get_matched_vertices(std::vector<pgr_basic_edge_t> &matched_vertices,
111  const std::vector<int64_t> &mate_map) {
112  V_it vi, vi_end;
113  E e;
114  bool exists;
115  if (boost::is_directed(boost_graph)) {
116  std::vector<bool> already_matched(num_vertices(boost_graph), false);
117  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
118  vi != vi_end;
119  ++vi) {
120  /*
121  * For each vertex I check:
122  * 1) It is matched with a non-null vertex
123  * 2) An edge exists from this vertex to his mate
124  * 3) The vertices have not been matched already
125  * (this last point prevents having double output with reversed
126  * source and target)
127  */
128  boost::tie(e, exists) =
129  boost::edge(*vi, mate_map[*vi], boost_graph);
130  if (((uint64_t)mate_map[*vi]
131  != boost::graph_traits<G>::null_vertex())
132  && exists && !already_matched[*vi]
133  && !already_matched[mate_map[*vi]]) {
134  already_matched[*vi] = true;
135  already_matched[mate_map[*vi]] = true;
136  pgr_basic_edge_t matched_couple;
137  matched_couple.source = get_vertex_id(*vi);
138  matched_couple.target = get_vertex_id(mate_map[*vi]);
139  matched_couple.edge_id = get_edge_id(e);
140  matched_vertices.push_back(matched_couple);
141  }
142  }
143  } else {
144  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
145  vi != vi_end;
146  ++vi) {
147  boost::tie(e, exists) =
148  boost::edge(*vi, mate_map[*vi], boost_graph);
149  if (((uint64_t)mate_map[*vi]
150  != boost::graph_traits<G>::null_vertex())
151  && (*vi < (uint64_t)mate_map[*vi])) {
152  pgr_basic_edge_t matched_couple;
153  matched_couple.source = get_vertex_id(*vi);
154  matched_couple.target = get_vertex_id(mate_map[*vi]);
155  matched_couple.edge_id = get_edge_id(e);
156  matched_vertices.push_back(matched_couple);
157  }
158  }
159  }
160  }
161 
162  void maximum_cardinality_matching(std::vector<int64_t> &mate_map) {
163  edmonds_maximum_cardinality_matching(boost_graph,
164  &mate_map[0]);
165  }
166 };
167 
168 } // namespace flow
169 } // namespace pgrouting
170 
171 #endif // SRC_MAX_FLOW_SRC_PGR_MAXIMUMCARDINALITYMATCHING_HPP_
boost::graph_traits< G >::vertex_iterator V_it
void get_matched_vertices(std::vector< pgr_basic_edge_t > &matched_vertices, const std::vector< int64_t > &mate_map)
boost::adjacency_list< boost::listS, boost::vecS, boost::directedS > BasicDirectedGraph
boost::graph_traits< G >::vertex_descriptor V
void maximum_cardinality_matching(std::vector< int64_t > &mate_map)
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS > BasicUndirectedGraph
void create_max_cardinality_graph(pgr_basic_edge_t *data_edges, size_t total_tuples)
boost::graph_traits< G >::edge_descriptor E
boost::graph_traits< G >::edge_iterator E_it