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_NETWORK_SIMPLEX_H
|
deba@2440
|
20 |
#define LEMON_NETWORK_SIMPLEX_H
|
deba@2440
|
21 |
|
deba@2440
|
22 |
/// \ingroup min_cost_flow
|
deba@2440
|
23 |
///
|
deba@2440
|
24 |
/// \file
|
deba@2440
|
25 |
/// \brief The network simplex algorithm for finding a minimum cost
|
deba@2440
|
26 |
/// flow.
|
deba@2440
|
27 |
|
deba@2440
|
28 |
#include <limits>
|
deba@2440
|
29 |
#include <lemon/smart_graph.h>
|
deba@2440
|
30 |
#include <lemon/graph_utils.h>
|
deba@2440
|
31 |
|
deba@2440
|
32 |
/// \brief The pivot rule used in the algorithm.
|
deba@2440
|
33 |
//#define FIRST_ELIGIBLE_PIVOT
|
deba@2440
|
34 |
//#define BEST_ELIGIBLE_PIVOT
|
deba@2444
|
35 |
#define EDGE_BLOCK_PIVOT
|
deba@2440
|
36 |
//#define CANDIDATE_LIST_PIVOT
|
deba@2440
|
37 |
//#define SORTED_LIST_PIVOT
|
deba@2440
|
38 |
|
deba@2444
|
39 |
//#define _DEBUG_ITER_
|
deba@2444
|
40 |
|
deba@2444
|
41 |
|
deba@2440
|
42 |
/// \brief State constant for edges at their lower bounds.
|
deba@2440
|
43 |
#define LOWER 1
|
deba@2440
|
44 |
/// \brief State constant for edges in the spanning tree.
|
deba@2440
|
45 |
#define TREE 0
|
deba@2440
|
46 |
/// \brief State constant for edges at their upper bounds.
|
deba@2440
|
47 |
#define UPPER -1
|
deba@2440
|
48 |
|
deba@2440
|
49 |
#ifdef EDGE_BLOCK_PIVOT
|
deba@2440
|
50 |
/// \brief Number of blocks for the "Edge Block" pivot rule.
|
deba@2444
|
51 |
#define BLOCK_NUM 100
|
deba@2444
|
52 |
/// \brief Lower bound for the size of blocks.
|
deba@2444
|
53 |
#define MIN_BLOCK_SIZE 10
|
deba@2440
|
54 |
#endif
|
deba@2440
|
55 |
|
deba@2440
|
56 |
#ifdef CANDIDATE_LIST_PIVOT
|
deba@2440
|
57 |
#include <list>
|
deba@2440
|
58 |
/// \brief The maximum length of the edge list for the
|
deba@2440
|
59 |
/// "Candidate List" pivot rule.
|
deba@2444
|
60 |
#define LIST_LENGTH 100
|
deba@2440
|
61 |
/// \brief The maximum number of minor iterations between two major
|
deba@2440
|
62 |
/// itarations.
|
deba@2444
|
63 |
#define MINOR_LIMIT 10
|
deba@2440
|
64 |
#endif
|
deba@2440
|
65 |
|
deba@2440
|
66 |
#ifdef SORTED_LIST_PIVOT
|
deba@2440
|
67 |
#include <deque>
|
deba@2440
|
68 |
#include <algorithm>
|
deba@2440
|
69 |
/// \brief The maximum length of the edge list for the
|
deba@2440
|
70 |
/// "Sorted List" pivot rule.
|
deba@2444
|
71 |
#define LIST_LENGTH 500
|
deba@2444
|
72 |
#define LOWER_DIV 3
|
deba@2440
|
73 |
#endif
|
deba@2440
|
74 |
|
deba@2440
|
75 |
namespace lemon {
|
deba@2440
|
76 |
|
deba@2440
|
77 |
/// \addtogroup min_cost_flow
|
deba@2440
|
78 |
/// @{
|
deba@2440
|
79 |
|
deba@2440
|
80 |
/// \brief Implementation of the network simplex algorithm for
|
deba@2440
|
81 |
/// finding a minimum cost flow.
|
deba@2440
|
82 |
///
|
deba@2440
|
83 |
/// \ref lemon::NetworkSimplex "NetworkSimplex" implements the
|
deba@2440
|
84 |
/// network simplex algorithm for finding a minimum cost flow.
|
deba@2440
|
85 |
///
|
deba@2440
|
86 |
/// \param Graph The directed graph type the algorithm runs on.
|
deba@2440
|
87 |
/// \param LowerMap The type of the lower bound map.
|
deba@2440
|
88 |
/// \param CapacityMap The type of the capacity (upper bound) map.
|
deba@2440
|
89 |
/// \param CostMap The type of the cost (length) map.
|
deba@2440
|
90 |
/// \param SupplyMap The type of the supply map.
|
deba@2440
|
91 |
///
|
deba@2440
|
92 |
/// \warning
|
deba@2440
|
93 |
/// - Edge capacities and costs should be nonnegative integers.
|
deba@2440
|
94 |
/// However \c CostMap::Value should be signed type.
|
deba@2440
|
95 |
/// - Supply values should be integers.
|
deba@2440
|
96 |
/// - \c LowerMap::Value must be convertible to
|
deba@2440
|
97 |
/// \c CapacityMap::Value and \c CapacityMap::Value must be
|
deba@2440
|
98 |
/// convertible to \c SupplyMap::Value.
|
deba@2440
|
99 |
///
|
deba@2440
|
100 |
/// \author Peter Kovacs
|
deba@2440
|
101 |
|
deba@2440
|
102 |
template < typename Graph,
|
deba@2440
|
103 |
typename LowerMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
104 |
typename CapacityMap = LowerMap,
|
deba@2440
|
105 |
typename CostMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
106 |
typename SupplyMap = typename Graph::template NodeMap
|
deba@2440
|
107 |
<typename CapacityMap::Value> >
|
deba@2440
|
108 |
class NetworkSimplex
|
deba@2440
|
109 |
{
|
deba@2440
|
110 |
typedef typename LowerMap::Value Lower;
|
deba@2440
|
111 |
typedef typename CapacityMap::Value Capacity;
|
deba@2440
|
112 |
typedef typename CostMap::Value Cost;
|
deba@2440
|
113 |
typedef typename SupplyMap::Value Supply;
|
deba@2440
|
114 |
|
deba@2440
|
115 |
typedef SmartGraph SGraph;
|
deba@2440
|
116 |
typedef typename SGraph::Node Node;
|
deba@2440
|
117 |
typedef typename SGraph::NodeIt NodeIt;
|
deba@2440
|
118 |
typedef typename SGraph::Edge Edge;
|
deba@2440
|
119 |
typedef typename SGraph::EdgeIt EdgeIt;
|
deba@2440
|
120 |
typedef typename SGraph::InEdgeIt InEdgeIt;
|
deba@2440
|
121 |
typedef typename SGraph::OutEdgeIt OutEdgeIt;
|
deba@2440
|
122 |
|
deba@2440
|
123 |
typedef typename SGraph::template EdgeMap<Lower> SLowerMap;
|
deba@2440
|
124 |
typedef typename SGraph::template EdgeMap<Capacity> SCapacityMap;
|
deba@2440
|
125 |
typedef typename SGraph::template EdgeMap<Cost> SCostMap;
|
deba@2440
|
126 |
typedef typename SGraph::template NodeMap<Supply> SSupplyMap;
|
deba@2440
|
127 |
typedef typename SGraph::template NodeMap<Cost> SPotentialMap;
|
deba@2440
|
128 |
|
deba@2440
|
129 |
typedef typename SGraph::template NodeMap<int> IntNodeMap;
|
deba@2440
|
130 |
typedef typename SGraph::template NodeMap<bool> BoolNodeMap;
|
deba@2440
|
131 |
typedef typename SGraph::template NodeMap<Node> NodeNodeMap;
|
deba@2440
|
132 |
typedef typename SGraph::template NodeMap<Edge> EdgeNodeMap;
|
deba@2440
|
133 |
typedef typename SGraph::template EdgeMap<int> IntEdgeMap;
|
deba@2440
|
134 |
|
deba@2440
|
135 |
typedef typename Graph::template NodeMap<Node> NodeRefMap;
|
deba@2440
|
136 |
typedef typename Graph::template EdgeMap<Edge> EdgeRefMap;
|
deba@2440
|
137 |
|
deba@2440
|
138 |
public:
|
deba@2440
|
139 |
|
deba@2440
|
140 |
/// \brief The type of the flow map.
|
deba@2440
|
141 |
typedef typename Graph::template EdgeMap<Capacity> FlowMap;
|
deba@2440
|
142 |
/// \brief The type of the potential map.
|
deba@2440
|
143 |
typedef typename Graph::template NodeMap<Cost> PotentialMap;
|
deba@2440
|
144 |
|
deba@2440
|
145 |
protected:
|
deba@2440
|
146 |
|
deba@2440
|
147 |
/// \brief Map adaptor class for handling reduced edge costs.
|
deba@2440
|
148 |
class ReducedCostMap : public MapBase<Edge, Cost>
|
deba@2440
|
149 |
{
|
deba@2440
|
150 |
private:
|
deba@2440
|
151 |
|
deba@2440
|
152 |
const SGraph &gr;
|
deba@2440
|
153 |
const SCostMap &cost_map;
|
deba@2440
|
154 |
const SPotentialMap &pot_map;
|
deba@2440
|
155 |
|
deba@2440
|
156 |
public:
|
deba@2440
|
157 |
|
deba@2440
|
158 |
typedef typename MapBase<Edge, Cost>::Value Value;
|
deba@2440
|
159 |
typedef typename MapBase<Edge, Cost>::Key Key;
|
deba@2440
|
160 |
|
deba@2440
|
161 |
ReducedCostMap( const SGraph &_gr,
|
deba@2440
|
162 |
const SCostMap &_cm,
|
deba@2440
|
163 |
const SPotentialMap &_pm ) :
|
deba@2440
|
164 |
gr(_gr), cost_map(_cm), pot_map(_pm) {}
|
deba@2440
|
165 |
|
deba@2440
|
166 |
Value operator[](const Key &e) const {
|
deba@2440
|
167 |
return cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
|
deba@2440
|
168 |
}
|
deba@2440
|
169 |
|
deba@2440
|
170 |
}; //class ReducedCostMap
|
deba@2440
|
171 |
|
deba@2440
|
172 |
protected:
|
deba@2440
|
173 |
|
deba@2440
|
174 |
/// \brief The directed graph the algorithm runs on.
|
deba@2440
|
175 |
SGraph graph;
|
deba@2440
|
176 |
/// \brief The original graph.
|
deba@2440
|
177 |
const Graph &graph_ref;
|
deba@2440
|
178 |
/// \brief The original lower bound map.
|
deba@2440
|
179 |
const LowerMap *lower;
|
deba@2440
|
180 |
/// \brief The capacity map.
|
deba@2440
|
181 |
SCapacityMap capacity;
|
deba@2440
|
182 |
/// \brief The cost map.
|
deba@2440
|
183 |
SCostMap cost;
|
deba@2440
|
184 |
/// \brief The supply map.
|
deba@2440
|
185 |
SSupplyMap supply;
|
deba@2440
|
186 |
/// \brief The reduced cost map.
|
deba@2440
|
187 |
ReducedCostMap red_cost;
|
deba@2440
|
188 |
/// \brief The sum of supply values equals zero.
|
deba@2440
|
189 |
bool valid_supply;
|
deba@2440
|
190 |
|
deba@2440
|
191 |
/// \brief The edge map of the current flow.
|
deba@2440
|
192 |
SCapacityMap flow;
|
deba@2440
|
193 |
/// \brief The edge map of the found flow on the original graph.
|
deba@2440
|
194 |
FlowMap flow_result;
|
deba@2440
|
195 |
/// \brief The potential node map.
|
deba@2440
|
196 |
SPotentialMap potential;
|
deba@2440
|
197 |
/// \brief The potential node map on the original graph.
|
deba@2440
|
198 |
PotentialMap potential_result;
|
deba@2440
|
199 |
|
deba@2440
|
200 |
/// \brief Node reference for the original graph.
|
deba@2440
|
201 |
NodeRefMap node_ref;
|
deba@2440
|
202 |
/// \brief Edge reference for the original graph.
|
deba@2440
|
203 |
EdgeRefMap edge_ref;
|
deba@2440
|
204 |
|
deba@2440
|
205 |
/// \brief The depth node map of the spanning tree structure.
|
deba@2440
|
206 |
IntNodeMap depth;
|
deba@2440
|
207 |
/// \brief The parent node map of the spanning tree structure.
|
deba@2440
|
208 |
NodeNodeMap parent;
|
deba@2440
|
209 |
/// \brief The pred_edge node map of the spanning tree structure.
|
deba@2440
|
210 |
EdgeNodeMap pred_edge;
|
deba@2440
|
211 |
/// \brief The thread node map of the spanning tree structure.
|
deba@2440
|
212 |
NodeNodeMap thread;
|
deba@2440
|
213 |
/// \brief The forward node map of the spanning tree structure.
|
deba@2440
|
214 |
BoolNodeMap forward;
|
deba@2440
|
215 |
/// \brief The state edge map.
|
deba@2440
|
216 |
IntEdgeMap state;
|
deba@2440
|
217 |
|
deba@2440
|
218 |
|
deba@2440
|
219 |
#ifdef EDGE_BLOCK_PIVOT
|
deba@2440
|
220 |
/// \brief The size of blocks for the "Edge Block" pivot rule.
|
deba@2440
|
221 |
int block_size;
|
deba@2440
|
222 |
#endif
|
deba@2440
|
223 |
#ifdef CANDIDATE_LIST_PIVOT
|
deba@2440
|
224 |
/// \brief The list of candidate edges for the "Candidate List"
|
deba@2440
|
225 |
/// pivot rule.
|
deba@2440
|
226 |
std::list<Edge> candidates;
|
deba@2440
|
227 |
/// \brief The number of minor iterations.
|
deba@2440
|
228 |
int minor_count;
|
deba@2440
|
229 |
#endif
|
deba@2440
|
230 |
#ifdef SORTED_LIST_PIVOT
|
deba@2440
|
231 |
/// \brief The list of candidate edges for the "Sorted List"
|
deba@2440
|
232 |
/// pivot rule.
|
deba@2440
|
233 |
std::deque<Edge> candidates;
|
deba@2440
|
234 |
#endif
|
deba@2440
|
235 |
|
deba@2440
|
236 |
// Root node of the starting spanning tree.
|
deba@2440
|
237 |
Node root;
|
deba@2440
|
238 |
// The entering edge of the current pivot iteration.
|
deba@2440
|
239 |
Edge in_edge;
|
deba@2440
|
240 |
// Temporary nodes used in the current pivot iteration.
|
deba@2440
|
241 |
Node join, u_in, v_in, u_out, v_out;
|
deba@2440
|
242 |
Node right, first, second, last;
|
deba@2440
|
243 |
Node stem, par_stem, new_stem;
|
deba@2440
|
244 |
// The maximum augment amount along the cycle in the current pivot
|
deba@2440
|
245 |
// iteration.
|
deba@2440
|
246 |
Capacity delta;
|
deba@2440
|
247 |
|
deba@2440
|
248 |
public :
|
deba@2440
|
249 |
|
deba@2440
|
250 |
/// \brief General constructor of the class (with lower bounds).
|
deba@2440
|
251 |
///
|
deba@2440
|
252 |
/// General constructor of the class (with lower bounds).
|
deba@2440
|
253 |
///
|
deba@2440
|
254 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
255 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
256 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
257 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
258 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
259 |
NetworkSimplex( const Graph &_graph,
|
deba@2440
|
260 |
const LowerMap &_lower,
|
deba@2440
|
261 |
const CapacityMap &_capacity,
|
deba@2440
|
262 |
const CostMap &_cost,
|
deba@2440
|
263 |
const SupplyMap &_supply ) :
|
deba@2440
|
264 |
graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
|
deba@2440
|
265 |
supply(graph), flow(graph), flow_result(_graph), potential(graph),
|
deba@2440
|
266 |
potential_result(_graph), depth(graph), parent(graph),
|
deba@2440
|
267 |
pred_edge(graph), thread(graph), forward(graph), state(graph),
|
deba@2440
|
268 |
node_ref(graph_ref), edge_ref(graph_ref),
|
deba@2440
|
269 |
red_cost(graph, cost, potential)
|
deba@2440
|
270 |
{
|
deba@2440
|
271 |
// Checking the sum of supply values
|
deba@2440
|
272 |
Supply sum = 0;
|
deba@2440
|
273 |
for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
|
deba@2440
|
274 |
sum += _supply[n];
|
deba@2440
|
275 |
if (!(valid_supply = sum == 0)) return;
|
deba@2440
|
276 |
|
deba@2440
|
277 |
// Copying graph_ref to graph
|
deba@2457
|
278 |
graph.reserveNode(countNodes(graph_ref) + 1);
|
deba@2457
|
279 |
graph.reserveEdge(countEdges(graph_ref) + countNodes(graph_ref));
|
deba@2440
|
280 |
copyGraph(graph, graph_ref)
|
deba@2440
|
281 |
.edgeMap(cost, _cost)
|
deba@2440
|
282 |
.nodeRef(node_ref)
|
deba@2440
|
283 |
.edgeRef(edge_ref)
|
deba@2440
|
284 |
.run();
|
deba@2440
|
285 |
|
deba@2440
|
286 |
// Removing nonzero lower bounds
|
deba@2440
|
287 |
for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
|
deba@2440
|
288 |
capacity[edge_ref[e]] = _capacity[e] - _lower[e];
|
deba@2440
|
289 |
}
|
deba@2440
|
290 |
for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
|
deba@2440
|
291 |
Supply s = _supply[n];
|
deba@2440
|
292 |
for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
|
deba@2440
|
293 |
s += _lower[e];
|
deba@2440
|
294 |
for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
|
deba@2440
|
295 |
s -= _lower[e];
|
deba@2440
|
296 |
supply[node_ref[n]] = s;
|
deba@2440
|
297 |
}
|
deba@2440
|
298 |
}
|
deba@2440
|
299 |
|
deba@2440
|
300 |
/// \brief General constructor of the class (without lower bounds).
|
deba@2440
|
301 |
///
|
deba@2440
|
302 |
/// General constructor of the class (without lower bounds).
|
deba@2440
|
303 |
///
|
deba@2440
|
304 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
305 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
306 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
307 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
308 |
NetworkSimplex( const Graph &_graph,
|
deba@2440
|
309 |
const CapacityMap &_capacity,
|
deba@2440
|
310 |
const CostMap &_cost,
|
deba@2440
|
311 |
const SupplyMap &_supply ) :
|
deba@2440
|
312 |
graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
|
deba@2440
|
313 |
supply(graph), flow(graph), flow_result(_graph), potential(graph),
|
deba@2440
|
314 |
potential_result(_graph), depth(graph), parent(graph),
|
deba@2440
|
315 |
pred_edge(graph), thread(graph), forward(graph), state(graph),
|
deba@2440
|
316 |
node_ref(graph_ref), edge_ref(graph_ref),
|
deba@2440
|
317 |
red_cost(graph, cost, potential)
|
deba@2440
|
318 |
{
|
deba@2440
|
319 |
// Checking the sum of supply values
|
deba@2440
|
320 |
Supply sum = 0;
|
deba@2440
|
321 |
for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
|
deba@2440
|
322 |
sum += _supply[n];
|
deba@2440
|
323 |
if (!(valid_supply = sum == 0)) return;
|
deba@2440
|
324 |
|
deba@2440
|
325 |
// Copying graph_ref to graph
|
deba@2440
|
326 |
copyGraph(graph, graph_ref)
|
deba@2440
|
327 |
.edgeMap(capacity, _capacity)
|
deba@2440
|
328 |
.edgeMap(cost, _cost)
|
deba@2440
|
329 |
.nodeMap(supply, _supply)
|
deba@2440
|
330 |
.nodeRef(node_ref)
|
deba@2440
|
331 |
.edgeRef(edge_ref)
|
deba@2440
|
332 |
.run();
|
deba@2440
|
333 |
}
|
deba@2440
|
334 |
|
deba@2440
|
335 |
/// \brief Simple constructor of the class (with lower bounds).
|
deba@2440
|
336 |
///
|
deba@2440
|
337 |
/// Simple constructor of the class (with lower bounds).
|
deba@2440
|
338 |
///
|
deba@2440
|
339 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
340 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
341 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
342 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
343 |
/// \param _s The source node.
|
deba@2440
|
344 |
/// \param _t The target node.
|
deba@2440
|
345 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
346 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
347 |
/// \c _t).
|
deba@2440
|
348 |
NetworkSimplex( const Graph &_graph,
|
deba@2440
|
349 |
const LowerMap &_lower,
|
deba@2440
|
350 |
const CapacityMap &_capacity,
|
deba@2440
|
351 |
const CostMap &_cost,
|
deba@2440
|
352 |
typename Graph::Node _s,
|
deba@2440
|
353 |
typename Graph::Node _t,
|
deba@2440
|
354 |
typename SupplyMap::Value _flow_value ) :
|
deba@2440
|
355 |
graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
|
deba@2440
|
356 |
supply(graph), flow(graph), flow_result(_graph), potential(graph),
|
deba@2440
|
357 |
potential_result(_graph), depth(graph), parent(graph),
|
deba@2440
|
358 |
pred_edge(graph), thread(graph), forward(graph), state(graph),
|
deba@2440
|
359 |
node_ref(graph_ref), edge_ref(graph_ref),
|
deba@2440
|
360 |
red_cost(graph, cost, potential)
|
deba@2440
|
361 |
{
|
deba@2440
|
362 |
// Copying graph_ref to graph
|
deba@2440
|
363 |
copyGraph(graph, graph_ref)
|
deba@2440
|
364 |
.edgeMap(cost, _cost)
|
deba@2440
|
365 |
.nodeRef(node_ref)
|
deba@2440
|
366 |
.edgeRef(edge_ref)
|
deba@2440
|
367 |
.run();
|
deba@2440
|
368 |
|
deba@2440
|
369 |
// Removing nonzero lower bounds
|
deba@2440
|
370 |
for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
|
deba@2440
|
371 |
capacity[edge_ref[e]] = _capacity[e] - _lower[e];
|
deba@2440
|
372 |
}
|
deba@2440
|
373 |
for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
|
deba@2440
|
374 |
Supply s = 0;
|
deba@2440
|
375 |
if (n == _s) s = _flow_value;
|
deba@2440
|
376 |
if (n == _t) s = -_flow_value;
|
deba@2440
|
377 |
for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
|
deba@2440
|
378 |
s += _lower[e];
|
deba@2440
|
379 |
for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
|
deba@2440
|
380 |
s -= _lower[e];
|
deba@2440
|
381 |
supply[node_ref[n]] = s;
|
deba@2440
|
382 |
}
|
deba@2440
|
383 |
valid_supply = true;
|
deba@2440
|
384 |
}
|
deba@2440
|
385 |
|
deba@2440
|
386 |
/// \brief Simple constructor of the class (without lower bounds).
|
deba@2440
|
387 |
///
|
deba@2440
|
388 |
/// Simple constructor of the class (without lower bounds).
|
deba@2440
|
389 |
///
|
deba@2440
|
390 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
391 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
392 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
393 |
/// \param _s The source node.
|
deba@2440
|
394 |
/// \param _t The target node.
|
deba@2440
|
395 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
396 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
397 |
/// \c _t).
|
deba@2440
|
398 |
NetworkSimplex( const Graph &_graph,
|
deba@2440
|
399 |
const CapacityMap &_capacity,
|
deba@2440
|
400 |
const CostMap &_cost,
|
deba@2440
|
401 |
typename Graph::Node _s,
|
deba@2440
|
402 |
typename Graph::Node _t,
|
deba@2440
|
403 |
typename SupplyMap::Value _flow_value ) :
|
deba@2440
|
404 |
graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
|
deba@2440
|
405 |
supply(graph, 0), flow(graph), flow_result(_graph), potential(graph),
|
deba@2440
|
406 |
potential_result(_graph), depth(graph), parent(graph),
|
deba@2440
|
407 |
pred_edge(graph), thread(graph), forward(graph), state(graph),
|
deba@2440
|
408 |
node_ref(graph_ref), edge_ref(graph_ref),
|
deba@2440
|
409 |
red_cost(graph, cost, potential)
|
deba@2440
|
410 |
{
|
deba@2440
|
411 |
// Copying graph_ref to graph
|
deba@2440
|
412 |
copyGraph(graph, graph_ref)
|
deba@2440
|
413 |
.edgeMap(capacity, _capacity)
|
deba@2440
|
414 |
.edgeMap(cost, _cost)
|
deba@2440
|
415 |
.nodeRef(node_ref)
|
deba@2440
|
416 |
.edgeRef(edge_ref)
|
deba@2440
|
417 |
.run();
|
deba@2440
|
418 |
supply[node_ref[_s]] = _flow_value;
|
deba@2440
|
419 |
supply[node_ref[_t]] = -_flow_value;
|
deba@2440
|
420 |
valid_supply = true;
|
deba@2440
|
421 |
}
|
deba@2440
|
422 |
|
deba@2440
|
423 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
424 |
///
|
deba@2440
|
425 |
/// Returns a const reference to the flow map.
|
deba@2440
|
426 |
///
|
deba@2440
|
427 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
428 |
const FlowMap& flowMap() const {
|
deba@2440
|
429 |
return flow_result;
|
deba@2440
|
430 |
}
|
deba@2440
|
431 |
|
deba@2440
|
432 |
/// \brief Returns a const reference to the potential map (the dual
|
deba@2440
|
433 |
/// solution).
|
deba@2440
|
434 |
///
|
deba@2440
|
435 |
/// Returns a const reference to the potential map (the dual
|
deba@2440
|
436 |
/// solution).
|
deba@2440
|
437 |
///
|
deba@2440
|
438 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
439 |
const PotentialMap& potentialMap() const {
|
deba@2440
|
440 |
return potential_result;
|
deba@2440
|
441 |
}
|
deba@2440
|
442 |
|
deba@2440
|
443 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
444 |
///
|
deba@2440
|
445 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
446 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
447 |
///
|
deba@2440
|
448 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
449 |
Cost totalCost() const {
|
deba@2440
|
450 |
Cost c = 0;
|
deba@2440
|
451 |
for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
|
deba@2440
|
452 |
c += flow_result[e] * cost[edge_ref[e]];
|
deba@2440
|
453 |
return c;
|
deba@2440
|
454 |
}
|
deba@2440
|
455 |
|
deba@2440
|
456 |
/// \brief Runs the algorithm.
|
deba@2440
|
457 |
///
|
deba@2440
|
458 |
/// Runs the algorithm.
|
deba@2440
|
459 |
///
|
deba@2440
|
460 |
/// \return \c true if a feasible flow can be found.
|
deba@2440
|
461 |
bool run() {
|
deba@2440
|
462 |
return init() && start();
|
deba@2440
|
463 |
}
|
deba@2440
|
464 |
|
deba@2440
|
465 |
protected:
|
deba@2440
|
466 |
|
deba@2440
|
467 |
/// \brief Extends the underlaying graph and initializes all the
|
deba@2440
|
468 |
/// node and edge maps.
|
deba@2440
|
469 |
bool init() {
|
deba@2440
|
470 |
if (!valid_supply) return false;
|
deba@2440
|
471 |
|
deba@2440
|
472 |
// Initializing state and flow maps
|
deba@2440
|
473 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
deba@2440
|
474 |
flow[e] = 0;
|
deba@2440
|
475 |
state[e] = LOWER;
|
deba@2440
|
476 |
}
|
deba@2440
|
477 |
|
deba@2440
|
478 |
// Adding an artificial root node to the graph
|
deba@2440
|
479 |
root = graph.addNode();
|
deba@2440
|
480 |
parent[root] = INVALID;
|
deba@2440
|
481 |
pred_edge[root] = INVALID;
|
deba@2457
|
482 |
depth[root] = 0;
|
deba@2457
|
483 |
supply[root] = 0;
|
deba@2457
|
484 |
potential[root] = 0;
|
deba@2440
|
485 |
|
deba@2440
|
486 |
// Adding artificial edges to the graph and initializing the node
|
deba@2440
|
487 |
// maps of the spanning tree data structure
|
deba@2440
|
488 |
Supply sum = 0;
|
deba@2440
|
489 |
Node last = root;
|
deba@2440
|
490 |
Edge e;
|
deba@2440
|
491 |
Cost max_cost = std::numeric_limits<Cost>::max() / 4;
|
deba@2440
|
492 |
for (NodeIt u(graph); u != INVALID; ++u) {
|
deba@2440
|
493 |
if (u == root) continue;
|
deba@2440
|
494 |
thread[last] = u;
|
deba@2440
|
495 |
last = u;
|
deba@2440
|
496 |
parent[u] = root;
|
deba@2440
|
497 |
depth[u] = 1;
|
deba@2440
|
498 |
sum += supply[u];
|
deba@2440
|
499 |
if (supply[u] >= 0) {
|
deba@2440
|
500 |
e = graph.addEdge(u, root);
|
deba@2440
|
501 |
flow[e] = supply[u];
|
deba@2440
|
502 |
forward[u] = true;
|
deba@2440
|
503 |
potential[u] = max_cost;
|
deba@2440
|
504 |
} else {
|
deba@2440
|
505 |
e = graph.addEdge(root, u);
|
deba@2440
|
506 |
flow[e] = -supply[u];
|
deba@2440
|
507 |
forward[u] = false;
|
deba@2440
|
508 |
potential[u] = -max_cost;
|
deba@2440
|
509 |
}
|
deba@2440
|
510 |
cost[e] = max_cost;
|
deba@2440
|
511 |
capacity[e] = std::numeric_limits<Capacity>::max();
|
deba@2440
|
512 |
state[e] = TREE;
|
deba@2440
|
513 |
pred_edge[u] = e;
|
deba@2440
|
514 |
}
|
deba@2440
|
515 |
thread[last] = root;
|
deba@2440
|
516 |
|
deba@2440
|
517 |
#ifdef EDGE_BLOCK_PIVOT
|
deba@2440
|
518 |
// Initializing block_size for the edge block pivot rule
|
deba@2440
|
519 |
int edge_num = countEdges(graph);
|
deba@2457
|
520 |
block_size = edge_num >= BLOCK_NUM * MIN_BLOCK_SIZE ?
|
deba@2444
|
521 |
edge_num / BLOCK_NUM : MIN_BLOCK_SIZE;
|
deba@2440
|
522 |
#endif
|
deba@2440
|
523 |
#ifdef CANDIDATE_LIST_PIVOT
|
deba@2440
|
524 |
minor_count = 0;
|
deba@2440
|
525 |
#endif
|
deba@2440
|
526 |
|
deba@2440
|
527 |
return sum == 0;
|
deba@2440
|
528 |
}
|
deba@2440
|
529 |
|
deba@2440
|
530 |
#ifdef FIRST_ELIGIBLE_PIVOT
|
deba@2440
|
531 |
/// \brief Finds entering edge according to the "First Eligible"
|
deba@2440
|
532 |
/// pivot rule.
|
deba@2440
|
533 |
bool findEnteringEdge(EdgeIt &next_edge) {
|
deba@2440
|
534 |
for (EdgeIt e = next_edge; e != INVALID; ++e) {
|
deba@2440
|
535 |
if (state[e] * red_cost[e] < 0) {
|
deba@2440
|
536 |
in_edge = e;
|
deba@2440
|
537 |
next_edge = ++e;
|
deba@2440
|
538 |
return true;
|
deba@2440
|
539 |
}
|
deba@2440
|
540 |
}
|
deba@2440
|
541 |
for (EdgeIt e(graph); e != next_edge; ++e) {
|
deba@2440
|
542 |
if (state[e] * red_cost[e] < 0) {
|
deba@2440
|
543 |
in_edge = e;
|
deba@2440
|
544 |
next_edge = ++e;
|
deba@2440
|
545 |
return true;
|
deba@2440
|
546 |
}
|
deba@2440
|
547 |
}
|
deba@2440
|
548 |
return false;
|
deba@2440
|
549 |
}
|
deba@2440
|
550 |
#endif
|
deba@2440
|
551 |
|
deba@2440
|
552 |
#ifdef BEST_ELIGIBLE_PIVOT
|
deba@2440
|
553 |
/// \brief Finds entering edge according to the "Best Eligible"
|
deba@2440
|
554 |
/// pivot rule.
|
deba@2440
|
555 |
bool findEnteringEdge() {
|
deba@2440
|
556 |
Cost min = 0;
|
deba@2440
|
557 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
deba@2440
|
558 |
if (state[e] * red_cost[e] < min) {
|
deba@2440
|
559 |
min = state[e] * red_cost[e];
|
deba@2440
|
560 |
in_edge = e;
|
deba@2440
|
561 |
}
|
deba@2440
|
562 |
}
|
deba@2440
|
563 |
return min < 0;
|
deba@2440
|
564 |
}
|
deba@2440
|
565 |
#endif
|
deba@2440
|
566 |
|
deba@2440
|
567 |
#ifdef EDGE_BLOCK_PIVOT
|
deba@2440
|
568 |
/// \brief Finds entering edge according to the "Edge Block"
|
deba@2440
|
569 |
/// pivot rule.
|
deba@2440
|
570 |
bool findEnteringEdge(EdgeIt &next_edge) {
|
deba@2444
|
571 |
// Performing edge block selection
|
deba@2444
|
572 |
Cost curr, min = 0;
|
deba@2444
|
573 |
EdgeIt min_edge(graph);
|
deba@2444
|
574 |
int cnt = 0;
|
deba@2444
|
575 |
for (EdgeIt e = next_edge; e != INVALID; ++e) {
|
deba@2444
|
576 |
if ((curr = state[e] * red_cost[e]) < min) {
|
deba@2444
|
577 |
min = curr;
|
deba@2444
|
578 |
min_edge = e;
|
deba@2440
|
579 |
}
|
deba@2444
|
580 |
if (++cnt == block_size) {
|
deba@2444
|
581 |
if (min < 0) break;
|
deba@2444
|
582 |
cnt = 0;
|
deba@2444
|
583 |
}
|
deba@2444
|
584 |
}
|
deba@2444
|
585 |
if (!(min < 0)) {
|
deba@2440
|
586 |
for (EdgeIt e(graph); e != next_edge; ++e) {
|
deba@2440
|
587 |
if ((curr = state[e] * red_cost[e]) < min) {
|
deba@2440
|
588 |
min = curr;
|
deba@2440
|
589 |
min_edge = e;
|
deba@2440
|
590 |
}
|
deba@2440
|
591 |
if (++cnt == block_size) {
|
deba@2440
|
592 |
if (min < 0) break;
|
deba@2440
|
593 |
cnt = 0;
|
deba@2440
|
594 |
}
|
deba@2440
|
595 |
}
|
deba@2440
|
596 |
}
|
deba@2444
|
597 |
in_edge = min_edge;
|
deba@2444
|
598 |
if ((next_edge = ++min_edge) == INVALID)
|
deba@2444
|
599 |
next_edge = EdgeIt(graph);
|
deba@2444
|
600 |
return min < 0;
|
deba@2440
|
601 |
}
|
deba@2440
|
602 |
#endif
|
deba@2440
|
603 |
|
deba@2440
|
604 |
#ifdef CANDIDATE_LIST_PIVOT
|
deba@2440
|
605 |
/// \brief Functor class for removing non-eligible edges from the
|
deba@2440
|
606 |
/// candidate list.
|
deba@2440
|
607 |
class RemoveFunc
|
deba@2440
|
608 |
{
|
deba@2440
|
609 |
private:
|
deba@2440
|
610 |
const IntEdgeMap &st;
|
deba@2440
|
611 |
const ReducedCostMap &rc;
|
deba@2440
|
612 |
public:
|
deba@2440
|
613 |
RemoveFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
|
deba@2440
|
614 |
st(_st), rc(_rc) {}
|
deba@2440
|
615 |
bool operator()(const Edge &e) {
|
deba@2440
|
616 |
return st[e] * rc[e] >= 0;
|
deba@2440
|
617 |
}
|
deba@2440
|
618 |
};
|
deba@2440
|
619 |
|
deba@2440
|
620 |
/// \brief Finds entering edge according to the "Candidate List"
|
deba@2440
|
621 |
/// pivot rule.
|
deba@2440
|
622 |
bool findEnteringEdge() {
|
deba@2440
|
623 |
static RemoveFunc remove_func(state, red_cost);
|
deba@2440
|
624 |
typedef typename std::list<Edge>::iterator ListIt;
|
deba@2440
|
625 |
|
deba@2440
|
626 |
candidates.remove_if(remove_func);
|
deba@2440
|
627 |
if (minor_count >= MINOR_LIMIT || candidates.size() == 0) {
|
deba@2440
|
628 |
// Major iteration
|
deba@2440
|
629 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
deba@2440
|
630 |
if (state[e] * red_cost[e] < 0) {
|
deba@2440
|
631 |
candidates.push_back(e);
|
deba@2440
|
632 |
if (candidates.size() == LIST_LENGTH) break;
|
deba@2440
|
633 |
}
|
deba@2440
|
634 |
}
|
deba@2440
|
635 |
if (candidates.size() == 0) return false;
|
deba@2440
|
636 |
}
|
deba@2440
|
637 |
|
deba@2440
|
638 |
// Minor iteration
|
deba@2440
|
639 |
++minor_count;
|
deba@2440
|
640 |
Cost min = 0;
|
deba@2440
|
641 |
for (ListIt it = candidates.begin(); it != candidates.end(); ++it) {
|
deba@2440
|
642 |
if (state[*it] * red_cost[*it] < min) {
|
deba@2440
|
643 |
min = state[*it] * red_cost[*it];
|
deba@2440
|
644 |
in_edge = *it;
|
deba@2440
|
645 |
}
|
deba@2440
|
646 |
}
|
deba@2440
|
647 |
return true;
|
deba@2440
|
648 |
}
|
deba@2440
|
649 |
#endif
|
deba@2440
|
650 |
|
deba@2440
|
651 |
#ifdef SORTED_LIST_PIVOT
|
deba@2440
|
652 |
/// \brief Functor class to compare edges during sort of the
|
deba@2440
|
653 |
/// candidate list.
|
deba@2440
|
654 |
class SortFunc
|
deba@2440
|
655 |
{
|
deba@2440
|
656 |
private:
|
deba@2440
|
657 |
const IntEdgeMap &st;
|
deba@2440
|
658 |
const ReducedCostMap &rc;
|
deba@2440
|
659 |
public:
|
deba@2440
|
660 |
SortFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
|
deba@2440
|
661 |
st(_st), rc(_rc) {}
|
deba@2440
|
662 |
bool operator()(const Edge &e1, const Edge &e2) {
|
deba@2440
|
663 |
return st[e1] * rc[e1] < st[e2] * rc[e2];
|
deba@2440
|
664 |
}
|
deba@2440
|
665 |
};
|
deba@2440
|
666 |
|
deba@2440
|
667 |
/// \brief Finds entering edge according to the "Sorted List"
|
deba@2440
|
668 |
/// pivot rule.
|
deba@2440
|
669 |
bool findEnteringEdge() {
|
deba@2440
|
670 |
static SortFunc sort_func(state, red_cost);
|
deba@2440
|
671 |
|
deba@2440
|
672 |
// Minor iteration
|
deba@2440
|
673 |
while (candidates.size() > 0) {
|
deba@2440
|
674 |
in_edge = candidates.front();
|
deba@2440
|
675 |
candidates.pop_front();
|
deba@2440
|
676 |
if (state[in_edge] * red_cost[in_edge] < 0) return true;
|
deba@2440
|
677 |
}
|
deba@2440
|
678 |
|
deba@2440
|
679 |
// Major iteration
|
deba@2440
|
680 |
Cost curr, min = 0;
|
deba@2440
|
681 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
deba@2440
|
682 |
if ((curr = state[e] * red_cost[e]) < min / LOWER_DIV) {
|
deba@2440
|
683 |
candidates.push_back(e);
|
deba@2440
|
684 |
if (curr < min) min = curr;
|
deba@2444
|
685 |
if (candidates.size() == LIST_LENGTH) break;
|
deba@2440
|
686 |
}
|
deba@2440
|
687 |
}
|
deba@2440
|
688 |
if (candidates.size() == 0) return false;
|
deba@2440
|
689 |
sort(candidates.begin(), candidates.end(), sort_func);
|
deba@2444
|
690 |
in_edge = candidates.front();
|
deba@2444
|
691 |
candidates.pop_front();
|
deba@2440
|
692 |
return true;
|
deba@2440
|
693 |
}
|
deba@2440
|
694 |
#endif
|
deba@2440
|
695 |
|
deba@2440
|
696 |
/// \brief Finds the join node.
|
deba@2440
|
697 |
Node findJoinNode() {
|
deba@2440
|
698 |
// Finding the join node
|
deba@2440
|
699 |
Node u = graph.source(in_edge);
|
deba@2440
|
700 |
Node v = graph.target(in_edge);
|
deba@2440
|
701 |
while (u != v) {
|
deba@2440
|
702 |
if (depth[u] == depth[v]) {
|
deba@2440
|
703 |
u = parent[u];
|
deba@2440
|
704 |
v = parent[v];
|
deba@2440
|
705 |
}
|
deba@2440
|
706 |
else if (depth[u] > depth[v]) u = parent[u];
|
deba@2440
|
707 |
else v = parent[v];
|
deba@2440
|
708 |
}
|
deba@2440
|
709 |
return u;
|
deba@2440
|
710 |
}
|
deba@2440
|
711 |
|
deba@2440
|
712 |
/// \brief Finds the leaving edge of the cycle. Returns \c true if
|
deba@2440
|
713 |
/// the leaving edge is not the same as the entering edge.
|
deba@2440
|
714 |
bool findLeavingEdge() {
|
deba@2440
|
715 |
// Initializing first and second nodes according to the direction
|
deba@2440
|
716 |
// of the cycle
|
deba@2440
|
717 |
if (state[in_edge] == LOWER) {
|
deba@2440
|
718 |
first = graph.source(in_edge);
|
deba@2440
|
719 |
second = graph.target(in_edge);
|
deba@2440
|
720 |
} else {
|
deba@2440
|
721 |
first = graph.target(in_edge);
|
deba@2440
|
722 |
second = graph.source(in_edge);
|
deba@2440
|
723 |
}
|
deba@2440
|
724 |
delta = capacity[in_edge];
|
deba@2440
|
725 |
bool result = false;
|
deba@2440
|
726 |
Capacity d;
|
deba@2440
|
727 |
Edge e;
|
deba@2440
|
728 |
|
deba@2440
|
729 |
// Searching the cycle along the path form the first node to the
|
deba@2440
|
730 |
// root node
|
deba@2440
|
731 |
for (Node u = first; u != join; u = parent[u]) {
|
deba@2440
|
732 |
e = pred_edge[u];
|
deba@2440
|
733 |
d = forward[u] ? flow[e] : capacity[e] - flow[e];
|
deba@2440
|
734 |
if (d < delta) {
|
deba@2440
|
735 |
delta = d;
|
deba@2440
|
736 |
u_out = u;
|
deba@2440
|
737 |
u_in = first;
|
deba@2440
|
738 |
v_in = second;
|
deba@2440
|
739 |
result = true;
|
deba@2440
|
740 |
}
|
deba@2440
|
741 |
}
|
deba@2440
|
742 |
// Searching the cycle along the path form the second node to the
|
deba@2440
|
743 |
// root node
|
deba@2440
|
744 |
for (Node u = second; u != join; u = parent[u]) {
|
deba@2440
|
745 |
e = pred_edge[u];
|
deba@2440
|
746 |
d = forward[u] ? capacity[e] - flow[e] : flow[e];
|
deba@2440
|
747 |
if (d <= delta) {
|
deba@2440
|
748 |
delta = d;
|
deba@2440
|
749 |
u_out = u;
|
deba@2440
|
750 |
u_in = second;
|
deba@2440
|
751 |
v_in = first;
|
deba@2440
|
752 |
result = true;
|
deba@2440
|
753 |
}
|
deba@2440
|
754 |
}
|
deba@2440
|
755 |
return result;
|
deba@2440
|
756 |
}
|
deba@2440
|
757 |
|
deba@2440
|
758 |
/// \brief Changes flow and state edge maps.
|
deba@2440
|
759 |
void changeFlows(bool change) {
|
deba@2440
|
760 |
// Augmenting along the cycle
|
deba@2440
|
761 |
if (delta > 0) {
|
deba@2440
|
762 |
Capacity val = state[in_edge] * delta;
|
deba@2440
|
763 |
flow[in_edge] += val;
|
deba@2440
|
764 |
for (Node u = graph.source(in_edge); u != join; u = parent[u]) {
|
deba@2440
|
765 |
flow[pred_edge[u]] += forward[u] ? -val : val;
|
deba@2440
|
766 |
}
|
deba@2440
|
767 |
for (Node u = graph.target(in_edge); u != join; u = parent[u]) {
|
deba@2440
|
768 |
flow[pred_edge[u]] += forward[u] ? val : -val;
|
deba@2440
|
769 |
}
|
deba@2440
|
770 |
}
|
deba@2440
|
771 |
// Updating the state of the entering and leaving edges
|
deba@2440
|
772 |
if (change) {
|
deba@2440
|
773 |
state[in_edge] = TREE;
|
deba@2440
|
774 |
state[pred_edge[u_out]] =
|
deba@2440
|
775 |
(flow[pred_edge[u_out]] == 0) ? LOWER : UPPER;
|
deba@2440
|
776 |
} else {
|
deba@2440
|
777 |
state[in_edge] = -state[in_edge];
|
deba@2440
|
778 |
}
|
deba@2440
|
779 |
}
|
deba@2440
|
780 |
|
deba@2440
|
781 |
/// \brief Updates thread and parent node maps.
|
deba@2440
|
782 |
void updateThreadParent() {
|
deba@2440
|
783 |
Node u;
|
deba@2440
|
784 |
v_out = parent[u_out];
|
deba@2440
|
785 |
|
deba@2440
|
786 |
// Handling the case when join and v_out coincide
|
deba@2440
|
787 |
bool par_first = false;
|
deba@2440
|
788 |
if (join == v_out) {
|
deba@2440
|
789 |
for (u = join; u != u_in && u != v_in; u = thread[u]) ;
|
deba@2440
|
790 |
if (u == v_in) {
|
deba@2440
|
791 |
par_first = true;
|
deba@2440
|
792 |
while (thread[u] != u_out) u = thread[u];
|
deba@2440
|
793 |
first = u;
|
deba@2440
|
794 |
}
|
deba@2440
|
795 |
}
|
deba@2440
|
796 |
|
deba@2440
|
797 |
// Finding the last successor of u_in (u) and the node after it
|
deba@2440
|
798 |
// (right) according to the thread index
|
deba@2440
|
799 |
for (u = u_in; depth[thread[u]] > depth[u_in]; u = thread[u]) ;
|
deba@2440
|
800 |
right = thread[u];
|
deba@2440
|
801 |
if (thread[v_in] == u_out) {
|
deba@2440
|
802 |
for (last = u; depth[last] > depth[u_out]; last = thread[last]) ;
|
deba@2440
|
803 |
if (last == u_out) last = thread[last];
|
deba@2440
|
804 |
}
|
deba@2440
|
805 |
else last = thread[v_in];
|
deba@2440
|
806 |
|
deba@2440
|
807 |
// Updating stem nodes
|
deba@2440
|
808 |
thread[v_in] = stem = u_in;
|
deba@2440
|
809 |
par_stem = v_in;
|
deba@2440
|
810 |
while (stem != u_out) {
|
deba@2440
|
811 |
thread[u] = new_stem = parent[stem];
|
deba@2440
|
812 |
|
deba@2440
|
813 |
// Finding the node just before the stem node (u) according to
|
deba@2440
|
814 |
// the original thread index
|
deba@2440
|
815 |
for (u = new_stem; thread[u] != stem; u = thread[u]) ;
|
deba@2440
|
816 |
thread[u] = right;
|
deba@2440
|
817 |
|
deba@2440
|
818 |
// Changing the parent node of stem and shifting stem and
|
deba@2440
|
819 |
// par_stem nodes
|
deba@2440
|
820 |
parent[stem] = par_stem;
|
deba@2440
|
821 |
par_stem = stem;
|
deba@2440
|
822 |
stem = new_stem;
|
deba@2440
|
823 |
|
deba@2440
|
824 |
// Finding the last successor of stem (u) and the node after it
|
deba@2440
|
825 |
// (right) according to the thread index
|
deba@2440
|
826 |
for (u = stem; depth[thread[u]] > depth[stem]; u = thread[u]) ;
|
deba@2440
|
827 |
right = thread[u];
|
deba@2440
|
828 |
}
|
deba@2440
|
829 |
parent[u_out] = par_stem;
|
deba@2440
|
830 |
thread[u] = last;
|
deba@2440
|
831 |
|
deba@2440
|
832 |
if (join == v_out && par_first) {
|
deba@2440
|
833 |
if (first != v_in) thread[first] = right;
|
deba@2440
|
834 |
} else {
|
deba@2440
|
835 |
for (u = v_out; thread[u] != u_out; u = thread[u]) ;
|
deba@2440
|
836 |
thread[u] = right;
|
deba@2440
|
837 |
}
|
deba@2440
|
838 |
}
|
deba@2440
|
839 |
|
deba@2440
|
840 |
/// \brief Updates pred_edge and forward node maps.
|
deba@2440
|
841 |
void updatePredEdge() {
|
deba@2440
|
842 |
Node u = u_out, v;
|
deba@2440
|
843 |
while (u != u_in) {
|
deba@2440
|
844 |
v = parent[u];
|
deba@2440
|
845 |
pred_edge[u] = pred_edge[v];
|
deba@2440
|
846 |
forward[u] = !forward[v];
|
deba@2440
|
847 |
u = v;
|
deba@2440
|
848 |
}
|
deba@2440
|
849 |
pred_edge[u_in] = in_edge;
|
deba@2440
|
850 |
forward[u_in] = (u_in == graph.source(in_edge));
|
deba@2440
|
851 |
}
|
deba@2440
|
852 |
|
deba@2440
|
853 |
/// \brief Updates depth and potential node maps.
|
deba@2440
|
854 |
void updateDepthPotential() {
|
deba@2440
|
855 |
depth[u_in] = depth[v_in] + 1;
|
deba@2440
|
856 |
potential[u_in] = forward[u_in] ?
|
deba@2440
|
857 |
potential[v_in] + cost[pred_edge[u_in]] :
|
deba@2440
|
858 |
potential[v_in] - cost[pred_edge[u_in]];
|
deba@2440
|
859 |
|
deba@2440
|
860 |
Node u = thread[u_in], v;
|
deba@2440
|
861 |
while (true) {
|
deba@2440
|
862 |
v = parent[u];
|
deba@2440
|
863 |
if (v == INVALID) break;
|
deba@2440
|
864 |
depth[u] = depth[v] + 1;
|
deba@2440
|
865 |
potential[u] = forward[u] ?
|
deba@2440
|
866 |
potential[v] + cost[pred_edge[u]] :
|
deba@2440
|
867 |
potential[v] - cost[pred_edge[u]];
|
deba@2440
|
868 |
if (depth[u] <= depth[v_in]) break;
|
deba@2440
|
869 |
u = thread[u];
|
deba@2440
|
870 |
}
|
deba@2440
|
871 |
}
|
deba@2440
|
872 |
|
deba@2440
|
873 |
/// \brief Executes the algorithm.
|
deba@2440
|
874 |
bool start() {
|
deba@2440
|
875 |
// Processing pivots
|
deba@2440
|
876 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
877 |
int iter_num = 0;
|
deba@2440
|
878 |
#endif
|
deba@2440
|
879 |
#if defined(FIRST_ELIGIBLE_PIVOT) || defined(EDGE_BLOCK_PIVOT)
|
deba@2440
|
880 |
EdgeIt next_edge(graph);
|
deba@2440
|
881 |
while (findEnteringEdge(next_edge))
|
deba@2440
|
882 |
#else
|
deba@2440
|
883 |
while (findEnteringEdge())
|
deba@2440
|
884 |
#endif
|
deba@2440
|
885 |
{
|
deba@2440
|
886 |
join = findJoinNode();
|
deba@2440
|
887 |
bool change = findLeavingEdge();
|
deba@2440
|
888 |
changeFlows(change);
|
deba@2440
|
889 |
if (change) {
|
deba@2440
|
890 |
updateThreadParent();
|
deba@2440
|
891 |
updatePredEdge();
|
deba@2440
|
892 |
updateDepthPotential();
|
deba@2440
|
893 |
}
|
deba@2440
|
894 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
895 |
++iter_num;
|
deba@2440
|
896 |
#endif
|
deba@2440
|
897 |
}
|
deba@2440
|
898 |
|
deba@2440
|
899 |
#ifdef _DEBUG_ITER_
|
deba@2440
|
900 |
std::cout << "Network Simplex algorithm finished. " << iter_num
|
deba@2444
|
901 |
<< " pivot iterations performed." << std::endl;
|
deba@2440
|
902 |
#endif
|
deba@2440
|
903 |
|
deba@2440
|
904 |
// Checking if the flow amount equals zero on all the
|
deba@2440
|
905 |
// artificial edges
|
deba@2440
|
906 |
for (InEdgeIt e(graph, root); e != INVALID; ++e)
|
deba@2440
|
907 |
if (flow[e] > 0) return false;
|
deba@2440
|
908 |
for (OutEdgeIt e(graph, root); e != INVALID; ++e)
|
deba@2440
|
909 |
if (flow[e] > 0) return false;
|
deba@2440
|
910 |
|
deba@2440
|
911 |
// Copying flow values to flow_result
|
deba@2440
|
912 |
if (lower) {
|
deba@2440
|
913 |
for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
|
deba@2440
|
914 |
flow_result[e] = (*lower)[e] + flow[edge_ref[e]];
|
deba@2440
|
915 |
} else {
|
deba@2440
|
916 |
for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
|
deba@2440
|
917 |
flow_result[e] = flow[edge_ref[e]];
|
deba@2440
|
918 |
}
|
deba@2440
|
919 |
// Copying potential values to potential_result
|
deba@2440
|
920 |
for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
|
deba@2440
|
921 |
potential_result[n] = potential[node_ref[n]];
|
deba@2440
|
922 |
|
deba@2440
|
923 |
return true;
|
deba@2440
|
924 |
}
|
deba@2440
|
925 |
|
deba@2440
|
926 |
}; //class NetworkSimplex
|
deba@2440
|
927 |
|
deba@2440
|
928 |
///@}
|
deba@2440
|
929 |
|
deba@2440
|
930 |
} //namespace lemon
|
deba@2440
|
931 |
|
deba@2440
|
932 |
#endif //LEMON_NETWORK_SIMPLEX_H
|