deba@2440
|
1 |
/* -*- C++ -*-
|
deba@2440
|
2 |
*
|
deba@2440
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2440
|
4 |
*
|
deba@2440
|
5 |
* Copyright (C) 2003-2007
|
deba@2440
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2440
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2440
|
8 |
*
|
deba@2440
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2440
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2440
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2440
|
12 |
*
|
deba@2440
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2440
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2440
|
15 |
* purpose.
|
deba@2440
|
16 |
*
|
deba@2440
|
17 |
*/
|
deba@2440
|
18 |
|
deba@2440
|
19 |
#ifndef LEMON_CYCLE_CANCELING_H
|
deba@2440
|
20 |
#define LEMON_CYCLE_CANCELING_H
|
deba@2440
|
21 |
|
deba@2440
|
22 |
/// \ingroup min_cost_flow
|
deba@2440
|
23 |
///
|
deba@2440
|
24 |
/// \file
|
deba@2457
|
25 |
/// \brief A cycle-canceling algorithm for finding a minimum cost flow.
|
deba@2440
|
26 |
|
deba@2440
|
27 |
#include <vector>
|
kpeter@2509
|
28 |
#include <lemon/graph_adaptor.h>
|
deba@2440
|
29 |
#include <lemon/circulation.h>
|
deba@2440
|
30 |
|
deba@2457
|
31 |
/// \brief The used cycle-canceling method.
|
deba@2440
|
32 |
#define LIMITED_CYCLE_CANCELING
|
deba@2440
|
33 |
//#define MIN_MEAN_CYCLE_CANCELING
|
deba@2440
|
34 |
|
deba@2440
|
35 |
#ifdef LIMITED_CYCLE_CANCELING
|
deba@2440
|
36 |
#include <lemon/bellman_ford.h>
|
deba@2440
|
37 |
/// \brief The maximum number of iterations for the first execution
|
deba@2440
|
38 |
/// of the \ref lemon::BellmanFord "Bellman-Ford" algorithm.
|
deba@2457
|
39 |
/// It should be at least 2.
|
deba@2440
|
40 |
#define STARTING_LIMIT 2
|
deba@2440
|
41 |
/// \brief The iteration limit for the
|
deba@2440
|
42 |
/// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
|
deba@2457
|
43 |
/// <tt>ALPHA_MUL / ALPHA_DIV</tt> in every round.
|
deba@2457
|
44 |
/// <tt>ALPHA_MUL / ALPHA_DIV</tt> must be greater than 1.
|
deba@2440
|
45 |
#define ALPHA_MUL 3
|
deba@2440
|
46 |
/// \brief The iteration limit for the
|
deba@2440
|
47 |
/// \ref lemon::BellmanFord "Bellman-Ford" algorithm is multiplied by
|
deba@2457
|
48 |
/// <tt>ALPHA_MUL / ALPHA_DIV</tt> in every round.
|
deba@2457
|
49 |
/// <tt>ALPHA_MUL / ALPHA_DIV</tt> must be greater than 1.
|
deba@2440
|
50 |
#define ALPHA_DIV 2
|
deba@2440
|
51 |
|
deba@2440
|
52 |
//#define _ONLY_ONE_CYCLE_
|
deba@2457
|
53 |
//#define _NO_BACK_STEP_
|
deba@2440
|
54 |
//#define _DEBUG_ITER_
|
deba@2440
|
55 |
#endif
|
deba@2440
|
56 |
|
deba@2440
|
57 |
#ifdef MIN_MEAN_CYCLE_CANCELING
|
deba@2440
|
58 |
#include <lemon/min_mean_cycle.h>
|
deba@2440
|
59 |
#include <lemon/path.h>
|
deba@2440
|
60 |
#endif
|
deba@2440
|
61 |
|
deba@2440
|
62 |
namespace lemon {
|
deba@2440
|
63 |
|
deba@2440
|
64 |
/// \addtogroup min_cost_flow
|
deba@2440
|
65 |
/// @{
|
deba@2440
|
66 |
|
deba@2457
|
67 |
/// \brief Implementation of a cycle-canceling algorithm for finding
|
deba@2440
|
68 |
/// a minimum cost flow.
|
deba@2440
|
69 |
///
|
deba@2457
|
70 |
/// \ref lemon::CycleCanceling "CycleCanceling" implements a
|
deba@2457
|
71 |
/// cycle-canceling algorithm for finding a minimum cost flow.
|
deba@2440
|
72 |
///
|
deba@2440
|
73 |
/// \param Graph The directed graph type the algorithm runs on.
|
deba@2440
|
74 |
/// \param LowerMap The type of the lower bound map.
|
deba@2440
|
75 |
/// \param CapacityMap The type of the capacity (upper bound) map.
|
deba@2440
|
76 |
/// \param CostMap The type of the cost (length) map.
|
deba@2440
|
77 |
/// \param SupplyMap The type of the supply map.
|
deba@2440
|
78 |
///
|
deba@2440
|
79 |
/// \warning
|
deba@2440
|
80 |
/// - Edge capacities and costs should be nonnegative integers.
|
deba@2440
|
81 |
/// However \c CostMap::Value should be signed type.
|
kpeter@2509
|
82 |
/// - Supply values should be signed integers.
|
deba@2440
|
83 |
/// - \c LowerMap::Value must be convertible to
|
deba@2440
|
84 |
/// \c CapacityMap::Value and \c CapacityMap::Value must be
|
deba@2440
|
85 |
/// convertible to \c SupplyMap::Value.
|
deba@2440
|
86 |
///
|
deba@2440
|
87 |
/// \author Peter Kovacs
|
deba@2440
|
88 |
|
deba@2440
|
89 |
template < typename Graph,
|
deba@2440
|
90 |
typename LowerMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
91 |
typename CapacityMap = LowerMap,
|
deba@2440
|
92 |
typename CostMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
93 |
typename SupplyMap = typename Graph::template NodeMap
|
deba@2440
|
94 |
<typename CapacityMap::Value> >
|
deba@2440
|
95 |
class CycleCanceling
|
deba@2440
|
96 |
{
|
deba@2440
|
97 |
typedef typename Graph::Node Node;
|
deba@2440
|
98 |
typedef typename Graph::NodeIt NodeIt;
|
deba@2440
|
99 |
typedef typename Graph::Edge Edge;
|
deba@2440
|
100 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@2440
|
101 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
deba@2440
|
102 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
deba@2440
|
103 |
|
deba@2440
|
104 |
typedef typename LowerMap::Value Lower;
|
deba@2440
|
105 |
typedef typename CapacityMap::Value Capacity;
|
deba@2440
|
106 |
typedef typename CostMap::Value Cost;
|
deba@2440
|
107 |
typedef typename SupplyMap::Value Supply;
|
deba@2440
|
108 |
typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
|
deba@2440
|
109 |
typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
|
deba@2440
|
110 |
|
deba@2440
|
111 |
typedef ResGraphAdaptor< const Graph, Capacity,
|
deba@2440
|
112 |
CapacityRefMap, CapacityRefMap > ResGraph;
|
deba@2440
|
113 |
typedef typename ResGraph::Node ResNode;
|
deba@2440
|
114 |
typedef typename ResGraph::NodeIt ResNodeIt;
|
deba@2440
|
115 |
typedef typename ResGraph::Edge ResEdge;
|
deba@2440
|
116 |
typedef typename ResGraph::EdgeIt ResEdgeIt;
|
deba@2440
|
117 |
|
deba@2440
|
118 |
public:
|
deba@2440
|
119 |
|
deba@2440
|
120 |
/// \brief The type of the flow map.
|
deba@2440
|
121 |
typedef CapacityRefMap FlowMap;
|
deba@2440
|
122 |
|
deba@2440
|
123 |
protected:
|
deba@2440
|
124 |
|
deba@2440
|
125 |
/// \brief Map adaptor class for handling residual edge costs.
|
deba@2440
|
126 |
class ResCostMap : public MapBase<ResEdge, Cost>
|
deba@2440
|
127 |
{
|
deba@2440
|
128 |
private:
|
deba@2440
|
129 |
|
deba@2440
|
130 |
const CostMap &cost_map;
|
deba@2440
|
131 |
|
deba@2440
|
132 |
public:
|
deba@2440
|
133 |
|
deba@2440
|
134 |
ResCostMap(const CostMap &_cost) : cost_map(_cost) {}
|
deba@2440
|
135 |
|
kpeter@2509
|
136 |
Cost operator[](const ResEdge &e) const {
|
deba@2440
|
137 |
return ResGraph::forward(e) ? cost_map[e] : -cost_map[e];
|
deba@2440
|
138 |
}
|
deba@2440
|
139 |
|
deba@2440
|
140 |
}; //class ResCostMap
|
deba@2440
|
141 |
|
deba@2440
|
142 |
protected:
|
deba@2440
|
143 |
|
deba@2440
|
144 |
/// \brief The directed graph the algorithm runs on.
|
deba@2440
|
145 |
const Graph &graph;
|
deba@2440
|
146 |
/// \brief The original lower bound map.
|
deba@2440
|
147 |
const LowerMap *lower;
|
deba@2440
|
148 |
/// \brief The modified capacity map.
|
deba@2440
|
149 |
CapacityRefMap capacity;
|
deba@2440
|
150 |
/// \brief The cost map.
|
deba@2440
|
151 |
const CostMap &cost;
|
deba@2440
|
152 |
/// \brief The modified supply map.
|
deba@2440
|
153 |
SupplyRefMap supply;
|
deba@2440
|
154 |
/// \brief The sum of supply values equals zero.
|
deba@2440
|
155 |
bool valid_supply;
|
deba@2440
|
156 |
|
deba@2440
|
157 |
/// \brief The current flow.
|
deba@2440
|
158 |
FlowMap flow;
|
deba@2440
|
159 |
/// \brief The residual graph.
|
deba@2440
|
160 |
ResGraph res_graph;
|
deba@2440
|
161 |
/// \brief The residual cost map.
|
deba@2440
|
162 |
ResCostMap res_cost;
|
deba@2440
|
163 |
|
deba@2440
|
164 |
public :
|
deba@2440
|
165 |
|
deba@2440
|
166 |
/// \brief General constructor of the class (with lower bounds).
|
deba@2440
|
167 |
///
|
deba@2440
|
168 |
/// General constructor of the class (with lower bounds).
|
deba@2440
|
169 |
///
|
deba@2440
|
170 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
171 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
172 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
173 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
174 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
175 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
176 |
const LowerMap &_lower,
|
deba@2440
|
177 |
const CapacityMap &_capacity,
|
deba@2440
|
178 |
const CostMap &_cost,
|
deba@2440
|
179 |
const SupplyMap &_supply ) :
|
deba@2440
|
180 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2457
|
181 |
supply(_graph), flow(_graph, 0),
|
deba@2440
|
182 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
183 |
{
|
deba@2440
|
184 |
// Removing nonzero lower bounds
|
deba@2440
|
185 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
186 |
Supply sum = 0;
|
deba@2440
|
187 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
188 |
Supply s = _supply[n];
|
deba@2440
|
189 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
190 |
s += _lower[e];
|
deba@2440
|
191 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
192 |
s -= _lower[e];
|
deba@2440
|
193 |
sum += (supply[n] = s);
|
deba@2440
|
194 |
}
|
deba@2440
|
195 |
valid_supply = sum == 0;
|
deba@2440
|
196 |
}
|
deba@2440
|
197 |
|
deba@2440
|
198 |
/// \brief General constructor of the class (without lower bounds).
|
deba@2440
|
199 |
///
|
deba@2440
|
200 |
/// General constructor of the class (without lower bounds).
|
deba@2440
|
201 |
///
|
deba@2440
|
202 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
203 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
204 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
205 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
206 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
207 |
const CapacityMap &_capacity,
|
deba@2440
|
208 |
const CostMap &_cost,
|
deba@2440
|
209 |
const SupplyMap &_supply ) :
|
deba@2440
|
210 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2457
|
211 |
supply(_supply), flow(_graph, 0),
|
deba@2440
|
212 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
213 |
{
|
deba@2440
|
214 |
// Checking the sum of supply values
|
deba@2440
|
215 |
Supply sum = 0;
|
deba@2440
|
216 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
217 |
valid_supply = sum == 0;
|
deba@2440
|
218 |
}
|
deba@2440
|
219 |
|
deba@2440
|
220 |
|
deba@2440
|
221 |
/// \brief Simple constructor of the class (with lower bounds).
|
deba@2440
|
222 |
///
|
deba@2440
|
223 |
/// Simple constructor of the class (with lower bounds).
|
deba@2440
|
224 |
///
|
deba@2440
|
225 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
226 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
227 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
228 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
229 |
/// \param _s The source node.
|
deba@2440
|
230 |
/// \param _t The target node.
|
deba@2440
|
231 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
232 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
233 |
/// \c _t).
|
deba@2440
|
234 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
235 |
const LowerMap &_lower,
|
deba@2440
|
236 |
const CapacityMap &_capacity,
|
deba@2440
|
237 |
const CostMap &_cost,
|
deba@2440
|
238 |
Node _s, Node _t,
|
deba@2440
|
239 |
Supply _flow_value ) :
|
deba@2440
|
240 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2457
|
241 |
supply(_graph), flow(_graph, 0),
|
deba@2440
|
242 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
243 |
{
|
deba@2440
|
244 |
// Removing nonzero lower bounds
|
deba@2440
|
245 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
246 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
247 |
Supply s = 0;
|
deba@2440
|
248 |
if (n == _s) s = _flow_value;
|
deba@2440
|
249 |
if (n == _t) s = -_flow_value;
|
deba@2440
|
250 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
251 |
s += _lower[e];
|
deba@2440
|
252 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
253 |
s -= _lower[e];
|
deba@2440
|
254 |
supply[n] = s;
|
deba@2440
|
255 |
}
|
deba@2440
|
256 |
valid_supply = true;
|
deba@2440
|
257 |
}
|
deba@2440
|
258 |
|
deba@2440
|
259 |
/// \brief Simple constructor of the class (without lower bounds).
|
deba@2440
|
260 |
///
|
deba@2440
|
261 |
/// Simple constructor of the class (without lower bounds).
|
deba@2440
|
262 |
///
|
deba@2440
|
263 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
264 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
265 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
266 |
/// \param _s The source node.
|
deba@2440
|
267 |
/// \param _t The target node.
|
deba@2440
|
268 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
269 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
270 |
/// \c _t).
|
deba@2440
|
271 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
272 |
const CapacityMap &_capacity,
|
deba@2440
|
273 |
const CostMap &_cost,
|
deba@2440
|
274 |
Node _s, Node _t,
|
deba@2440
|
275 |
Supply _flow_value ) :
|
deba@2440
|
276 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2457
|
277 |
supply(_graph, 0), flow(_graph, 0),
|
deba@2440
|
278 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
279 |
{
|
deba@2440
|
280 |
supply[_s] = _flow_value;
|
deba@2440
|
281 |
supply[_t] = -_flow_value;
|
deba@2440
|
282 |
valid_supply = true;
|
deba@2440
|
283 |
}
|
deba@2440
|
284 |
|
deba@2440
|
285 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
286 |
///
|
deba@2440
|
287 |
/// Returns a const reference to the flow map.
|
deba@2440
|
288 |
///
|
deba@2440
|
289 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
290 |
const FlowMap& flowMap() const {
|
deba@2440
|
291 |
return flow;
|
deba@2440
|
292 |
}
|
deba@2440
|
293 |
|
deba@2440
|
294 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
295 |
///
|
deba@2440
|
296 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
297 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
298 |
///
|
deba@2440
|
299 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
300 |
Cost totalCost() const {
|
deba@2440
|
301 |
Cost c = 0;
|
deba@2440
|
302 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
303 |
c += flow[e] * cost[e];
|
deba@2440
|
304 |
return c;
|
deba@2440
|
305 |
}
|
deba@2440
|
306 |
|
deba@2440
|
307 |
/// \brief Runs the algorithm.
|
deba@2440
|
308 |
///
|
deba@2440
|
309 |
/// Runs the algorithm.
|
deba@2440
|
310 |
///
|
deba@2440
|
311 |
/// \return \c true if a feasible flow can be found.
|
deba@2440
|
312 |
bool run() {
|
deba@2440
|
313 |
return init() && start();
|
deba@2440
|
314 |
}
|
deba@2440
|
315 |
|
deba@2440
|
316 |
protected:
|
deba@2440
|
317 |
|
deba@2440
|
318 |
/// \brief Initializes the algorithm.
|
deba@2440
|
319 |
bool init() {
|
deba@2440
|
320 |
// Checking the sum of supply values
|
deba@2440
|
321 |
Supply sum = 0;
|
deba@2440
|
322 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
323 |
if (sum != 0) return false;
|
deba@2440
|
324 |
|
deba@2440
|
325 |
// Finding a feasible flow
|
deba@2440
|
326 |
Circulation< Graph, Capacity, FlowMap, ConstMap<Edge, Capacity>,
|
deba@2457
|
327 |
CapacityRefMap, SupplyMap >
|
deba@2440
|
328 |
circulation( graph, constMap<Edge>((Capacity)0),
|
deba@2457
|
329 |
capacity, supply, flow );
|
deba@2440
|
330 |
return circulation.run() == -1;
|
deba@2440
|
331 |
}
|
deba@2440
|
332 |
|
deba@2440
|
333 |
#ifdef LIMITED_CYCLE_CANCELING
|
deba@2457
|
334 |
/// \brief Executes a cycle-canceling algorithm using
|
deba@2440
|
335 |
/// \ref lemon::BellmanFord "Bellman-Ford" algorithm with limited
|
deba@2440
|
336 |
/// iteration count.
|
deba@2440
|
337 |
bool start() {
|
deba@2440
|
338 |
typename BellmanFord<ResGraph, ResCostMap>::PredMap pred(res_graph);
|
deba@2440
|
339 |
typename ResGraph::template NodeMap<int> visited(res_graph);
|
deba@2440
|
340 |
std::vector<ResEdge> cycle;
|
deba@2440
|
341 |
int node_num = countNodes(graph);
|
deba@2440
|
342 |
|
deba@2440
|
343 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
344 |
int cycle_num = 0;
|
deba@2440
|
345 |
#endif
|
deba@2440
|
346 |
int length_bound = STARTING_LIMIT;
|
deba@2440
|
347 |
bool optimal = false;
|
deba@2440
|
348 |
while (!optimal) {
|
deba@2440
|
349 |
BellmanFord<ResGraph, ResCostMap> bf(res_graph, res_cost);
|
deba@2440
|
350 |
bf.predMap(pred);
|
deba@2440
|
351 |
bf.init(0);
|
deba@2440
|
352 |
int iter_num = 0;
|
deba@2440
|
353 |
bool cycle_found = false;
|
deba@2440
|
354 |
while (!cycle_found) {
|
deba@2457
|
355 |
#ifdef _NO_BACK_STEP_
|
deba@2457
|
356 |
int curr_iter_num = length_bound <= node_num ?
|
deba@2457
|
357 |
length_bound - iter_num : node_num - iter_num;
|
deba@2457
|
358 |
#else
|
deba@2440
|
359 |
int curr_iter_num = iter_num + length_bound <= node_num ?
|
deba@2440
|
360 |
length_bound : node_num - iter_num;
|
deba@2457
|
361 |
#endif
|
deba@2440
|
362 |
iter_num += curr_iter_num;
|
deba@2440
|
363 |
int real_iter_num = curr_iter_num;
|
deba@2440
|
364 |
for (int i = 0; i < curr_iter_num; ++i) {
|
deba@2440
|
365 |
if (bf.processNextWeakRound()) {
|
deba@2440
|
366 |
real_iter_num = i;
|
deba@2440
|
367 |
break;
|
deba@2440
|
368 |
}
|
deba@2440
|
369 |
}
|
deba@2440
|
370 |
if (real_iter_num < curr_iter_num) {
|
deba@2440
|
371 |
optimal = true;
|
deba@2440
|
372 |
break;
|
deba@2440
|
373 |
} else {
|
deba@2440
|
374 |
// Searching for node disjoint negative cycles
|
deba@2440
|
375 |
for (ResNodeIt n(res_graph); n != INVALID; ++n)
|
deba@2440
|
376 |
visited[n] = 0;
|
deba@2440
|
377 |
int id = 0;
|
deba@2440
|
378 |
for (ResNodeIt n(res_graph); n != INVALID; ++n) {
|
deba@2440
|
379 |
if (visited[n] > 0) continue;
|
deba@2440
|
380 |
visited[n] = ++id;
|
deba@2440
|
381 |
ResNode u = pred[n] == INVALID ?
|
deba@2440
|
382 |
INVALID : res_graph.source(pred[n]);
|
deba@2440
|
383 |
while (u != INVALID && visited[u] == 0) {
|
deba@2440
|
384 |
visited[u] = id;
|
deba@2440
|
385 |
u = pred[u] == INVALID ?
|
deba@2440
|
386 |
INVALID : res_graph.source(pred[u]);
|
deba@2440
|
387 |
}
|
deba@2440
|
388 |
if (u != INVALID && visited[u] == id) {
|
deba@2440
|
389 |
// Finding the negative cycle
|
deba@2440
|
390 |
cycle_found = true;
|
deba@2440
|
391 |
cycle.clear();
|
deba@2440
|
392 |
ResEdge e = pred[u];
|
deba@2440
|
393 |
cycle.push_back(e);
|
deba@2440
|
394 |
Capacity d = res_graph.rescap(e);
|
deba@2440
|
395 |
while (res_graph.source(e) != u) {
|
deba@2440
|
396 |
cycle.push_back(e = pred[res_graph.source(e)]);
|
deba@2440
|
397 |
if (res_graph.rescap(e) < d)
|
deba@2440
|
398 |
d = res_graph.rescap(e);
|
deba@2440
|
399 |
}
|
deba@2440
|
400 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
401 |
++cycle_num;
|
deba@2440
|
402 |
#endif
|
deba@2440
|
403 |
// Augmenting along the cycle
|
deba@2440
|
404 |
for (int i = 0; i < cycle.size(); ++i)
|
deba@2440
|
405 |
res_graph.augment(cycle[i], d);
|
deba@2440
|
406 |
#ifdef _ONLY_ONE_CYCLE_
|
deba@2440
|
407 |
break;
|
deba@2440
|
408 |
#endif
|
deba@2440
|
409 |
}
|
deba@2440
|
410 |
}
|
deba@2440
|
411 |
}
|
deba@2440
|
412 |
|
deba@2440
|
413 |
if (!cycle_found)
|
deba@2440
|
414 |
length_bound = length_bound * ALPHA_MUL / ALPHA_DIV;
|
deba@2440
|
415 |
}
|
deba@2440
|
416 |
}
|
deba@2440
|
417 |
|
deba@2440
|
418 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
419 |
std::cout << "Limited cycle-canceling algorithm finished. "
|
deba@2440
|
420 |
<< "Found " << cycle_num << " negative cycles."
|
deba@2440
|
421 |
<< std::endl;
|
deba@2440
|
422 |
#endif
|
deba@2440
|
423 |
|
deba@2440
|
424 |
// Handling nonzero lower bounds
|
deba@2440
|
425 |
if (lower) {
|
deba@2440
|
426 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
427 |
flow[e] += (*lower)[e];
|
deba@2440
|
428 |
}
|
deba@2440
|
429 |
return true;
|
deba@2440
|
430 |
}
|
deba@2440
|
431 |
#endif
|
deba@2440
|
432 |
|
deba@2440
|
433 |
#ifdef MIN_MEAN_CYCLE_CANCELING
|
deba@2457
|
434 |
/// \brief Executes the minimum mean cycle-canceling algorithm
|
deba@2440
|
435 |
/// using \ref lemon::MinMeanCycle "MinMeanCycle" class.
|
deba@2440
|
436 |
bool start() {
|
deba@2440
|
437 |
typedef Path<ResGraph> ResPath;
|
deba@2440
|
438 |
MinMeanCycle<ResGraph, ResCostMap> mmc(res_graph, res_cost);
|
deba@2440
|
439 |
ResPath cycle;
|
deba@2440
|
440 |
|
deba@2440
|
441 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
442 |
int cycle_num = 0;
|
deba@2440
|
443 |
#endif
|
deba@2440
|
444 |
mmc.cyclePath(cycle).init();
|
deba@2440
|
445 |
if (mmc.findMinMean()) {
|
deba@2440
|
446 |
while (mmc.cycleLength() < 0) {
|
deba@2440
|
447 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
448 |
++iter;
|
deba@2440
|
449 |
#endif
|
deba@2440
|
450 |
// Finding the cycle
|
deba@2440
|
451 |
mmc.findCycle();
|
deba@2440
|
452 |
|
deba@2440
|
453 |
// Finding the largest flow amount that can be augmented
|
deba@2440
|
454 |
// along the cycle
|
deba@2440
|
455 |
Capacity delta = 0;
|
deba@2440
|
456 |
for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e) {
|
deba@2440
|
457 |
if (delta == 0 || res_graph.rescap(e) < delta)
|
deba@2440
|
458 |
delta = res_graph.rescap(e);
|
deba@2440
|
459 |
}
|
deba@2440
|
460 |
|
deba@2440
|
461 |
// Augmenting along the cycle
|
deba@2440
|
462 |
for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e)
|
deba@2440
|
463 |
res_graph.augment(e, delta);
|
deba@2440
|
464 |
|
deba@2440
|
465 |
// Finding the minimum cycle mean for the modified residual
|
deba@2440
|
466 |
// graph
|
deba@2440
|
467 |
mmc.reset();
|
deba@2440
|
468 |
if (!mmc.findMinMean()) break;
|
deba@2440
|
469 |
}
|
deba@2440
|
470 |
}
|
deba@2440
|
471 |
|
deba@2440
|
472 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
473 |
std::cout << "Minimum mean cycle-canceling algorithm finished. "
|
deba@2440
|
474 |
<< "Found " << cycle_num << " negative cycles."
|
deba@2440
|
475 |
<< std::endl;
|
deba@2440
|
476 |
#endif
|
deba@2440
|
477 |
|
deba@2440
|
478 |
// Handling nonzero lower bounds
|
deba@2440
|
479 |
if (lower) {
|
deba@2440
|
480 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
481 |
flow[e] += (*lower)[e];
|
deba@2440
|
482 |
}
|
deba@2440
|
483 |
return true;
|
deba@2440
|
484 |
}
|
deba@2440
|
485 |
#endif
|
deba@2440
|
486 |
|
deba@2440
|
487 |
}; //class CycleCanceling
|
deba@2440
|
488 |
|
deba@2440
|
489 |
///@}
|
deba@2440
|
490 |
|
deba@2440
|
491 |
} //namespace lemon
|
deba@2440
|
492 |
|
deba@2440
|
493 |
#endif //LEMON_CYCLE_CANCELING_H
|