PGROUTING  3.2
pgr_maxflow.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_MAXFLOW_HPP_
28 #define INCLUDE_MAX_FLOW_PGR_MAXFLOW_HPP_
29 #pragma once
30 
31 
32 #include "pgr_flowgraph.hpp"
33 #include <boost/graph/push_relabel_max_flow.hpp>
34 #include <boost/graph/edmonds_karp_max_flow.hpp>
35 #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
36 
37 
38 #include <map>
39 #include <string>
40 #include <utility>
41 #include <vector>
42 #include <set>
43 #include <limits>
44 
45 #include "c_types/pgr_flow_t.h"
46 #include "c_types/pgr_edge_t.h"
49 
50 
51 namespace pgrouting {
52 namespace graph {
53 
54 
55 class PgrFlowGraph {
56  typedef boost::graph_traits<FlowGraph>::vertex_descriptor V;
57  typedef boost::graph_traits<FlowGraph>::edge_descriptor E;
58  typedef boost::graph_traits<FlowGraph>::vertex_iterator V_it;
59  typedef boost::graph_traits<FlowGraph>::edge_iterator E_it;
60  typedef boost::graph_traits<FlowGraph>::out_edge_iterator Eout_it;
61 
62 
63  boost::property_map
64  <FlowGraph, boost::edge_capacity_t>::type capacity;
65  boost::property_map
66  <FlowGraph, boost::edge_reverse_t>::type rev;
67  boost::property_map
68  <FlowGraph, boost::edge_residual_capacity_t>::type residual_capacity;
69 
70 
71  public:
72  int64_t push_relabel() {
73  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
74  CHECK_FOR_INTERRUPTS();
75  return boost::push_relabel_max_flow(
76  graph,
78  supersink);
79  }
80 
81  int64_t edmonds_karp() {
82  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
83  CHECK_FOR_INTERRUPTS();
84  return boost::edmonds_karp_max_flow(
85  graph,
87  supersink);
88  }
89 
90  int64_t boykov_kolmogorov() {
91  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
92  CHECK_FOR_INTERRUPTS();
93  return boost::boykov_kolmogorov_max_flow(
94  graph,
96  supersink);
97  }
98 
99  std::vector<General_path_element_t> edge_disjoint_paths() {
100  /* abort in case of an interruption occurs (e.g. the query is being cancelled) */
101  CHECK_FOR_INTERRUPTS();
102  auto flow = boost::boykov_kolmogorov_max_flow(
103  graph,
104  supersource,
105  supersink);
106  return get_edge_disjoint_paths(static_cast<size_t>(flow));
107  }
108 
109  PgrFlowGraph(
110  const std::vector<pgr_edge_t> &edges,
111  const std::set<int64_t> &source_vertices,
112  const std::set<int64_t> &sink_vertices,
113  int algorithm);
114 
115  PgrFlowGraph(
116  const std::vector<pgr_edge_t> &edges,
117  const std::set<int64_t> &source_vertices,
118  const std::set<int64_t> &sink_vertices,
119  bool directed);
120 
121 
122  std::vector<pgr_flow_t> get_flow_edges() const;
123 
124  std::vector<General_path_element_t> get_edge_disjoint_paths(
125  size_t flow);
126 
127  private:
128  V get_boost_vertex(int64_t id) const {
129  return id_to_V.at(id);
130  }
131 
132  int64_t get_vertex_id(V v) const {
133  return V_to_id.at(v);
134  }
135 
136  int64_t get_edge_id(E e) const {
137  return E_to_id.at(e);
138  }
139 
140  void set_supersource(
141  const std::set<int64_t> &source_vertices);
142  void set_supersink(
143  const std::set<int64_t> &sink_vertices);
144 
146  const std::vector<pgr_edge_t> &edges);
147  void insert_edges(
148  const std::vector<pgr_edge_t> &edges);
150  const std::vector<pgr_edge_t> &edges,
151  bool directed);
152 
153  void flow_dfs(
154  V vertex,
155  size_t path_id,
156  std::vector<std::vector<int64_t> > &paths);
157 
158  /*
159  * vertices = {sources} U {sink} U {edges.source} U {edge.target}
160  */
161  template <typename T>
163  const T &edges,
164  const std::set<int64_t> &source_vertices,
165  const std::set<int64_t> &sink_vertices) {
166  std::set<int64_t> vertices(source_vertices);
167  vertices.insert(sink_vertices.begin(), sink_vertices.end());
168 
169  for (const auto e : edges) {
170  vertices.insert(e.source);
171  vertices.insert(e.target);
172  }
173 
174  for (const auto id : vertices) {
175  V v = add_vertex(graph);
176  id_to_V.insert(std::pair<int64_t, V>(id, v));
177  V_to_id.insert(std::pair<V, int64_t>(v, id));
178  }
179 
180  set_supersource(source_vertices);
181  set_supersink(sink_vertices);
182  }
183 
184  private:
186  std::map<int64_t, V> id_to_V;
187  std::map<V, int64_t> V_to_id;
188  std::map<E, int64_t> E_to_id;
189 
190 
191  /* In multi source flow graphs, a super source is created connected to all sources with "infinite" capacity
192  * The same applies for sinks.
193  * To avoid code repetition, a supersource/sink is used even in the one to one signature.
194  */
197 };
198 
199 } // namespace graph
200 } // namespace pgrouting
201 
202 #endif // INCLUDE_MAX_FLOW_PGR_MAXFLOW_HPP_
pgrouting::graph::PgrFlowGraph::supersink
V supersink
Definition: pgr_maxflow.hpp:196
interruption.h
pgrouting::graph::PgrFlowGraph::boykov_kolmogorov
int64_t boykov_kolmogorov()
Definition: pgr_maxflow.hpp:90
pgrouting::graph::PgrFlowGraph::V
boost::graph_traits< FlowGraph >::vertex_descriptor V
Definition: pgr_maxflow.hpp:56
pgrouting::graph::PgrFlowGraph::insert_edges
void insert_edges(const std::vector< pgr_edge_t > &edges)
Definition: pgr_maxflow.cpp:109
pgrouting::graph::PgrFlowGraph::set_supersink
void set_supersink(const std::set< int64_t > &sink_vertices)
Definition: pgr_maxflow.cpp:181
pgrouting::graph::PgrFlowGraph::get_flow_edges
std::vector< pgr_flow_t > get_flow_edges() const
Definition: pgr_maxflow.cpp:204
pgrouting::graph::PgrFlowGraph::id_to_V
std::map< int64_t, V > id_to_V
Definition: pgr_maxflow.hpp:186
pgrouting::FlowGraph
boost::adjacency_list< boost::listS, boost::vecS, boost::directedS, boost::property< boost::vertex_index_t, int64_t, boost::property< boost::vertex_color_t, boost::default_color_type, boost::property< boost::vertex_distance_t, int64_t, boost::property< boost::vertex_predecessor_t, Traits::edge_descriptor > > > >, boost::property< boost::edge_capacity_t, int64_t, boost::property< boost::edge_residual_capacity_t, int64_t, boost::property< boost::edge_reverse_t, Traits::edge_descriptor > > > > FlowGraph
Definition: pgr_flowgraph.hpp:50
pgrouting::graph::PgrFlowGraph::residual_capacity
boost::property_map< FlowGraph, boost::edge_residual_capacity_t >::type residual_capacity
Definition: pgr_maxflow.hpp:68
pgrouting::graph::PgrFlowGraph::add_vertices
void add_vertices(const T &edges, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices)
Definition: pgr_maxflow.hpp:162
pgrouting::graph::PgrFlowGraph::get_edge_disjoint_paths
std::vector< General_path_element_t > get_edge_disjoint_paths(size_t flow)
Definition: pgr_maxflow.cpp:254
pgrouting::graph::PgrFlowGraph::V_it
boost::graph_traits< FlowGraph >::vertex_iterator V_it
Definition: pgr_maxflow.hpp:58
pgrouting::graph::PgrFlowGraph::get_vertex_id
int64_t get_vertex_id(V v) const
Definition: pgr_maxflow.hpp:132
pgrouting::graph::PgrFlowGraph::capacity
boost::property_map< FlowGraph, boost::edge_capacity_t >::type capacity
Definition: pgr_maxflow.hpp:64
pgr_flowgraph.hpp
pgrouting::graph::PgrFlowGraph
Definition: pgr_maxflow.hpp:55
pgrouting::graph::PgrFlowGraph::Eout_it
boost::graph_traits< FlowGraph >::out_edge_iterator Eout_it
Definition: pgr_maxflow.hpp:60
pgrouting::graph::PgrFlowGraph::edge_disjoint_paths
std::vector< General_path_element_t > edge_disjoint_paths()
Definition: pgr_maxflow.hpp:99
pgrouting::graph::PgrFlowGraph::supersource
V supersource
Definition: pgr_maxflow.hpp:195
pgr_flow_t.h
pgrouting::graph::PgrFlowGraph::push_relabel
int64_t push_relabel()
Definition: pgr_maxflow.hpp:72
pgrouting::graph::PgrFlowGraph::rev
boost::property_map< FlowGraph, boost::edge_reverse_t >::type rev
Definition: pgr_maxflow.hpp:66
pgrouting::graph::PgrFlowGraph::V_to_id
std::map< V, int64_t > V_to_id
Definition: pgr_maxflow.hpp:187
pgrouting::graph::PgrFlowGraph::edmonds_karp
int64_t edmonds_karp()
Definition: pgr_maxflow.hpp:81
pgrouting::graph::PgrFlowGraph::get_boost_vertex
V get_boost_vertex(int64_t id) const
Definition: pgr_maxflow.hpp:128
pgrouting::graph::PgrFlowGraph::E
boost::graph_traits< FlowGraph >::edge_descriptor E
Definition: pgr_maxflow.hpp:57
pgrouting::graph::PgrFlowGraph::get_edge_id
int64_t get_edge_id(E e) const
Definition: pgr_maxflow.hpp:136
pgrouting::graph::PgrFlowGraph::PgrFlowGraph
PgrFlowGraph(const std::vector< pgr_edge_t > &edges, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices, int algorithm)
Definition: pgr_maxflow.cpp:38
general_path_element_t.h
pgrouting::graph::PgrFlowGraph::flow_dfs
void flow_dfs(V vertex, size_t path_id, std::vector< std::vector< int64_t > > &paths)
Definition: pgr_maxflow.cpp:228
pgrouting::graph::PgrFlowGraph::E_to_id
std::map< E, int64_t > E_to_id
Definition: pgr_maxflow.hpp:188
pgr_edge_t.h
pgrouting::graph::PgrFlowGraph::E_it
boost::graph_traits< FlowGraph >::edge_iterator E_it
Definition: pgr_maxflow.hpp:59
pgrouting::graph::PgrFlowGraph::graph
FlowGraph graph
Definition: pgr_maxflow.hpp:185
pgrouting::graph::PgrFlowGraph::set_supersource
void set_supersource(const std::set< int64_t > &source_vertices)
Definition: pgr_maxflow.cpp:161
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::graph::PgrFlowGraph::insert_edges_edge_disjoint
void insert_edges_edge_disjoint(const std::vector< pgr_edge_t > &edges, bool directed)
Definition: pgr_maxflow.cpp:132
pgrouting::graph::PgrFlowGraph::insert_edges_push_relabel
void insert_edges_push_relabel(const std::vector< pgr_edge_t > &edges)
Definition: pgr_maxflow.cpp:74