deba@2514
|
1 |
/* -*- C++ -*-
|
deba@2514
|
2 |
*
|
deba@2514
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2514
|
4 |
*
|
deba@2514
|
5 |
* Copyright (C) 2003-2007
|
deba@2514
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2514
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2514
|
8 |
*
|
deba@2514
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2514
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2514
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2514
|
12 |
*
|
deba@2514
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2514
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2514
|
15 |
* purpose.
|
deba@2514
|
16 |
*
|
deba@2514
|
17 |
*/
|
deba@2514
|
18 |
|
deba@2514
|
19 |
#ifndef LEMON_DINITZ_SLEATOR_TARJAN_H
|
deba@2514
|
20 |
#define LEMON_DINITZ_SLEATOR_TARJAN_H
|
deba@2514
|
21 |
|
deba@2514
|
22 |
/// \file
|
deba@2514
|
23 |
/// \ingroup max_flow
|
deba@2514
|
24 |
/// \brief Implementation the dynamic tree data structure of Sleator
|
deba@2514
|
25 |
/// and Tarjan.
|
deba@2514
|
26 |
|
deba@2514
|
27 |
#include <lemon/graph_utils.h>
|
deba@2514
|
28 |
#include <lemon/tolerance.h>
|
deba@2514
|
29 |
#include <lemon/dynamic_tree.h>
|
deba@2514
|
30 |
|
deba@2514
|
31 |
#include <vector>
|
deba@2514
|
32 |
#include <limits>
|
deba@2514
|
33 |
#include <fstream>
|
deba@2514
|
34 |
|
deba@2514
|
35 |
|
deba@2514
|
36 |
namespace lemon {
|
deba@2514
|
37 |
|
deba@2514
|
38 |
/// \brief Default traits class of DinitzSleatorTarjan class.
|
deba@2514
|
39 |
///
|
deba@2514
|
40 |
/// Default traits class of DinitzSleatorTarjan class.
|
deba@2514
|
41 |
/// \param _Graph Graph type.
|
deba@2514
|
42 |
/// \param _CapacityMap Type of capacity map.
|
deba@2514
|
43 |
template <typename _Graph, typename _CapacityMap>
|
deba@2514
|
44 |
struct DinitzSleatorTarjanDefaultTraits {
|
deba@2514
|
45 |
|
deba@2514
|
46 |
/// \brief The graph type the algorithm runs on.
|
deba@2514
|
47 |
typedef _Graph Graph;
|
deba@2514
|
48 |
|
deba@2514
|
49 |
/// \brief The type of the map that stores the edge capacities.
|
deba@2514
|
50 |
///
|
deba@2514
|
51 |
/// The type of the map that stores the edge capacities.
|
deba@2514
|
52 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
deba@2514
|
53 |
typedef _CapacityMap CapacityMap;
|
deba@2514
|
54 |
|
deba@2514
|
55 |
/// \brief The type of the length of the edges.
|
deba@2514
|
56 |
typedef typename CapacityMap::Value Value;
|
deba@2514
|
57 |
|
deba@2514
|
58 |
/// \brief The map type that stores the flow values.
|
deba@2514
|
59 |
///
|
deba@2514
|
60 |
/// The map type that stores the flow values.
|
deba@2514
|
61 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
deba@2514
|
62 |
typedef typename Graph::template EdgeMap<Value> FlowMap;
|
deba@2514
|
63 |
|
deba@2514
|
64 |
/// \brief Instantiates a FlowMap.
|
deba@2514
|
65 |
///
|
deba@2514
|
66 |
/// This function instantiates a \ref FlowMap.
|
deba@2514
|
67 |
/// \param graph The graph, to which we would like to define the flow map.
|
deba@2514
|
68 |
static FlowMap* createFlowMap(const Graph& graph) {
|
deba@2514
|
69 |
return new FlowMap(graph);
|
deba@2514
|
70 |
}
|
deba@2514
|
71 |
|
deba@2514
|
72 |
/// \brief The tolerance used by the algorithm
|
deba@2514
|
73 |
///
|
deba@2514
|
74 |
/// The tolerance used by the algorithm to handle inexact computation.
|
deba@2514
|
75 |
typedef Tolerance<Value> Tolerance;
|
deba@2514
|
76 |
|
deba@2514
|
77 |
};
|
deba@2514
|
78 |
|
deba@2514
|
79 |
/// \ingroup max_flow
|
deba@2514
|
80 |
///
|
deba@2514
|
81 |
/// \brief Dinitz-Sleator-Tarjan algorithms class.
|
deba@2514
|
82 |
///
|
deba@2514
|
83 |
/// This class provides an implementation of the \e
|
deba@2514
|
84 |
/// Dinitz-Sleator-Tarjan \e algorithm producing a flow of maximum
|
deba@2514
|
85 |
/// value in a directed graph. The DinitzSleatorTarjan algorithm is
|
deba@2514
|
86 |
/// the fastest known max flow algorithms wich using blocking
|
deba@2514
|
87 |
/// flow. It is an improvement of the Dinitz algorithm by using the
|
deba@2514
|
88 |
/// \ref DynamicTree "dynamic tree" data structure of Sleator and
|
deba@2514
|
89 |
/// Tarjan.
|
deba@2514
|
90 |
///
|
deba@2514
|
91 |
/// This blocking flow algorithms builds a layered graph according
|
deba@2514
|
92 |
/// to \ref Bfs "breadth-first search" distance from the target node
|
deba@2514
|
93 |
/// in the reversed residual graph. The layered graph contains each
|
deba@2514
|
94 |
/// residual edge which steps one level down. After that the
|
deba@2514
|
95 |
/// algorithm constructs a blocking flow on the layered graph and
|
deba@2514
|
96 |
/// augments the overall flow with it. The number of the levels in
|
deba@2514
|
97 |
/// the layered graph is strictly increasing in each augmenting
|
deba@2514
|
98 |
/// phase therefore the number of the augmentings is at most
|
deba@2514
|
99 |
/// \f$n-1\f$. The length of each phase is at most
|
deba@2514
|
100 |
/// \f$O(m\log(n))\f$, that the overall time complexity is
|
deba@2514
|
101 |
/// \f$O(nm\log(n))\f$.
|
deba@2514
|
102 |
///
|
deba@2514
|
103 |
/// \param _Graph The directed graph type the algorithm runs on.
|
deba@2514
|
104 |
/// \param _CapacityMap The capacity map type.
|
deba@2514
|
105 |
/// \param _Traits Traits class to set various data types used by
|
deba@2514
|
106 |
/// the algorithm. The default traits class is \ref
|
deba@2514
|
107 |
/// DinitzSleatorTarjanDefaultTraits. See \ref
|
deba@2514
|
108 |
/// DinitzSleatorTarjanDefaultTraits for the documentation of a
|
deba@2514
|
109 |
/// Dinitz-Sleator-Tarjan traits class.
|
deba@2514
|
110 |
///
|
deba@2514
|
111 |
/// \author Tamas Hamori and Balazs Dezso
|
deba@2514
|
112 |
#ifdef DOXYGEN
|
deba@2514
|
113 |
template <typename _Graph, typename _CapacityMap, typename _Traits>
|
deba@2514
|
114 |
#else
|
deba@2514
|
115 |
template <typename _Graph,
|
deba@2514
|
116 |
typename _CapacityMap = typename _Graph::template EdgeMap<int>,
|
deba@2514
|
117 |
typename _Traits =
|
deba@2514
|
118 |
DinitzSleatorTarjanDefaultTraits<_Graph, _CapacityMap> >
|
deba@2514
|
119 |
#endif
|
deba@2514
|
120 |
class DinitzSleatorTarjan {
|
deba@2514
|
121 |
public:
|
deba@2514
|
122 |
|
deba@2514
|
123 |
typedef _Traits Traits;
|
deba@2514
|
124 |
typedef typename Traits::Graph Graph;
|
deba@2514
|
125 |
typedef typename Traits::CapacityMap CapacityMap;
|
deba@2514
|
126 |
typedef typename Traits::Value Value;
|
deba@2514
|
127 |
|
deba@2514
|
128 |
typedef typename Traits::FlowMap FlowMap;
|
deba@2514
|
129 |
typedef typename Traits::Tolerance Tolerance;
|
deba@2514
|
130 |
|
deba@2514
|
131 |
|
deba@2514
|
132 |
private:
|
deba@2514
|
133 |
|
deba@2514
|
134 |
GRAPH_TYPEDEFS(typename Graph);
|
deba@2514
|
135 |
|
deba@2514
|
136 |
|
deba@2514
|
137 |
typedef typename Graph::template NodeMap<int> LevelMap;
|
deba@2514
|
138 |
typedef typename Graph::template NodeMap<int> IntNodeMap;
|
deba@2514
|
139 |
typedef typename Graph::template NodeMap<Edge> EdgeNodeMap;
|
deba@2514
|
140 |
typedef DynamicTree<Value, IntNodeMap, Tolerance, false> DynTree;
|
deba@2514
|
141 |
|
deba@2514
|
142 |
private:
|
deba@2514
|
143 |
|
deba@2514
|
144 |
const Graph& _graph;
|
deba@2514
|
145 |
const CapacityMap* _capacity;
|
deba@2514
|
146 |
|
deba@2514
|
147 |
Node _source, _target;
|
deba@2514
|
148 |
|
deba@2514
|
149 |
FlowMap* _flow;
|
deba@2514
|
150 |
bool _local_flow;
|
deba@2514
|
151 |
|
deba@2514
|
152 |
IntNodeMap* _level;
|
deba@2514
|
153 |
EdgeNodeMap* _dt_edges;
|
deba@2514
|
154 |
|
deba@2514
|
155 |
IntNodeMap* _dt_index;
|
deba@2514
|
156 |
DynTree* _dt;
|
deba@2514
|
157 |
|
deba@2519
|
158 |
std::vector<Node> _queue;
|
deba@2519
|
159 |
|
deba@2514
|
160 |
Tolerance _tolerance;
|
deba@2514
|
161 |
|
deba@2514
|
162 |
Value _flow_value;
|
deba@2514
|
163 |
Value _max_value;
|
deba@2514
|
164 |
|
deba@2514
|
165 |
|
deba@2514
|
166 |
public:
|
deba@2514
|
167 |
|
deba@2514
|
168 |
typedef DinitzSleatorTarjan Create;
|
deba@2514
|
169 |
|
deba@2514
|
170 |
///\name Named template parameters
|
deba@2514
|
171 |
|
deba@2514
|
172 |
///@{
|
deba@2514
|
173 |
|
deba@2514
|
174 |
template <typename _FlowMap>
|
deba@2514
|
175 |
struct DefFlowMapTraits : public Traits {
|
deba@2514
|
176 |
typedef _FlowMap FlowMap;
|
deba@2514
|
177 |
static FlowMap *createFlowMap(const Graph&) {
|
deba@2514
|
178 |
throw UninitializedParameter();
|
deba@2514
|
179 |
}
|
deba@2514
|
180 |
};
|
deba@2514
|
181 |
|
deba@2514
|
182 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
deba@2514
|
183 |
/// FlowMap type
|
deba@2514
|
184 |
///
|
deba@2514
|
185 |
/// \ref named-templ-param "Named parameter" for setting FlowMap
|
deba@2514
|
186 |
/// type
|
deba@2514
|
187 |
template <typename _FlowMap>
|
deba@2514
|
188 |
struct DefFlowMap
|
deba@2514
|
189 |
: public DinitzSleatorTarjan<Graph, CapacityMap,
|
deba@2514
|
190 |
DefFlowMapTraits<_FlowMap> > {
|
deba@2514
|
191 |
typedef DinitzSleatorTarjan<Graph, CapacityMap,
|
deba@2514
|
192 |
DefFlowMapTraits<_FlowMap> > Create;
|
deba@2514
|
193 |
};
|
deba@2514
|
194 |
|
deba@2514
|
195 |
template <typename _Elevator>
|
deba@2514
|
196 |
struct DefElevatorTraits : public Traits {
|
deba@2514
|
197 |
typedef _Elevator Elevator;
|
deba@2514
|
198 |
static Elevator *createElevator(const Graph&, int) {
|
deba@2514
|
199 |
throw UninitializedParameter();
|
deba@2514
|
200 |
}
|
deba@2514
|
201 |
};
|
deba@2514
|
202 |
|
deba@2514
|
203 |
/// @}
|
deba@2514
|
204 |
|
deba@2514
|
205 |
/// \brief \ref Exception for the case when the source equals the target.
|
deba@2514
|
206 |
///
|
deba@2514
|
207 |
/// \ref Exception for the case when the source equals the target.
|
deba@2514
|
208 |
///
|
deba@2514
|
209 |
class InvalidArgument : public lemon::LogicError {
|
deba@2514
|
210 |
public:
|
deba@2514
|
211 |
virtual const char* what() const throw() {
|
deba@2514
|
212 |
return "lemon::DinitzSleatorTarjan::InvalidArgument";
|
deba@2514
|
213 |
}
|
deba@2514
|
214 |
};
|
deba@2514
|
215 |
|
deba@2514
|
216 |
/// \brief The constructor of the class.
|
deba@2514
|
217 |
///
|
deba@2514
|
218 |
/// The constructor of the class.
|
deba@2514
|
219 |
/// \param graph The directed graph the algorithm runs on.
|
deba@2514
|
220 |
/// \param capacity The capacity of the edges.
|
deba@2514
|
221 |
/// \param source The source node.
|
deba@2514
|
222 |
/// \param target The target node.
|
deba@2514
|
223 |
DinitzSleatorTarjan(const Graph& graph, const CapacityMap& capacity,
|
deba@2514
|
224 |
Node source, Node target)
|
deba@2514
|
225 |
: _graph(graph), _capacity(&capacity),
|
deba@2514
|
226 |
_source(source), _target(target),
|
deba@2514
|
227 |
_flow(0), _local_flow(false),
|
deba@2514
|
228 |
_level(0), _dt_edges(0),
|
deba@2519
|
229 |
_dt_index(0), _dt(0), _queue(),
|
deba@2514
|
230 |
_tolerance(), _flow_value(), _max_value()
|
deba@2514
|
231 |
{
|
deba@2514
|
232 |
if (_source == _target) {
|
deba@2514
|
233 |
throw InvalidArgument();
|
deba@2514
|
234 |
}
|
deba@2514
|
235 |
}
|
deba@2514
|
236 |
|
deba@2514
|
237 |
/// \brief Destrcutor.
|
deba@2514
|
238 |
///
|
deba@2514
|
239 |
/// Destructor.
|
deba@2514
|
240 |
~DinitzSleatorTarjan() {
|
deba@2514
|
241 |
destroyStructures();
|
deba@2514
|
242 |
}
|
deba@2514
|
243 |
|
deba@2514
|
244 |
/// \brief Sets the capacity map.
|
deba@2514
|
245 |
///
|
deba@2514
|
246 |
/// Sets the capacity map.
|
deba@2514
|
247 |
/// \return \c (*this)
|
deba@2514
|
248 |
DinitzSleatorTarjan& capacityMap(const CapacityMap& map) {
|
deba@2514
|
249 |
_capacity = ↦
|
deba@2514
|
250 |
return *this;
|
deba@2514
|
251 |
}
|
deba@2514
|
252 |
|
deba@2514
|
253 |
/// \brief Sets the flow map.
|
deba@2514
|
254 |
///
|
deba@2514
|
255 |
/// Sets the flow map.
|
deba@2514
|
256 |
/// \return \c (*this)
|
deba@2514
|
257 |
DinitzSleatorTarjan& flowMap(FlowMap& map) {
|
deba@2514
|
258 |
if (_local_flow) {
|
deba@2514
|
259 |
delete _flow;
|
deba@2514
|
260 |
_local_flow = false;
|
deba@2514
|
261 |
}
|
deba@2514
|
262 |
_flow = ↦
|
deba@2514
|
263 |
return *this;
|
deba@2514
|
264 |
}
|
deba@2514
|
265 |
|
deba@2514
|
266 |
/// \brief Returns the flow map.
|
deba@2514
|
267 |
///
|
deba@2514
|
268 |
/// \return The flow map.
|
deba@2514
|
269 |
const FlowMap& flowMap() {
|
deba@2514
|
270 |
return *_flow;
|
deba@2514
|
271 |
}
|
deba@2514
|
272 |
|
deba@2514
|
273 |
/// \brief Sets the source node.
|
deba@2514
|
274 |
///
|
deba@2514
|
275 |
/// Sets the source node.
|
deba@2514
|
276 |
/// \return \c (*this)
|
deba@2514
|
277 |
DinitzSleatorTarjan& source(const Node& node) {
|
deba@2514
|
278 |
_source = node;
|
deba@2514
|
279 |
return *this;
|
deba@2514
|
280 |
}
|
deba@2514
|
281 |
|
deba@2514
|
282 |
/// \brief Sets the target node.
|
deba@2514
|
283 |
///
|
deba@2514
|
284 |
/// Sets the target node.
|
deba@2514
|
285 |
/// \return \c (*this)
|
deba@2514
|
286 |
DinitzSleatorTarjan& target(const Node& node) {
|
deba@2514
|
287 |
_target = node;
|
deba@2514
|
288 |
return *this;
|
deba@2514
|
289 |
}
|
deba@2514
|
290 |
|
deba@2514
|
291 |
/// \brief Sets the tolerance used by algorithm.
|
deba@2514
|
292 |
///
|
deba@2514
|
293 |
/// Sets the tolerance used by algorithm.
|
deba@2514
|
294 |
DinitzSleatorTarjan& tolerance(const Tolerance& tolerance) const {
|
deba@2514
|
295 |
_tolerance = tolerance;
|
deba@2514
|
296 |
if (_dt) {
|
deba@2514
|
297 |
_dt.tolerance(_tolerance);
|
deba@2514
|
298 |
}
|
deba@2514
|
299 |
return *this;
|
deba@2514
|
300 |
}
|
deba@2514
|
301 |
|
deba@2514
|
302 |
/// \brief Returns the tolerance used by algorithm.
|
deba@2514
|
303 |
///
|
deba@2514
|
304 |
/// Returns the tolerance used by algorithm.
|
deba@2514
|
305 |
const Tolerance& tolerance() const {
|
deba@2514
|
306 |
return tolerance;
|
deba@2514
|
307 |
}
|
deba@2514
|
308 |
|
deba@2514
|
309 |
private:
|
deba@2514
|
310 |
|
deba@2514
|
311 |
void createStructures() {
|
deba@2514
|
312 |
if (!_flow) {
|
deba@2514
|
313 |
_flow = Traits::createFlowMap(_graph);
|
deba@2514
|
314 |
_local_flow = true;
|
deba@2514
|
315 |
}
|
deba@2514
|
316 |
if (!_level) {
|
deba@2514
|
317 |
_level = new LevelMap(_graph);
|
deba@2514
|
318 |
}
|
deba@2514
|
319 |
if (!_dt_index && !_dt) {
|
deba@2514
|
320 |
_dt_index = new IntNodeMap(_graph);
|
deba@2514
|
321 |
_dt = new DynTree(*_dt_index, _tolerance);
|
deba@2514
|
322 |
}
|
deba@2514
|
323 |
if (!_dt_edges) {
|
deba@2514
|
324 |
_dt_edges = new EdgeNodeMap(_graph);
|
deba@2514
|
325 |
}
|
deba@2519
|
326 |
_queue.resize(countNodes(_graph));
|
deba@2514
|
327 |
_max_value = _dt->maxValue();
|
deba@2514
|
328 |
}
|
deba@2514
|
329 |
|
deba@2514
|
330 |
void destroyStructures() {
|
deba@2514
|
331 |
if (_local_flow) {
|
deba@2514
|
332 |
delete _flow;
|
deba@2514
|
333 |
}
|
deba@2514
|
334 |
if (_level) {
|
deba@2514
|
335 |
delete _level;
|
deba@2514
|
336 |
}
|
deba@2514
|
337 |
if (_dt) {
|
deba@2514
|
338 |
delete _dt;
|
deba@2514
|
339 |
}
|
deba@2514
|
340 |
if (_dt_index) {
|
deba@2514
|
341 |
delete _dt_index;
|
deba@2514
|
342 |
}
|
deba@2514
|
343 |
if (_dt_edges) {
|
deba@2514
|
344 |
delete _dt_edges;
|
deba@2514
|
345 |
}
|
deba@2514
|
346 |
}
|
deba@2514
|
347 |
|
deba@2514
|
348 |
bool createLayeredGraph() {
|
deba@2514
|
349 |
|
deba@2514
|
350 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
351 |
_level->set(n, -2);
|
deba@2514
|
352 |
}
|
deba@2514
|
353 |
|
deba@2514
|
354 |
int level = 0;
|
deba@2514
|
355 |
|
deba@2519
|
356 |
_queue[0] = _target;
|
deba@2514
|
357 |
_level->set(_target, level);
|
deba@2519
|
358 |
|
deba@2519
|
359 |
int first = 0, last = 1, limit = 0;
|
deba@2514
|
360 |
|
deba@2519
|
361 |
while (first != last && (*_level)[_source] == -2) {
|
deba@2519
|
362 |
if (first == limit) {
|
deba@2519
|
363 |
limit = last;
|
deba@2519
|
364 |
++level;
|
deba@2519
|
365 |
}
|
deba@2514
|
366 |
|
deba@2519
|
367 |
Node n = _queue[first++];
|
deba@2514
|
368 |
|
deba@2519
|
369 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2519
|
370 |
Node v = _graph.target(e);
|
deba@2519
|
371 |
if ((*_level)[v] != -2) continue;
|
deba@2519
|
372 |
Value rem = (*_flow)[e];
|
deba@2519
|
373 |
if (!_tolerance.positive(rem)) continue;
|
deba@2519
|
374 |
_level->set(v, level);
|
deba@2519
|
375 |
_queue[last++] = v;
|
deba@2514
|
376 |
}
|
deba@2519
|
377 |
|
deba@2519
|
378 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2519
|
379 |
Node v = _graph.source(e);
|
deba@2519
|
380 |
if ((*_level)[v] != -2) continue;
|
deba@2519
|
381 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2519
|
382 |
if (!_tolerance.positive(rem)) continue;
|
deba@2519
|
383 |
_level->set(v, level);
|
deba@2519
|
384 |
_queue[last++] = v;
|
deba@2519
|
385 |
}
|
deba@2514
|
386 |
}
|
deba@2514
|
387 |
return (*_level)[_source] != -2;
|
deba@2514
|
388 |
}
|
deba@2514
|
389 |
|
deba@2514
|
390 |
void initEdges() {
|
deba@2514
|
391 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
392 |
_graph.firstOut((*_dt_edges)[n], n);
|
deba@2514
|
393 |
}
|
deba@2514
|
394 |
}
|
deba@2514
|
395 |
|
deba@2514
|
396 |
|
deba@2514
|
397 |
void augmentPath() {
|
deba@2514
|
398 |
Value rem;
|
deba@2514
|
399 |
Node n = _dt->findCost(_source, rem);
|
deba@2514
|
400 |
_flow_value += rem;
|
deba@2514
|
401 |
_dt->addCost(_source, - rem);
|
deba@2514
|
402 |
|
deba@2514
|
403 |
_dt->cut(n);
|
deba@2514
|
404 |
_dt->addCost(n, _max_value);
|
deba@2514
|
405 |
|
deba@2514
|
406 |
Edge e = (*_dt_edges)[n];
|
deba@2514
|
407 |
if (_graph.source(e) == n) {
|
deba@2514
|
408 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
409 |
|
deba@2514
|
410 |
_graph.nextOut(e);
|
deba@2514
|
411 |
if (e == INVALID) {
|
deba@2514
|
412 |
_graph.firstIn(e, n);
|
deba@2514
|
413 |
}
|
deba@2514
|
414 |
} else {
|
deba@2514
|
415 |
_flow->set(e, 0);
|
deba@2514
|
416 |
_graph.nextIn(e);
|
deba@2514
|
417 |
}
|
deba@2514
|
418 |
_dt_edges->set(n, e);
|
deba@2514
|
419 |
|
deba@2514
|
420 |
}
|
deba@2514
|
421 |
|
deba@2514
|
422 |
bool advance(Node n) {
|
deba@2514
|
423 |
Edge e = (*_dt_edges)[n];
|
deba@2514
|
424 |
if (e == INVALID) return false;
|
deba@2514
|
425 |
|
deba@2514
|
426 |
Node u;
|
deba@2514
|
427 |
Value rem;
|
deba@2514
|
428 |
if (_graph.source(e) == n) {
|
deba@2514
|
429 |
u = _graph.target(e);
|
deba@2514
|
430 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
431 |
!_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
deba@2514
|
432 |
_graph.nextOut(e);
|
deba@2514
|
433 |
if (e == INVALID) break;
|
deba@2514
|
434 |
u = _graph.target(e);
|
deba@2514
|
435 |
}
|
deba@2514
|
436 |
if (e != INVALID) {
|
deba@2514
|
437 |
rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
438 |
} else {
|
deba@2514
|
439 |
_graph.firstIn(e, n);
|
deba@2514
|
440 |
if (e == INVALID) {
|
deba@2514
|
441 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
442 |
return false;
|
deba@2514
|
443 |
}
|
deba@2514
|
444 |
u = _graph.source(e);
|
deba@2514
|
445 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
446 |
!_tolerance.positive((*_flow)[e])) {
|
deba@2514
|
447 |
_graph.nextIn(e);
|
deba@2514
|
448 |
if (e == INVALID) {
|
deba@2514
|
449 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
450 |
return false;
|
deba@2514
|
451 |
}
|
deba@2514
|
452 |
u = _graph.source(e);
|
deba@2514
|
453 |
}
|
deba@2514
|
454 |
rem = (*_flow)[e];
|
deba@2514
|
455 |
}
|
deba@2514
|
456 |
} else {
|
deba@2514
|
457 |
u = _graph.source(e);
|
deba@2514
|
458 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
459 |
!_tolerance.positive((*_flow)[e])) {
|
deba@2514
|
460 |
_graph.nextIn(e);
|
deba@2514
|
461 |
if (e == INVALID) {
|
deba@2514
|
462 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
463 |
return false;
|
deba@2514
|
464 |
}
|
deba@2514
|
465 |
u = _graph.source(e);
|
deba@2514
|
466 |
}
|
deba@2514
|
467 |
rem = (*_flow)[e];
|
deba@2514
|
468 |
}
|
deba@2514
|
469 |
|
deba@2514
|
470 |
_dt->addCost(n, - std::numeric_limits<Value>::max());
|
deba@2514
|
471 |
_dt->addCost(n, rem);
|
deba@2514
|
472 |
_dt->link(n, u);
|
deba@2514
|
473 |
_dt_edges->set(n, e);
|
deba@2514
|
474 |
return true;
|
deba@2514
|
475 |
}
|
deba@2514
|
476 |
|
deba@2514
|
477 |
void retreat(Node n) {
|
deba@2514
|
478 |
_level->set(n, -1);
|
deba@2514
|
479 |
|
deba@2514
|
480 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
481 |
Node u = _graph.target(e);
|
deba@2514
|
482 |
if ((*_dt_edges)[u] == e && _dt->findRoot(u) == n) {
|
deba@2514
|
483 |
Value rem;
|
deba@2514
|
484 |
_dt->findCost(u, rem);
|
deba@2514
|
485 |
_flow->set(e, rem);
|
deba@2514
|
486 |
_dt->cut(u);
|
deba@2514
|
487 |
_dt->addCost(u, - rem);
|
deba@2514
|
488 |
_dt->addCost(u, _max_value);
|
deba@2514
|
489 |
}
|
deba@2514
|
490 |
}
|
deba@2514
|
491 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
492 |
Node u = _graph.source(e);
|
deba@2514
|
493 |
if ((*_dt_edges)[u] == e && _dt->findRoot(u) == n) {
|
deba@2514
|
494 |
Value rem;
|
deba@2514
|
495 |
_dt->findCost(u, rem);
|
deba@2514
|
496 |
_flow->set(e, (*_capacity)[e] - rem);
|
deba@2514
|
497 |
_dt->cut(u);
|
deba@2514
|
498 |
_dt->addCost(u, - rem);
|
deba@2514
|
499 |
_dt->addCost(u, _max_value);
|
deba@2514
|
500 |
}
|
deba@2514
|
501 |
}
|
deba@2514
|
502 |
}
|
deba@2514
|
503 |
|
deba@2514
|
504 |
void extractTrees() {
|
deba@2514
|
505 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
506 |
|
deba@2514
|
507 |
Node w = _dt->findRoot(n);
|
deba@2514
|
508 |
|
deba@2514
|
509 |
while (w != n) {
|
deba@2514
|
510 |
|
deba@2514
|
511 |
Value rem;
|
deba@2514
|
512 |
Node u = _dt->findCost(n, rem);
|
deba@2514
|
513 |
|
deba@2514
|
514 |
_dt->cut(u);
|
deba@2514
|
515 |
_dt->addCost(u, - rem);
|
deba@2514
|
516 |
_dt->addCost(u, _max_value);
|
deba@2514
|
517 |
|
deba@2514
|
518 |
Edge e = (*_dt_edges)[u];
|
deba@2514
|
519 |
_dt_edges->set(u, INVALID);
|
deba@2514
|
520 |
|
deba@2514
|
521 |
if (u == _graph.source(e)) {
|
deba@2514
|
522 |
_flow->set(e, (*_capacity)[e] - rem);
|
deba@2514
|
523 |
} else {
|
deba@2514
|
524 |
_flow->set(e, rem);
|
deba@2514
|
525 |
}
|
deba@2514
|
526 |
|
deba@2514
|
527 |
w = _dt->findRoot(n);
|
deba@2514
|
528 |
}
|
deba@2514
|
529 |
}
|
deba@2514
|
530 |
}
|
deba@2514
|
531 |
|
deba@2514
|
532 |
|
deba@2514
|
533 |
public:
|
deba@2514
|
534 |
|
deba@2514
|
535 |
/// \name Execution control The simplest way to execute the
|
deba@2514
|
536 |
/// algorithm is to use the \c run() member functions.
|
deba@2514
|
537 |
/// \n
|
deba@2514
|
538 |
/// If you need more control on initial solution or
|
deba@2514
|
539 |
/// execution then you have to call one \ref init() function and then
|
deba@2514
|
540 |
/// the start() or multiple times the \c augment() member function.
|
deba@2514
|
541 |
|
deba@2514
|
542 |
///@{
|
deba@2514
|
543 |
|
deba@2514
|
544 |
/// \brief Initializes the algorithm
|
deba@2514
|
545 |
///
|
deba@2514
|
546 |
/// It sets the flow to empty flow.
|
deba@2514
|
547 |
void init() {
|
deba@2514
|
548 |
createStructures();
|
deba@2514
|
549 |
|
deba@2514
|
550 |
_dt->clear();
|
deba@2514
|
551 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
552 |
_dt->makeTree(n);
|
deba@2514
|
553 |
_dt->addCost(n, _max_value);
|
deba@2514
|
554 |
}
|
deba@2514
|
555 |
|
deba@2514
|
556 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
557 |
_flow->set(it, 0);
|
deba@2514
|
558 |
}
|
deba@2514
|
559 |
_flow_value = 0;
|
deba@2514
|
560 |
}
|
deba@2514
|
561 |
|
deba@2514
|
562 |
/// \brief Initializes the algorithm
|
deba@2514
|
563 |
///
|
deba@2514
|
564 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
565 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
566 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
567 |
/// outgoing flow.
|
deba@2514
|
568 |
template <typename FlowMap>
|
deba@2514
|
569 |
void flowInit(const FlowMap& flowMap) {
|
deba@2514
|
570 |
createStructures();
|
deba@2514
|
571 |
|
deba@2514
|
572 |
_dt->clear();
|
deba@2514
|
573 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
574 |
_dt->makeTree(n);
|
deba@2514
|
575 |
_dt->addCost(n, _max_value);
|
deba@2514
|
576 |
}
|
deba@2514
|
577 |
|
deba@2514
|
578 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
579 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
580 |
}
|
deba@2514
|
581 |
_flow_value = 0;
|
deba@2514
|
582 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
583 |
_flow_value += (*_flow)[jt];
|
deba@2514
|
584 |
}
|
deba@2514
|
585 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
586 |
_flow_value -= (*_flow)[jt];
|
deba@2514
|
587 |
}
|
deba@2514
|
588 |
}
|
deba@2514
|
589 |
|
deba@2514
|
590 |
/// \brief Initializes the algorithm
|
deba@2514
|
591 |
///
|
deba@2514
|
592 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
593 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
594 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
595 |
/// outgoing flow.
|
deba@2514
|
596 |
/// \return %False when the given flowMap does not contain
|
deba@2514
|
597 |
/// feasible flow.
|
deba@2514
|
598 |
template <typename FlowMap>
|
deba@2514
|
599 |
bool checkedFlowInit(const FlowMap& flowMap) {
|
deba@2514
|
600 |
createStructures();
|
deba@2514
|
601 |
|
deba@2514
|
602 |
_dt->clear();
|
deba@2514
|
603 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
604 |
_dt->makeTree(n);
|
deba@2514
|
605 |
_dt->addCost(n, _max_value);
|
deba@2514
|
606 |
}
|
deba@2514
|
607 |
|
deba@2514
|
608 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
609 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
610 |
}
|
deba@2514
|
611 |
for (NodeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
612 |
if (it == _source || it == _target) continue;
|
deba@2514
|
613 |
Value outFlow = 0;
|
deba@2514
|
614 |
for (OutEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
615 |
outFlow += (*_flow)[jt];
|
deba@2514
|
616 |
}
|
deba@2514
|
617 |
Value inFlow = 0;
|
deba@2514
|
618 |
for (InEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
619 |
inFlow += (*_flow)[jt];
|
deba@2514
|
620 |
}
|
deba@2514
|
621 |
if (_tolerance.different(outFlow, inFlow)) {
|
deba@2514
|
622 |
return false;
|
deba@2514
|
623 |
}
|
deba@2514
|
624 |
}
|
deba@2514
|
625 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
626 |
if (_tolerance.less((*_flow)[it], 0)) return false;
|
deba@2514
|
627 |
if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false;
|
deba@2514
|
628 |
}
|
deba@2514
|
629 |
_flow_value = 0;
|
deba@2514
|
630 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
631 |
_flow_value += (*_flow)[jt];
|
deba@2514
|
632 |
}
|
deba@2514
|
633 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
634 |
_flow_value -= (*_flow)[jt];
|
deba@2514
|
635 |
}
|
deba@2514
|
636 |
return true;
|
deba@2514
|
637 |
}
|
deba@2514
|
638 |
|
deba@2514
|
639 |
/// \brief Executes the algorithm
|
deba@2514
|
640 |
///
|
deba@2514
|
641 |
/// It runs augmenting phases by adding blocking flow until the
|
deba@2514
|
642 |
/// optimal solution is reached.
|
deba@2514
|
643 |
void start() {
|
deba@2514
|
644 |
while (augment());
|
deba@2514
|
645 |
}
|
deba@2514
|
646 |
|
deba@2514
|
647 |
/// \brief Augments the flow with a blocking flow on a layered
|
deba@2514
|
648 |
/// graph.
|
deba@2514
|
649 |
///
|
deba@2514
|
650 |
/// This function builds a layered graph and then find a blocking
|
deba@2514
|
651 |
/// flow on this graph. The number of the levels in the layered
|
deba@2514
|
652 |
/// graph is strictly increasing in each augmenting phase
|
deba@2514
|
653 |
/// therefore the number of the augmentings is at most \f$ n-1
|
deba@2514
|
654 |
/// \f$. The length of each phase is at most \f$ O(m \log(n))
|
deba@2514
|
655 |
/// \f$, that the overall time complexity is \f$ O(nm \log(n)) \f$.
|
deba@2514
|
656 |
/// \return %False when there is not residual path between the
|
deba@2514
|
657 |
/// source and the target so the current flow is a feasible and
|
deba@2514
|
658 |
/// optimal solution.
|
deba@2514
|
659 |
bool augment() {
|
deba@2514
|
660 |
Node n;
|
deba@2514
|
661 |
|
deba@2514
|
662 |
if (createLayeredGraph()) {
|
deba@2514
|
663 |
|
deba@2514
|
664 |
Timer bf_timer;
|
deba@2514
|
665 |
initEdges();
|
deba@2514
|
666 |
|
deba@2514
|
667 |
n = _dt->findRoot(_source);
|
deba@2514
|
668 |
while (true) {
|
deba@2514
|
669 |
Edge e;
|
deba@2514
|
670 |
if (n == _target) {
|
deba@2514
|
671 |
augmentPath();
|
deba@2514
|
672 |
} else if (!advance(n)) {
|
deba@2514
|
673 |
if (n != _source) {
|
deba@2514
|
674 |
retreat(n);
|
deba@2514
|
675 |
} else {
|
deba@2514
|
676 |
break;
|
deba@2514
|
677 |
}
|
deba@2514
|
678 |
}
|
deba@2514
|
679 |
n = _dt->findRoot(_source);
|
deba@2514
|
680 |
}
|
deba@2514
|
681 |
extractTrees();
|
deba@2514
|
682 |
|
deba@2514
|
683 |
return true;
|
deba@2514
|
684 |
} else {
|
deba@2514
|
685 |
return false;
|
deba@2514
|
686 |
}
|
deba@2514
|
687 |
}
|
deba@2514
|
688 |
|
deba@2514
|
689 |
/// \brief runs the algorithm.
|
deba@2514
|
690 |
///
|
deba@2514
|
691 |
/// It is just a shorthand for:
|
deba@2514
|
692 |
///
|
deba@2514
|
693 |
///\code
|
deba@2514
|
694 |
/// ek.init();
|
deba@2514
|
695 |
/// ek.start();
|
deba@2514
|
696 |
///\endcode
|
deba@2514
|
697 |
void run() {
|
deba@2514
|
698 |
init();
|
deba@2514
|
699 |
start();
|
deba@2514
|
700 |
}
|
deba@2514
|
701 |
|
deba@2514
|
702 |
/// @}
|
deba@2514
|
703 |
|
deba@2522
|
704 |
/// \name Query Functions
|
deba@2522
|
705 |
/// The result of the Dinitz-Sleator-Tarjan algorithm can be
|
deba@2522
|
706 |
/// obtained using these functions.
|
deba@2522
|
707 |
/// \n
|
deba@2514
|
708 |
/// Before the use of these functions,
|
deba@2514
|
709 |
/// either run() or start() must be called.
|
deba@2514
|
710 |
|
deba@2514
|
711 |
///@{
|
deba@2514
|
712 |
|
deba@2514
|
713 |
/// \brief Returns the value of the maximum flow.
|
deba@2514
|
714 |
///
|
deba@2514
|
715 |
/// Returns the value of the maximum flow by returning the excess
|
deba@2514
|
716 |
/// of the target node \c t. This value equals to the value of
|
deba@2514
|
717 |
/// the maximum flow already after the first phase.
|
deba@2514
|
718 |
Value flowValue() const {
|
deba@2514
|
719 |
return _flow_value;
|
deba@2514
|
720 |
}
|
deba@2514
|
721 |
|
deba@2514
|
722 |
|
deba@2514
|
723 |
/// \brief Returns the flow on the edge.
|
deba@2514
|
724 |
///
|
deba@2514
|
725 |
/// Sets the \c flowMap to the flow on the edges. This method can
|
deba@2514
|
726 |
/// be called after the second phase of algorithm.
|
deba@2514
|
727 |
Value flow(const Edge& edge) const {
|
deba@2514
|
728 |
return (*_flow)[edge];
|
deba@2514
|
729 |
}
|
deba@2514
|
730 |
|
deba@2514
|
731 |
/// \brief Returns true when the node is on the source side of minimum cut.
|
deba@2514
|
732 |
///
|
deba@2514
|
733 |
|
deba@2514
|
734 |
/// Returns true when the node is on the source side of minimum
|
deba@2514
|
735 |
/// cut. This method can be called both after running \ref
|
deba@2514
|
736 |
/// startFirstPhase() and \ref startSecondPhase().
|
deba@2514
|
737 |
bool minCut(const Node& node) const {
|
deba@2514
|
738 |
return (*_level)[node] == -2;
|
deba@2514
|
739 |
}
|
deba@2514
|
740 |
|
deba@2514
|
741 |
/// \brief Returns a minimum value cut.
|
deba@2514
|
742 |
///
|
deba@2514
|
743 |
/// Sets \c cut to the characteristic vector of a minimum value cut
|
deba@2514
|
744 |
/// It simply calls the minMinCut member.
|
deba@2514
|
745 |
/// \retval cut Write node bool map.
|
deba@2514
|
746 |
template <typename CutMap>
|
deba@2514
|
747 |
void minCutMap(CutMap& cutMap) const {
|
deba@2514
|
748 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
749 |
cutMap.set(n, (*_level)[n] == -2);
|
deba@2514
|
750 |
}
|
deba@2514
|
751 |
cutMap.set(_source, true);
|
deba@2514
|
752 |
}
|
deba@2514
|
753 |
|
deba@2514
|
754 |
/// @}
|
deba@2514
|
755 |
|
deba@2514
|
756 |
};
|
deba@2514
|
757 |
}
|
deba@2514
|
758 |
|
deba@2514
|
759 |
#endif
|