pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 #pragma once
28 
29 #if defined(__MINGW32__) || defined(_MSC_VER)
30 #include <winsock2.h>
31 #include <windows.h>
32 #ifdef unlink
33 #undef unlink
34 #endif
35 #endif
36 
37 #include <boost/config.hpp>
38 #include <boost/graph/adjacency_list.hpp>
39 #include <boost/graph/max_cardinality_matching.hpp>
40 
41 #if 0
42 #include "./../../common/src/signalhandler.h"
43 #endif
44 #include "./../../common/src/pgr_types.h"
45 
46 #include <map>
47 #include <vector>
48 #include <utility>
49 #include <set>
50 
51 
52 // user's functions
53 // for development
54 
55 typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS>
57 typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS>
59 
60 template<class G>
62  public:
64 
65  typedef typename boost::graph_traits<G>::vertex_descriptor V;
66  typedef typename boost::graph_traits<G>::edge_descriptor E;
67  typedef typename boost::graph_traits<G>::vertex_iterator V_it;
68  typedef typename boost::graph_traits<G>::edge_iterator E_it;
69 
70  std::map<int64_t, V> id_to_V;
71  std::map<V, int64_t> V_to_id;
72  std::map<E, int64_t> E_to_id;
73 
74  V get_boost_vertex(int64_t id) {
75  return id_to_V[id];
76  }
77 
78  int64_t get_vertex_id(V v) {
79  return V_to_id[v];
80  }
81 
82  int64_t get_edge_id(E e) {
83  return E_to_id[e];
84  }
85 
87  size_t total_tuples) {
88  std::set<int64_t> vertices;
89  for (size_t i = 0; i < total_tuples; ++i) {
90  vertices.insert(data_edges[i].source);
91  vertices.insert(data_edges[i].target);
92  }
93  for (int64_t id : vertices) {
94  V v = add_vertex(boost_graph);
95  id_to_V.insert(std::pair<int64_t, V>(id, v));
96  V_to_id.insert(std::pair<V, int64_t>(v, id));
97  }
98  bool added;
99 
100  for (size_t i = 0; i < total_tuples; ++i) {
101  V v1 = get_boost_vertex(data_edges[i].source);
102  V v2 = get_boost_vertex(data_edges[i].target);
103  E e1;
104  E e2;
105  if (data_edges[i].going) {
106  boost::tie(e1, added) = boost::add_edge(v1, v2, boost_graph);
107  E_to_id.insert(std::pair<E, int64_t>(e1, data_edges[i].id));
108  }
109  if (data_edges[i].coming) {
110  boost::tie(e2, added) = boost::add_edge(v2, v1, boost_graph);
111  E_to_id.insert(std::pair<E, int64_t>(e2, data_edges[i].id));
112  }
113  }
114  }
115 
116  void get_matched_vertices(std::vector<pgr_basic_edge_t> &matched_vertices,
117  const std::vector<int64_t> &mate_map) {
118  V_it vi, vi_end;
119  E e;
120  bool exists;
121  if (boost::is_directed(boost_graph)) {
122  std::vector<bool> already_matched(num_vertices(boost_graph), false);
123  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
124  vi != vi_end;
125  ++vi) {
126  /*
127  * For each vertex I check:
128  * 1) It is matched with a non-null vertex
129  * 2) An edge exists from this vertex to his mate
130  * 3) The vertices have not been matched already
131  * (this last point prevents having double output with reversed
132  * source and target)
133  */
134  boost::tie(e, exists) =
135  boost::edge(*vi, mate_map[*vi], boost_graph);
136  if (((uint64_t)mate_map[*vi] != boost::graph_traits<G>::null_vertex())
137  && exists && !already_matched[*vi]
138  && !already_matched[mate_map[*vi]]) {
139  already_matched[*vi] = true;
140  already_matched[mate_map[*vi]] = true;
141  pgr_basic_edge_t matched_couple;
142  matched_couple.source = get_vertex_id(*vi);
143  matched_couple.target = get_vertex_id(mate_map[*vi]);
144  matched_couple.edge_id = get_edge_id(e);
145  matched_vertices.push_back(matched_couple);
146  }
147  }
148  } else {
149  for (boost::tie(vi, vi_end) = boost::vertices(boost_graph);
150  vi != vi_end;
151  ++vi) {
152  boost::tie(e, exists) =
153  boost::edge(*vi, mate_map[*vi], boost_graph);
154  if (((uint64_t)mate_map[*vi] != boost::graph_traits<G>::null_vertex())
155  && (*vi < (uint64_t)mate_map[*vi])) {
156  pgr_basic_edge_t matched_couple;
157  matched_couple.source = get_vertex_id(*vi);
158  matched_couple.target = get_vertex_id(mate_map[*vi]);
159  matched_couple.edge_id = get_edge_id(e);
160  matched_vertices.push_back(matched_couple);
161  }
162  }
163  }
164  }
165 
166  void maximum_cardinality_matching(std::vector<int64_t> &mate_map) {
167  edmonds_maximum_cardinality_matching(boost_graph,
168  &mate_map[0]);
169  }
170 };
void get_matched_vertices(std::vector< pgr_basic_edge_t > &matched_vertices, const std::vector< int64_t > &mate_map)
int64_t edge_id
Definition: pgr_types.h:124
boost::graph_traits< G >::vertex_descriptor V
void create_max_cardinality_graph(pgr_basic_edge_t *data_edges, size_t total_tuples)
boost::adjacency_list< boost::listS, boost::vecS, boost::directedS > BasicDirectedGraph
int64_t source
Definition: pgr_types.h:120
int64_t target
Definition: pgr_types.h:121
boost::graph_traits< G >::edge_descriptor E
boost::graph_traits< G >::vertex_iterator V_it
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS > BasicUndirectedGraph
boost::graph_traits< G >::edge_iterator E_it
void maximum_cardinality_matching(std::vector< int64_t > &mate_map)