-
-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathmaxflow.cpp
More file actions
363 lines (326 loc) · 11.4 KB
/
Copy pathmaxflow.cpp
File metadata and controls
363 lines (326 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*PGR-GNU*****************************************************************
File: maxflow.cpp
Copyright (c) 2013-2026 pgRouting developers
Mail: project@pgrouting.org
Copyright (c) 2016 Andrea Nardelli
Mail: nrd.nardelli@gmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#include "max_flow/maxflow.hpp"
#include <limits>
#include <utility>
#include <vector>
#include <set>
#include <map>
namespace {
std::vector<Path_rt>
single_execution(
std::vector<Edge_t> edges,
int64_t source,
int64_t target,
bool directed) {
std::set<int64_t> set_source_vertices;
std::set<int64_t> set_sink_vertices;
set_source_vertices.insert(source);
set_sink_vertices.insert(target);
pgrouting::graph::PgrFlowGraph G(
edges,
set_source_vertices,
set_sink_vertices, directed);
/*
* boykov_kolmogorov is only for directed graphs
*/
return G.edge_disjoint_paths();
}
} // namespace
namespace pgrouting {
namespace graph {
PgrFlowGraph::PgrFlowGraph(
const std::vector<Edge_t> &edges,
const std::set<int64_t> &source_vertices,
const std::set<int64_t> &sink_vertices,
Which algorithm) {
add_vertices(edges, source_vertices, sink_vertices);
capacity = get(boost::edge_capacity, graph);
rev = get(boost::edge_reverse, graph);
residual_capacity = get(boost::edge_residual_capacity, graph);
switch (algorithm) {
case MAXFLOW:
case PUSHRELABEL:
insert_edges_push_relabel(edges);
break;
case BOYKOV:
case EDMONDSKARP:
insert_edges(edges);
break;
default:
{};
/* Maybe do a throw */
}
}
PgrFlowGraph::PgrFlowGraph(
const std::vector<Edge_t> &edges,
const std::set<int64_t> &source_vertices,
const std::set<int64_t> &sink_vertices,
bool directed) {
add_vertices(edges, source_vertices, sink_vertices);
capacity = get(boost::edge_capacity, graph);
rev = get(boost::edge_reverse, graph);
residual_capacity =
get(boost::edge_residual_capacity, graph);
insert_edges_edge_disjoint(edges, directed);
}
/* Inserting edges
* Push-relabel requires each edge to be mapped to its reverse with capacity 0.
*/
void PgrFlowGraph::insert_edges_push_relabel(
const std::vector<Edge_t> &edges) {
bool added = false;
for (const auto edge : edges) {
V v1 = get_boost_vertex(edge.source);
V v2 = get_boost_vertex(edge.target);
E e1, e1_rev, e2, e2_rev;
if (edge.cost > 0) {
boost::tie(e1, added) = boost::add_edge(v1, v2, graph);
boost::tie(e1_rev, added) =
boost::add_edge(v2, v1, graph);
E_to_id.insert(std::pair<E, int64_t>(e1, edge.id));
E_to_id.insert(std::pair<E, int64_t>(e1_rev, edge.id));
capacity[e1] = static_cast<int64_t>(edge.cost);
capacity[e1_rev] = 0;
rev[e1] = e1_rev;
rev[e1_rev] = e1;
}
if (edge.reverse_cost > 0) {
boost::tie(e2, added) = boost::add_edge(v2, v1, graph);
boost::tie(e2_rev, added) =
boost::add_edge(v1, v2, graph);
E_to_id.insert(std::pair<E, int64_t>(e2, edge.id));
E_to_id.insert(std::pair<E, int64_t>(e2_rev, edge.id));
capacity[e2] = static_cast<int64_t>(edge.reverse_cost);
capacity[e2_rev] = 0;
rev[e2] = e2_rev;
rev[e2_rev] = e2;
}
}
}
/* Inserting edges
* The other pgr_maxflow algorithms have no such requirement. (can have have as many edges)
*/
void PgrFlowGraph::insert_edges(
const std::vector<Edge_t> &edges) {
bool added = false;
for (const auto edge : edges) {
V v1 = get_boost_vertex(edge.source);
V v2 = get_boost_vertex(edge.target);
E e, e_rev;
boost::tie(e, added) = boost::add_edge(v1, v2, graph);
boost::tie(e_rev, added) =
boost::add_edge(v2, v1, graph);
E_to_id.insert(std::pair<E, int64_t>(e, edge.id));
E_to_id.insert(std::pair<E, int64_t>(e_rev, edge.id));
capacity[e] = edge.cost > 0 ? static_cast<int64_t>(edge.cost) : 0;
capacity[e_rev] = edge.reverse_cost > 0
? static_cast<int64_t>(edge.reverse_cost) : 0;
rev[e] = e_rev;
rev[e_rev] = e;
}
}
/* Inserting edges
* for the edge_disjoint_paths algorithms
*/
void PgrFlowGraph::insert_edges_edge_disjoint(
const std::vector<Edge_t> &edges,
bool directed) {
bool added = false;
for (const auto edge : edges) {
V v1 = get_boost_vertex(edge.source);
V v2 = get_boost_vertex(edge.target);
E e, e_rev;
boost::tie(e, added) =
boost::add_edge(v1, v2, graph);
boost::tie(e_rev, added) =
boost::add_edge(v2, v1, graph);
E_to_id.insert(std::pair<E, int64_t>(e, edge.id));
E_to_id.insert(std::pair<E, int64_t>(e_rev,
edge.id));
if (directed) {
capacity[e] = edge.cost >= 0 ? 1 : 0;
capacity[e_rev] = edge.reverse_cost >= 0 ? 1 : 0;
} else {
if (edge.cost >= 0 || edge.reverse_cost >= 0) {
capacity[e] = 1;
capacity[e_rev] = 1;
}
}
rev[e] = e_rev;
rev[e_rev] = e;
}
}
void PgrFlowGraph::set_supersource(
const std::set<int64_t> &source_vertices) {
bool added = false;
supersource = add_vertex(graph);
for (int64_t source_id : source_vertices) {
V source = get_boost_vertex(source_id);
E e, e_rev;
boost::tie(e, added) =
boost::add_edge(supersource, source, graph);
boost::tie(e_rev, added) =
boost::add_edge(source, supersource, graph);
capacity[e] = (std::numeric_limits<int32_t>::max)();
/* From sources to supersource has 0 capacity*/
capacity[e_rev] = 0;
rev[e] = e_rev;
rev[e_rev] = e;
}
}
void PgrFlowGraph::set_supersink(
const std::set<int64_t> &sink_vertices) {
bool added = false;
supersink = add_vertex(graph);
for (int64_t sink_id : sink_vertices) {
V sink = get_boost_vertex(sink_id);
E e, e_rev;
boost::tie(e, added) = boost::add_edge(sink, supersink, graph);
boost::tie(e_rev, added) =
boost::add_edge(supersink, sink, graph);
/*
* NOTE: int64_t crashes the server
*/
/* From sinks to supersink has maximum capacity*/
capacity[e] = (std::numeric_limits<int32_t>::max)();
/* From supersink to sinks has 0 capacity*/
capacity[e_rev] = 0;
rev[e] = e_rev;
rev[e_rev] = e;
}
}
std::vector<Flow_t>
PgrFlowGraph::get_flow_edges() const {
std::vector<Flow_t> flow_edges;
E_it e, e_end;
for (boost::tie(e, e_end) = boost::edges(graph); e != e_end;
++e) {
// A supersource/supersink is used internally
if (((capacity[*e] - residual_capacity[*e]) > 0) &&
((*e).m_source != supersource) &&
((*e).m_target != supersink)) {
Flow_t edge = {};
edge.edge = get_edge_id(*e);
edge.source = get_vertex_id((*e).m_source);
edge.target = get_vertex_id((*e).m_target);
edge.flow = capacity[*e] - residual_capacity[*e];
edge.residual_capacity = residual_capacity[*e];
flow_edges.push_back(edge);
}
}
return flow_edges;
}
void
PgrFlowGraph::flow_dfs(V vertex,
size_t path_id,
std::vector<std::vector<int64_t> > &paths) {
Eout_it ei, e_end;
if (boost::edge(vertex, supersink, graph).second) {
int64_t v_id = get_vertex_id(vertex);
paths[path_id].push_back(v_id);
} else {
for (boost::tie(ei, e_end) =
boost::out_edges(vertex, graph);
ei != e_end; ++ei) {
if (residual_capacity[*ei] < capacity[*ei]) {
// exclude this edge from subsequent visits
capacity[*ei] = -1;
int64_t v_id = get_vertex_id(vertex);
paths[path_id].push_back(v_id);
flow_dfs((*ei).m_target,
path_id,
paths);
break;
}
}
}
}
std::vector<Path_rt>
PgrFlowGraph::get_edge_disjoint_paths(
size_t flow) {
std::vector<Path_rt> path_elements;
std::vector<std::vector<int64_t> > paths(flow, std::vector<int64_t>());
size_t path_id = 0;
Eout_it ei, e_end, ei2, e2_end;
for (boost::tie(ei, e_end) =
boost::out_edges(supersource, graph);
ei != e_end; ++ei) {
if (capacity[*ei] - residual_capacity[*ei] > 0) {
for (boost::tie(ei2, e2_end) =
boost::out_edges((*ei).m_target, graph);
ei2 != e2_end; ++ei2) {
if (capacity[*ei2] - residual_capacity[*ei2]
> 0) {
paths[path_id].push_back(get_vertex_id((*ei2).m_source));
flow_dfs((*ei2).m_target, path_id, paths);
path_id++;
}
}
}
}
for (size_t i = 0; i < flow; i++) {
size_t size = paths[i].size();
E e;
bool exists = false;
size_t j = 0;
for (j = 0; j < size - 1; j++) {
Path_rt edge = {};
edge.start_id = paths[i][0];
edge.end_id = paths[i][size - 1];
edge.node = paths[i][j];
boost::tie(e, exists) = boost::edge(get_boost_vertex(paths[i][j]),
get_boost_vertex(paths[i][j + 1]),
graph);
edge.edge = get_edge_id(e);
path_elements.push_back(edge);
}
Path_rt edge = {};
edge.start_id = paths[i][0];
edge.end_id = paths[i][size - 1];
edge.node = paths[i][j];
edge.edge = -1;
path_elements.push_back(edge);
}
return path_elements;
}
} // namespace graph
namespace functions {
std::vector<Path_rt>
edgeDisjoint(
std::vector<Edge_t> edges,
const std::map<int64_t, std::set<int64_t>> & combinations,
bool directed) {
std::vector<Path_rt> results;
for (const auto &c : combinations) {
for (const auto &t : c.second) {
/*
* a source can not be a sink
* aka there is no path
*/
if (c.first == t) continue;
auto result = single_execution(edges, c.first, t, directed);
results.insert(results.end(), result.begin(), result.end());
}
}
return results;
}
} // namespace functions
} // namespace pgrouting