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>
|
deba@2440
|
28 |
#include <lemon/circulation.h>
|
deba@2440
|
29 |
#include <lemon/graph_adaptor.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.
|
deba@2440
|
82 |
/// - Supply values should be 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 |
typedef typename MapBase<ResEdge, Cost>::Value Value;
|
deba@2440
|
135 |
typedef typename MapBase<ResEdge, Cost>::Key Key;
|
deba@2440
|
136 |
|
deba@2440
|
137 |
ResCostMap(const CostMap &_cost) : cost_map(_cost) {}
|
deba@2440
|
138 |
|
deba@2440
|
139 |
Value operator[](const Key &e) const {
|
deba@2440
|
140 |
return ResGraph::forward(e) ? cost_map[e] : -cost_map[e];
|
deba@2440
|
141 |
}
|
deba@2440
|
142 |
|
deba@2440
|
143 |
}; //class ResCostMap
|
deba@2440
|
144 |
|
deba@2440
|
145 |
protected:
|
deba@2440
|
146 |
|
deba@2440
|
147 |
/// \brief The directed graph the algorithm runs on.
|
deba@2440
|
148 |
const Graph &graph;
|
deba@2440
|
149 |
/// \brief The original lower bound map.
|
deba@2440
|
150 |
const LowerMap *lower;
|
deba@2440
|
151 |
/// \brief The modified capacity map.
|
deba@2440
|
152 |
CapacityRefMap capacity;
|
deba@2440
|
153 |
/// \brief The cost map.
|
deba@2440
|
154 |
const CostMap &cost;
|
deba@2440
|
155 |
/// \brief The modified supply map.
|
deba@2440
|
156 |
SupplyRefMap supply;
|
deba@2440
|
157 |
/// \brief The sum of supply values equals zero.
|
deba@2440
|
158 |
bool valid_supply;
|
deba@2440
|
159 |
|
deba@2440
|
160 |
/// \brief The current flow.
|
deba@2440
|
161 |
FlowMap flow;
|
deba@2440
|
162 |
/// \brief The residual graph.
|
deba@2440
|
163 |
ResGraph res_graph;
|
deba@2440
|
164 |
/// \brief The residual cost map.
|
deba@2440
|
165 |
ResCostMap res_cost;
|
deba@2440
|
166 |
|
deba@2440
|
167 |
public :
|
deba@2440
|
168 |
|
deba@2440
|
169 |
/// \brief General constructor of the class (with lower bounds).
|
deba@2440
|
170 |
///
|
deba@2440
|
171 |
/// General constructor of the class (with lower bounds).
|
deba@2440
|
172 |
///
|
deba@2440
|
173 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
174 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
175 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
176 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
177 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
178 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
179 |
const LowerMap &_lower,
|
deba@2440
|
180 |
const CapacityMap &_capacity,
|
deba@2440
|
181 |
const CostMap &_cost,
|
deba@2440
|
182 |
const SupplyMap &_supply ) :
|
deba@2440
|
183 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2457
|
184 |
supply(_graph), flow(_graph, 0),
|
deba@2440
|
185 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
186 |
{
|
deba@2440
|
187 |
// Removing nonzero lower bounds
|
deba@2440
|
188 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
189 |
Supply sum = 0;
|
deba@2440
|
190 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
191 |
Supply s = _supply[n];
|
deba@2440
|
192 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
193 |
s += _lower[e];
|
deba@2440
|
194 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
195 |
s -= _lower[e];
|
deba@2440
|
196 |
sum += (supply[n] = s);
|
deba@2440
|
197 |
}
|
deba@2440
|
198 |
valid_supply = sum == 0;
|
deba@2440
|
199 |
}
|
deba@2440
|
200 |
|
deba@2440
|
201 |
/// \brief General constructor of the class (without lower bounds).
|
deba@2440
|
202 |
///
|
deba@2440
|
203 |
/// General constructor of the class (without lower bounds).
|
deba@2440
|
204 |
///
|
deba@2440
|
205 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
206 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
207 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
208 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
209 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
210 |
const CapacityMap &_capacity,
|
deba@2440
|
211 |
const CostMap &_cost,
|
deba@2440
|
212 |
const SupplyMap &_supply ) :
|
deba@2440
|
213 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2457
|
214 |
supply(_supply), flow(_graph, 0),
|
deba@2440
|
215 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
216 |
{
|
deba@2440
|
217 |
// Checking the sum of supply values
|
deba@2440
|
218 |
Supply sum = 0;
|
deba@2440
|
219 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
220 |
valid_supply = sum == 0;
|
deba@2440
|
221 |
}
|
deba@2440
|
222 |
|
deba@2440
|
223 |
|
deba@2440
|
224 |
/// \brief Simple constructor of the class (with lower bounds).
|
deba@2440
|
225 |
///
|
deba@2440
|
226 |
/// Simple constructor of the class (with lower bounds).
|
deba@2440
|
227 |
///
|
deba@2440
|
228 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
229 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
230 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
231 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
232 |
/// \param _s The source node.
|
deba@2440
|
233 |
/// \param _t The target node.
|
deba@2440
|
234 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
235 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
236 |
/// \c _t).
|
deba@2440
|
237 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
238 |
const LowerMap &_lower,
|
deba@2440
|
239 |
const CapacityMap &_capacity,
|
deba@2440
|
240 |
const CostMap &_cost,
|
deba@2440
|
241 |
Node _s, Node _t,
|
deba@2440
|
242 |
Supply _flow_value ) :
|
deba@2440
|
243 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2457
|
244 |
supply(_graph), flow(_graph, 0),
|
deba@2440
|
245 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
246 |
{
|
deba@2440
|
247 |
// Removing nonzero lower bounds
|
deba@2440
|
248 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
249 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
250 |
Supply s = 0;
|
deba@2440
|
251 |
if (n == _s) s = _flow_value;
|
deba@2440
|
252 |
if (n == _t) s = -_flow_value;
|
deba@2440
|
253 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
254 |
s += _lower[e];
|
deba@2440
|
255 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
256 |
s -= _lower[e];
|
deba@2440
|
257 |
supply[n] = s;
|
deba@2440
|
258 |
}
|
deba@2440
|
259 |
valid_supply = true;
|
deba@2440
|
260 |
}
|
deba@2440
|
261 |
|
deba@2440
|
262 |
/// \brief Simple constructor of the class (without lower bounds).
|
deba@2440
|
263 |
///
|
deba@2440
|
264 |
/// Simple constructor of the class (without lower bounds).
|
deba@2440
|
265 |
///
|
deba@2440
|
266 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
267 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
268 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
269 |
/// \param _s The source node.
|
deba@2440
|
270 |
/// \param _t The target node.
|
deba@2440
|
271 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
272 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
273 |
/// \c _t).
|
deba@2440
|
274 |
CycleCanceling( const Graph &_graph,
|
deba@2440
|
275 |
const CapacityMap &_capacity,
|
deba@2440
|
276 |
const CostMap &_cost,
|
deba@2440
|
277 |
Node _s, Node _t,
|
deba@2440
|
278 |
Supply _flow_value ) :
|
deba@2440
|
279 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2457
|
280 |
supply(_graph, 0), flow(_graph, 0),
|
deba@2440
|
281 |
res_graph(_graph, capacity, flow), res_cost(cost)
|
deba@2440
|
282 |
{
|
deba@2440
|
283 |
supply[_s] = _flow_value;
|
deba@2440
|
284 |
supply[_t] = -_flow_value;
|
deba@2440
|
285 |
valid_supply = true;
|
deba@2440
|
286 |
}
|
deba@2440
|
287 |
|
deba@2440
|
288 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
289 |
///
|
deba@2440
|
290 |
/// Returns a const reference to the flow map.
|
deba@2440
|
291 |
///
|
deba@2440
|
292 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
293 |
const FlowMap& flowMap() const {
|
deba@2440
|
294 |
return flow;
|
deba@2440
|
295 |
}
|
deba@2440
|
296 |
|
deba@2440
|
297 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
298 |
///
|
deba@2440
|
299 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
300 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
301 |
///
|
deba@2440
|
302 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
303 |
Cost totalCost() const {
|
deba@2440
|
304 |
Cost c = 0;
|
deba@2440
|
305 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
306 |
c += flow[e] * cost[e];
|
deba@2440
|
307 |
return c;
|
deba@2440
|
308 |
}
|
deba@2440
|
309 |
|
deba@2440
|
310 |
/// \brief Runs the algorithm.
|
deba@2440
|
311 |
///
|
deba@2440
|
312 |
/// Runs the algorithm.
|
deba@2440
|
313 |
///
|
deba@2440
|
314 |
/// \return \c true if a feasible flow can be found.
|
deba@2440
|
315 |
bool run() {
|
deba@2440
|
316 |
return init() && start();
|
deba@2440
|
317 |
}
|
deba@2440
|
318 |
|
deba@2440
|
319 |
protected:
|
deba@2440
|
320 |
|
deba@2440
|
321 |
/// \brief Initializes the algorithm.
|
deba@2440
|
322 |
bool init() {
|
deba@2440
|
323 |
// Checking the sum of supply values
|
deba@2440
|
324 |
Supply sum = 0;
|
deba@2440
|
325 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
326 |
if (sum != 0) return false;
|
deba@2440
|
327 |
|
deba@2440
|
328 |
// Finding a feasible flow
|
deba@2440
|
329 |
Circulation< Graph, Capacity, FlowMap, ConstMap<Edge, Capacity>,
|
deba@2457
|
330 |
CapacityRefMap, SupplyMap >
|
deba@2440
|
331 |
circulation( graph, constMap<Edge>((Capacity)0),
|
deba@2457
|
332 |
capacity, supply, flow );
|
deba@2440
|
333 |
return circulation.run() == -1;
|
deba@2440
|
334 |
}
|
deba@2440
|
335 |
|
deba@2440
|
336 |
#ifdef LIMITED_CYCLE_CANCELING
|
deba@2457
|
337 |
/// \brief Executes a cycle-canceling algorithm using
|
deba@2440
|
338 |
/// \ref lemon::BellmanFord "Bellman-Ford" algorithm with limited
|
deba@2440
|
339 |
/// iteration count.
|
deba@2440
|
340 |
bool start() {
|
deba@2440
|
341 |
typename BellmanFord<ResGraph, ResCostMap>::PredMap pred(res_graph);
|
deba@2440
|
342 |
typename ResGraph::template NodeMap<int> visited(res_graph);
|
deba@2440
|
343 |
std::vector<ResEdge> cycle;
|
deba@2440
|
344 |
int node_num = countNodes(graph);
|
deba@2440
|
345 |
|
deba@2440
|
346 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
347 |
int cycle_num = 0;
|
deba@2440
|
348 |
#endif
|
deba@2440
|
349 |
int length_bound = STARTING_LIMIT;
|
deba@2440
|
350 |
bool optimal = false;
|
deba@2440
|
351 |
while (!optimal) {
|
deba@2440
|
352 |
BellmanFord<ResGraph, ResCostMap> bf(res_graph, res_cost);
|
deba@2440
|
353 |
bf.predMap(pred);
|
deba@2440
|
354 |
bf.init(0);
|
deba@2440
|
355 |
int iter_num = 0;
|
deba@2440
|
356 |
bool cycle_found = false;
|
deba@2440
|
357 |
while (!cycle_found) {
|
deba@2457
|
358 |
#ifdef _NO_BACK_STEP_
|
deba@2457
|
359 |
int curr_iter_num = length_bound <= node_num ?
|
deba@2457
|
360 |
length_bound - iter_num : node_num - iter_num;
|
deba@2457
|
361 |
#else
|
deba@2440
|
362 |
int curr_iter_num = iter_num + length_bound <= node_num ?
|
deba@2440
|
363 |
length_bound : node_num - iter_num;
|
deba@2457
|
364 |
#endif
|
deba@2440
|
365 |
iter_num += curr_iter_num;
|
deba@2440
|
366 |
int real_iter_num = curr_iter_num;
|
deba@2440
|
367 |
for (int i = 0; i < curr_iter_num; ++i) {
|
deba@2440
|
368 |
if (bf.processNextWeakRound()) {
|
deba@2440
|
369 |
real_iter_num = i;
|
deba@2440
|
370 |
break;
|
deba@2440
|
371 |
}
|
deba@2440
|
372 |
}
|
deba@2440
|
373 |
if (real_iter_num < curr_iter_num) {
|
deba@2440
|
374 |
optimal = true;
|
deba@2440
|
375 |
break;
|
deba@2440
|
376 |
} else {
|
deba@2440
|
377 |
// Searching for node disjoint negative cycles
|
deba@2440
|
378 |
for (ResNodeIt n(res_graph); n != INVALID; ++n)
|
deba@2440
|
379 |
visited[n] = 0;
|
deba@2440
|
380 |
int id = 0;
|
deba@2440
|
381 |
for (ResNodeIt n(res_graph); n != INVALID; ++n) {
|
deba@2440
|
382 |
if (visited[n] > 0) continue;
|
deba@2440
|
383 |
visited[n] = ++id;
|
deba@2440
|
384 |
ResNode u = pred[n] == INVALID ?
|
deba@2440
|
385 |
INVALID : res_graph.source(pred[n]);
|
deba@2440
|
386 |
while (u != INVALID && visited[u] == 0) {
|
deba@2440
|
387 |
visited[u] = id;
|
deba@2440
|
388 |
u = pred[u] == INVALID ?
|
deba@2440
|
389 |
INVALID : res_graph.source(pred[u]);
|
deba@2440
|
390 |
}
|
deba@2440
|
391 |
if (u != INVALID && visited[u] == id) {
|
deba@2440
|
392 |
// Finding the negative cycle
|
deba@2440
|
393 |
cycle_found = true;
|
deba@2440
|
394 |
cycle.clear();
|
deba@2440
|
395 |
ResEdge e = pred[u];
|
deba@2440
|
396 |
cycle.push_back(e);
|
deba@2440
|
397 |
Capacity d = res_graph.rescap(e);
|
deba@2440
|
398 |
while (res_graph.source(e) != u) {
|
deba@2440
|
399 |
cycle.push_back(e = pred[res_graph.source(e)]);
|
deba@2440
|
400 |
if (res_graph.rescap(e) < d)
|
deba@2440
|
401 |
d = res_graph.rescap(e);
|
deba@2440
|
402 |
}
|
deba@2440
|
403 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
404 |
++cycle_num;
|
deba@2440
|
405 |
#endif
|
deba@2440
|
406 |
// Augmenting along the cycle
|
deba@2440
|
407 |
for (int i = 0; i < cycle.size(); ++i)
|
deba@2440
|
408 |
res_graph.augment(cycle[i], d);
|
deba@2440
|
409 |
#ifdef _ONLY_ONE_CYCLE_
|
deba@2440
|
410 |
break;
|
deba@2440
|
411 |
#endif
|
deba@2440
|
412 |
}
|
deba@2440
|
413 |
}
|
deba@2440
|
414 |
}
|
deba@2440
|
415 |
|
deba@2440
|
416 |
if (!cycle_found)
|
deba@2440
|
417 |
length_bound = length_bound * ALPHA_MUL / ALPHA_DIV;
|
deba@2440
|
418 |
}
|
deba@2440
|
419 |
}
|
deba@2440
|
420 |
|
deba@2440
|
421 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
422 |
std::cout << "Limited cycle-canceling algorithm finished. "
|
deba@2440
|
423 |
<< "Found " << cycle_num << " negative cycles."
|
deba@2440
|
424 |
<< std::endl;
|
deba@2440
|
425 |
#endif
|
deba@2440
|
426 |
|
deba@2440
|
427 |
// Handling nonzero lower bounds
|
deba@2440
|
428 |
if (lower) {
|
deba@2440
|
429 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
430 |
flow[e] += (*lower)[e];
|
deba@2440
|
431 |
}
|
deba@2440
|
432 |
return true;
|
deba@2440
|
433 |
}
|
deba@2440
|
434 |
#endif
|
deba@2440
|
435 |
|
deba@2440
|
436 |
#ifdef MIN_MEAN_CYCLE_CANCELING
|
deba@2457
|
437 |
/// \brief Executes the minimum mean cycle-canceling algorithm
|
deba@2440
|
438 |
/// using \ref lemon::MinMeanCycle "MinMeanCycle" class.
|
deba@2440
|
439 |
bool start() {
|
deba@2440
|
440 |
typedef Path<ResGraph> ResPath;
|
deba@2440
|
441 |
MinMeanCycle<ResGraph, ResCostMap> mmc(res_graph, res_cost);
|
deba@2440
|
442 |
ResPath cycle;
|
deba@2440
|
443 |
|
deba@2440
|
444 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
445 |
int cycle_num = 0;
|
deba@2440
|
446 |
#endif
|
deba@2440
|
447 |
mmc.cyclePath(cycle).init();
|
deba@2440
|
448 |
if (mmc.findMinMean()) {
|
deba@2440
|
449 |
while (mmc.cycleLength() < 0) {
|
deba@2440
|
450 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
451 |
++iter;
|
deba@2440
|
452 |
#endif
|
deba@2440
|
453 |
// Finding the cycle
|
deba@2440
|
454 |
mmc.findCycle();
|
deba@2440
|
455 |
|
deba@2440
|
456 |
// Finding the largest flow amount that can be augmented
|
deba@2440
|
457 |
// along the cycle
|
deba@2440
|
458 |
Capacity delta = 0;
|
deba@2440
|
459 |
for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e) {
|
deba@2440
|
460 |
if (delta == 0 || res_graph.rescap(e) < delta)
|
deba@2440
|
461 |
delta = res_graph.rescap(e);
|
deba@2440
|
462 |
}
|
deba@2440
|
463 |
|
deba@2440
|
464 |
// Augmenting along the cycle
|
deba@2440
|
465 |
for (typename ResPath::EdgeIt e(cycle); e != INVALID; ++e)
|
deba@2440
|
466 |
res_graph.augment(e, delta);
|
deba@2440
|
467 |
|
deba@2440
|
468 |
// Finding the minimum cycle mean for the modified residual
|
deba@2440
|
469 |
// graph
|
deba@2440
|
470 |
mmc.reset();
|
deba@2440
|
471 |
if (!mmc.findMinMean()) break;
|
deba@2440
|
472 |
}
|
deba@2440
|
473 |
}
|
deba@2440
|
474 |
|
deba@2440
|
475 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
476 |
std::cout << "Minimum mean cycle-canceling algorithm finished. "
|
deba@2440
|
477 |
<< "Found " << cycle_num << " negative cycles."
|
deba@2440
|
478 |
<< std::endl;
|
deba@2440
|
479 |
#endif
|
deba@2440
|
480 |
|
deba@2440
|
481 |
// Handling nonzero lower bounds
|
deba@2440
|
482 |
if (lower) {
|
deba@2440
|
483 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
484 |
flow[e] += (*lower)[e];
|
deba@2440
|
485 |
}
|
deba@2440
|
486 |
return true;
|
deba@2440
|
487 |
}
|
deba@2440
|
488 |
#endif
|
deba@2440
|
489 |
|
deba@2440
|
490 |
}; //class CycleCanceling
|
deba@2440
|
491 |
|
deba@2440
|
492 |
///@}
|
deba@2440
|
493 |
|
deba@2440
|
494 |
} //namespace lemon
|
deba@2440
|
495 |
|
deba@2440
|
496 |
#endif //LEMON_CYCLE_CANCELING_H
|