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@2527
|
216 |
protected:
|
deba@2527
|
217 |
|
deba@2527
|
218 |
DinitzSleatorTarjan() {}
|
deba@2527
|
219 |
|
deba@2527
|
220 |
public:
|
deba@2527
|
221 |
|
deba@2514
|
222 |
/// \brief The constructor of the class.
|
deba@2514
|
223 |
///
|
deba@2514
|
224 |
/// The constructor of the class.
|
deba@2514
|
225 |
/// \param graph The directed graph the algorithm runs on.
|
deba@2514
|
226 |
/// \param capacity The capacity of the edges.
|
deba@2514
|
227 |
/// \param source The source node.
|
deba@2514
|
228 |
/// \param target The target node.
|
deba@2514
|
229 |
DinitzSleatorTarjan(const Graph& graph, const CapacityMap& capacity,
|
deba@2514
|
230 |
Node source, Node target)
|
deba@2514
|
231 |
: _graph(graph), _capacity(&capacity),
|
deba@2514
|
232 |
_source(source), _target(target),
|
deba@2514
|
233 |
_flow(0), _local_flow(false),
|
deba@2514
|
234 |
_level(0), _dt_edges(0),
|
deba@2519
|
235 |
_dt_index(0), _dt(0), _queue(),
|
deba@2514
|
236 |
_tolerance(), _flow_value(), _max_value()
|
deba@2514
|
237 |
{
|
deba@2514
|
238 |
if (_source == _target) {
|
deba@2514
|
239 |
throw InvalidArgument();
|
deba@2514
|
240 |
}
|
deba@2514
|
241 |
}
|
deba@2514
|
242 |
|
deba@2514
|
243 |
/// \brief Destrcutor.
|
deba@2514
|
244 |
///
|
deba@2514
|
245 |
/// Destructor.
|
deba@2514
|
246 |
~DinitzSleatorTarjan() {
|
deba@2514
|
247 |
destroyStructures();
|
deba@2514
|
248 |
}
|
deba@2514
|
249 |
|
deba@2514
|
250 |
/// \brief Sets the capacity map.
|
deba@2514
|
251 |
///
|
deba@2514
|
252 |
/// Sets the capacity map.
|
deba@2514
|
253 |
/// \return \c (*this)
|
deba@2514
|
254 |
DinitzSleatorTarjan& capacityMap(const CapacityMap& map) {
|
deba@2514
|
255 |
_capacity = ↦
|
deba@2514
|
256 |
return *this;
|
deba@2514
|
257 |
}
|
deba@2514
|
258 |
|
deba@2514
|
259 |
/// \brief Sets the flow map.
|
deba@2514
|
260 |
///
|
deba@2514
|
261 |
/// Sets the flow map.
|
deba@2514
|
262 |
/// \return \c (*this)
|
deba@2514
|
263 |
DinitzSleatorTarjan& flowMap(FlowMap& map) {
|
deba@2514
|
264 |
if (_local_flow) {
|
deba@2514
|
265 |
delete _flow;
|
deba@2514
|
266 |
_local_flow = false;
|
deba@2514
|
267 |
}
|
deba@2514
|
268 |
_flow = ↦
|
deba@2514
|
269 |
return *this;
|
deba@2514
|
270 |
}
|
deba@2514
|
271 |
|
deba@2514
|
272 |
/// \brief Returns the flow map.
|
deba@2514
|
273 |
///
|
deba@2514
|
274 |
/// \return The flow map.
|
deba@2514
|
275 |
const FlowMap& flowMap() {
|
deba@2514
|
276 |
return *_flow;
|
deba@2514
|
277 |
}
|
deba@2514
|
278 |
|
deba@2514
|
279 |
/// \brief Sets the source node.
|
deba@2514
|
280 |
///
|
deba@2514
|
281 |
/// Sets the source node.
|
deba@2514
|
282 |
/// \return \c (*this)
|
deba@2514
|
283 |
DinitzSleatorTarjan& source(const Node& node) {
|
deba@2514
|
284 |
_source = node;
|
deba@2514
|
285 |
return *this;
|
deba@2514
|
286 |
}
|
deba@2514
|
287 |
|
deba@2514
|
288 |
/// \brief Sets the target node.
|
deba@2514
|
289 |
///
|
deba@2514
|
290 |
/// Sets the target node.
|
deba@2514
|
291 |
/// \return \c (*this)
|
deba@2514
|
292 |
DinitzSleatorTarjan& target(const Node& node) {
|
deba@2514
|
293 |
_target = node;
|
deba@2514
|
294 |
return *this;
|
deba@2514
|
295 |
}
|
deba@2514
|
296 |
|
deba@2514
|
297 |
/// \brief Sets the tolerance used by algorithm.
|
deba@2514
|
298 |
///
|
deba@2514
|
299 |
/// Sets the tolerance used by algorithm.
|
deba@2514
|
300 |
DinitzSleatorTarjan& tolerance(const Tolerance& tolerance) const {
|
deba@2514
|
301 |
_tolerance = tolerance;
|
deba@2514
|
302 |
if (_dt) {
|
deba@2514
|
303 |
_dt.tolerance(_tolerance);
|
deba@2514
|
304 |
}
|
deba@2514
|
305 |
return *this;
|
deba@2514
|
306 |
}
|
deba@2514
|
307 |
|
deba@2514
|
308 |
/// \brief Returns the tolerance used by algorithm.
|
deba@2514
|
309 |
///
|
deba@2514
|
310 |
/// Returns the tolerance used by algorithm.
|
deba@2514
|
311 |
const Tolerance& tolerance() const {
|
deba@2514
|
312 |
return tolerance;
|
deba@2514
|
313 |
}
|
deba@2514
|
314 |
|
deba@2514
|
315 |
private:
|
deba@2514
|
316 |
|
deba@2514
|
317 |
void createStructures() {
|
deba@2514
|
318 |
if (!_flow) {
|
deba@2514
|
319 |
_flow = Traits::createFlowMap(_graph);
|
deba@2514
|
320 |
_local_flow = true;
|
deba@2514
|
321 |
}
|
deba@2514
|
322 |
if (!_level) {
|
deba@2514
|
323 |
_level = new LevelMap(_graph);
|
deba@2514
|
324 |
}
|
deba@2514
|
325 |
if (!_dt_index && !_dt) {
|
deba@2514
|
326 |
_dt_index = new IntNodeMap(_graph);
|
deba@2514
|
327 |
_dt = new DynTree(*_dt_index, _tolerance);
|
deba@2514
|
328 |
}
|
deba@2514
|
329 |
if (!_dt_edges) {
|
deba@2514
|
330 |
_dt_edges = new EdgeNodeMap(_graph);
|
deba@2514
|
331 |
}
|
deba@2519
|
332 |
_queue.resize(countNodes(_graph));
|
deba@2514
|
333 |
_max_value = _dt->maxValue();
|
deba@2514
|
334 |
}
|
deba@2514
|
335 |
|
deba@2514
|
336 |
void destroyStructures() {
|
deba@2514
|
337 |
if (_local_flow) {
|
deba@2514
|
338 |
delete _flow;
|
deba@2514
|
339 |
}
|
deba@2514
|
340 |
if (_level) {
|
deba@2514
|
341 |
delete _level;
|
deba@2514
|
342 |
}
|
deba@2514
|
343 |
if (_dt) {
|
deba@2514
|
344 |
delete _dt;
|
deba@2514
|
345 |
}
|
deba@2514
|
346 |
if (_dt_index) {
|
deba@2514
|
347 |
delete _dt_index;
|
deba@2514
|
348 |
}
|
deba@2514
|
349 |
if (_dt_edges) {
|
deba@2514
|
350 |
delete _dt_edges;
|
deba@2514
|
351 |
}
|
deba@2514
|
352 |
}
|
deba@2514
|
353 |
|
deba@2514
|
354 |
bool createLayeredGraph() {
|
deba@2514
|
355 |
|
deba@2514
|
356 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
357 |
_level->set(n, -2);
|
deba@2514
|
358 |
}
|
deba@2514
|
359 |
|
deba@2514
|
360 |
int level = 0;
|
deba@2514
|
361 |
|
deba@2519
|
362 |
_queue[0] = _target;
|
deba@2514
|
363 |
_level->set(_target, level);
|
deba@2519
|
364 |
|
deba@2519
|
365 |
int first = 0, last = 1, limit = 0;
|
deba@2514
|
366 |
|
deba@2519
|
367 |
while (first != last && (*_level)[_source] == -2) {
|
deba@2519
|
368 |
if (first == limit) {
|
deba@2519
|
369 |
limit = last;
|
deba@2519
|
370 |
++level;
|
deba@2519
|
371 |
}
|
deba@2514
|
372 |
|
deba@2519
|
373 |
Node n = _queue[first++];
|
deba@2514
|
374 |
|
deba@2519
|
375 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2519
|
376 |
Node v = _graph.target(e);
|
deba@2519
|
377 |
if ((*_level)[v] != -2) continue;
|
deba@2519
|
378 |
Value rem = (*_flow)[e];
|
deba@2519
|
379 |
if (!_tolerance.positive(rem)) continue;
|
deba@2519
|
380 |
_level->set(v, level);
|
deba@2519
|
381 |
_queue[last++] = v;
|
deba@2514
|
382 |
}
|
deba@2519
|
383 |
|
deba@2519
|
384 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2519
|
385 |
Node v = _graph.source(e);
|
deba@2519
|
386 |
if ((*_level)[v] != -2) continue;
|
deba@2519
|
387 |
Value rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2519
|
388 |
if (!_tolerance.positive(rem)) continue;
|
deba@2519
|
389 |
_level->set(v, level);
|
deba@2519
|
390 |
_queue[last++] = v;
|
deba@2519
|
391 |
}
|
deba@2514
|
392 |
}
|
deba@2514
|
393 |
return (*_level)[_source] != -2;
|
deba@2514
|
394 |
}
|
deba@2514
|
395 |
|
deba@2514
|
396 |
void initEdges() {
|
deba@2514
|
397 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
398 |
_graph.firstOut((*_dt_edges)[n], n);
|
deba@2514
|
399 |
}
|
deba@2514
|
400 |
}
|
deba@2514
|
401 |
|
deba@2514
|
402 |
|
deba@2514
|
403 |
void augmentPath() {
|
deba@2514
|
404 |
Value rem;
|
deba@2514
|
405 |
Node n = _dt->findCost(_source, rem);
|
deba@2514
|
406 |
_flow_value += rem;
|
deba@2514
|
407 |
_dt->addCost(_source, - rem);
|
deba@2514
|
408 |
|
deba@2514
|
409 |
_dt->cut(n);
|
deba@2514
|
410 |
_dt->addCost(n, _max_value);
|
deba@2514
|
411 |
|
deba@2514
|
412 |
Edge e = (*_dt_edges)[n];
|
deba@2514
|
413 |
if (_graph.source(e) == n) {
|
deba@2514
|
414 |
_flow->set(e, (*_capacity)[e]);
|
deba@2514
|
415 |
|
deba@2514
|
416 |
_graph.nextOut(e);
|
deba@2514
|
417 |
if (e == INVALID) {
|
deba@2514
|
418 |
_graph.firstIn(e, n);
|
deba@2514
|
419 |
}
|
deba@2514
|
420 |
} else {
|
deba@2514
|
421 |
_flow->set(e, 0);
|
deba@2514
|
422 |
_graph.nextIn(e);
|
deba@2514
|
423 |
}
|
deba@2514
|
424 |
_dt_edges->set(n, e);
|
deba@2514
|
425 |
|
deba@2514
|
426 |
}
|
deba@2514
|
427 |
|
deba@2514
|
428 |
bool advance(Node n) {
|
deba@2514
|
429 |
Edge e = (*_dt_edges)[n];
|
deba@2514
|
430 |
if (e == INVALID) return false;
|
deba@2514
|
431 |
|
deba@2514
|
432 |
Node u;
|
deba@2514
|
433 |
Value rem;
|
deba@2514
|
434 |
if (_graph.source(e) == n) {
|
deba@2514
|
435 |
u = _graph.target(e);
|
deba@2514
|
436 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
437 |
!_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
|
deba@2514
|
438 |
_graph.nextOut(e);
|
deba@2514
|
439 |
if (e == INVALID) break;
|
deba@2514
|
440 |
u = _graph.target(e);
|
deba@2514
|
441 |
}
|
deba@2514
|
442 |
if (e != INVALID) {
|
deba@2514
|
443 |
rem = (*_capacity)[e] - (*_flow)[e];
|
deba@2514
|
444 |
} else {
|
deba@2514
|
445 |
_graph.firstIn(e, n);
|
deba@2514
|
446 |
if (e == INVALID) {
|
deba@2514
|
447 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
448 |
return false;
|
deba@2514
|
449 |
}
|
deba@2514
|
450 |
u = _graph.source(e);
|
deba@2514
|
451 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
452 |
!_tolerance.positive((*_flow)[e])) {
|
deba@2514
|
453 |
_graph.nextIn(e);
|
deba@2514
|
454 |
if (e == INVALID) {
|
deba@2514
|
455 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
456 |
return false;
|
deba@2514
|
457 |
}
|
deba@2514
|
458 |
u = _graph.source(e);
|
deba@2514
|
459 |
}
|
deba@2514
|
460 |
rem = (*_flow)[e];
|
deba@2514
|
461 |
}
|
deba@2514
|
462 |
} else {
|
deba@2514
|
463 |
u = _graph.source(e);
|
deba@2514
|
464 |
while ((*_level)[n] != (*_level)[u] + 1 ||
|
deba@2514
|
465 |
!_tolerance.positive((*_flow)[e])) {
|
deba@2514
|
466 |
_graph.nextIn(e);
|
deba@2514
|
467 |
if (e == INVALID) {
|
deba@2514
|
468 |
_dt_edges->set(n, INVALID);
|
deba@2514
|
469 |
return false;
|
deba@2514
|
470 |
}
|
deba@2514
|
471 |
u = _graph.source(e);
|
deba@2514
|
472 |
}
|
deba@2514
|
473 |
rem = (*_flow)[e];
|
deba@2514
|
474 |
}
|
deba@2514
|
475 |
|
deba@2514
|
476 |
_dt->addCost(n, - std::numeric_limits<Value>::max());
|
deba@2514
|
477 |
_dt->addCost(n, rem);
|
deba@2514
|
478 |
_dt->link(n, u);
|
deba@2514
|
479 |
_dt_edges->set(n, e);
|
deba@2514
|
480 |
return true;
|
deba@2514
|
481 |
}
|
deba@2514
|
482 |
|
deba@2514
|
483 |
void retreat(Node n) {
|
deba@2514
|
484 |
_level->set(n, -1);
|
deba@2514
|
485 |
|
deba@2514
|
486 |
for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
487 |
Node u = _graph.target(e);
|
deba@2514
|
488 |
if ((*_dt_edges)[u] == e && _dt->findRoot(u) == n) {
|
deba@2514
|
489 |
Value rem;
|
deba@2514
|
490 |
_dt->findCost(u, rem);
|
deba@2514
|
491 |
_flow->set(e, rem);
|
deba@2514
|
492 |
_dt->cut(u);
|
deba@2514
|
493 |
_dt->addCost(u, - rem);
|
deba@2514
|
494 |
_dt->addCost(u, _max_value);
|
deba@2514
|
495 |
}
|
deba@2514
|
496 |
}
|
deba@2514
|
497 |
for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
|
deba@2514
|
498 |
Node u = _graph.source(e);
|
deba@2514
|
499 |
if ((*_dt_edges)[u] == e && _dt->findRoot(u) == n) {
|
deba@2514
|
500 |
Value rem;
|
deba@2514
|
501 |
_dt->findCost(u, rem);
|
deba@2514
|
502 |
_flow->set(e, (*_capacity)[e] - rem);
|
deba@2514
|
503 |
_dt->cut(u);
|
deba@2514
|
504 |
_dt->addCost(u, - rem);
|
deba@2514
|
505 |
_dt->addCost(u, _max_value);
|
deba@2514
|
506 |
}
|
deba@2514
|
507 |
}
|
deba@2514
|
508 |
}
|
deba@2514
|
509 |
|
deba@2514
|
510 |
void extractTrees() {
|
deba@2514
|
511 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
512 |
|
deba@2514
|
513 |
Node w = _dt->findRoot(n);
|
deba@2514
|
514 |
|
deba@2514
|
515 |
while (w != n) {
|
deba@2514
|
516 |
|
deba@2514
|
517 |
Value rem;
|
deba@2514
|
518 |
Node u = _dt->findCost(n, rem);
|
deba@2514
|
519 |
|
deba@2514
|
520 |
_dt->cut(u);
|
deba@2514
|
521 |
_dt->addCost(u, - rem);
|
deba@2514
|
522 |
_dt->addCost(u, _max_value);
|
deba@2514
|
523 |
|
deba@2514
|
524 |
Edge e = (*_dt_edges)[u];
|
deba@2514
|
525 |
_dt_edges->set(u, INVALID);
|
deba@2514
|
526 |
|
deba@2514
|
527 |
if (u == _graph.source(e)) {
|
deba@2514
|
528 |
_flow->set(e, (*_capacity)[e] - rem);
|
deba@2514
|
529 |
} else {
|
deba@2514
|
530 |
_flow->set(e, rem);
|
deba@2514
|
531 |
}
|
deba@2514
|
532 |
|
deba@2514
|
533 |
w = _dt->findRoot(n);
|
deba@2514
|
534 |
}
|
deba@2514
|
535 |
}
|
deba@2514
|
536 |
}
|
deba@2514
|
537 |
|
deba@2514
|
538 |
|
deba@2514
|
539 |
public:
|
deba@2514
|
540 |
|
deba@2514
|
541 |
/// \name Execution control The simplest way to execute the
|
deba@2514
|
542 |
/// algorithm is to use the \c run() member functions.
|
deba@2514
|
543 |
/// \n
|
deba@2514
|
544 |
/// If you need more control on initial solution or
|
deba@2514
|
545 |
/// execution then you have to call one \ref init() function and then
|
deba@2514
|
546 |
/// the start() or multiple times the \c augment() member function.
|
deba@2514
|
547 |
|
deba@2514
|
548 |
///@{
|
deba@2514
|
549 |
|
deba@2514
|
550 |
/// \brief Initializes the algorithm
|
deba@2514
|
551 |
///
|
deba@2514
|
552 |
/// It sets the flow to empty flow.
|
deba@2514
|
553 |
void init() {
|
deba@2514
|
554 |
createStructures();
|
deba@2514
|
555 |
|
deba@2514
|
556 |
_dt->clear();
|
deba@2514
|
557 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
558 |
_dt->makeTree(n);
|
deba@2514
|
559 |
_dt->addCost(n, _max_value);
|
deba@2514
|
560 |
}
|
deba@2514
|
561 |
|
deba@2514
|
562 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
563 |
_flow->set(it, 0);
|
deba@2514
|
564 |
}
|
deba@2514
|
565 |
_flow_value = 0;
|
deba@2514
|
566 |
}
|
deba@2514
|
567 |
|
deba@2514
|
568 |
/// \brief Initializes the algorithm
|
deba@2514
|
569 |
///
|
deba@2514
|
570 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
571 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
572 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
573 |
/// outgoing flow.
|
deba@2514
|
574 |
template <typename FlowMap>
|
deba@2514
|
575 |
void flowInit(const FlowMap& flowMap) {
|
deba@2514
|
576 |
createStructures();
|
deba@2514
|
577 |
|
deba@2514
|
578 |
_dt->clear();
|
deba@2514
|
579 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
580 |
_dt->makeTree(n);
|
deba@2514
|
581 |
_dt->addCost(n, _max_value);
|
deba@2514
|
582 |
}
|
deba@2514
|
583 |
|
deba@2514
|
584 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
585 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
586 |
}
|
deba@2514
|
587 |
_flow_value = 0;
|
deba@2514
|
588 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
589 |
_flow_value += (*_flow)[jt];
|
deba@2514
|
590 |
}
|
deba@2514
|
591 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
592 |
_flow_value -= (*_flow)[jt];
|
deba@2514
|
593 |
}
|
deba@2514
|
594 |
}
|
deba@2514
|
595 |
|
deba@2514
|
596 |
/// \brief Initializes the algorithm
|
deba@2514
|
597 |
///
|
deba@2514
|
598 |
/// Initializes the flow to the \c flowMap. The \c flowMap should
|
deba@2514
|
599 |
/// contain a feasible flow, ie. in each node excluding the source
|
deba@2514
|
600 |
/// and the target the incoming flow should be equal to the
|
deba@2514
|
601 |
/// outgoing flow.
|
deba@2514
|
602 |
/// \return %False when the given flowMap does not contain
|
deba@2514
|
603 |
/// feasible flow.
|
deba@2514
|
604 |
template <typename FlowMap>
|
deba@2514
|
605 |
bool checkedFlowInit(const FlowMap& flowMap) {
|
deba@2514
|
606 |
createStructures();
|
deba@2514
|
607 |
|
deba@2514
|
608 |
_dt->clear();
|
deba@2514
|
609 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
610 |
_dt->makeTree(n);
|
deba@2514
|
611 |
_dt->addCost(n, _max_value);
|
deba@2514
|
612 |
}
|
deba@2514
|
613 |
|
deba@2514
|
614 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@2514
|
615 |
_flow->set(e, flowMap[e]);
|
deba@2514
|
616 |
}
|
deba@2514
|
617 |
for (NodeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
618 |
if (it == _source || it == _target) continue;
|
deba@2514
|
619 |
Value outFlow = 0;
|
deba@2514
|
620 |
for (OutEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
621 |
outFlow += (*_flow)[jt];
|
deba@2514
|
622 |
}
|
deba@2514
|
623 |
Value inFlow = 0;
|
deba@2514
|
624 |
for (InEdgeIt jt(_graph, it); jt != INVALID; ++jt) {
|
deba@2514
|
625 |
inFlow += (*_flow)[jt];
|
deba@2514
|
626 |
}
|
deba@2514
|
627 |
if (_tolerance.different(outFlow, inFlow)) {
|
deba@2514
|
628 |
return false;
|
deba@2514
|
629 |
}
|
deba@2514
|
630 |
}
|
deba@2514
|
631 |
for (EdgeIt it(_graph); it != INVALID; ++it) {
|
deba@2514
|
632 |
if (_tolerance.less((*_flow)[it], 0)) return false;
|
deba@2514
|
633 |
if (_tolerance.less((*_capacity)[it], (*_flow)[it])) return false;
|
deba@2514
|
634 |
}
|
deba@2514
|
635 |
_flow_value = 0;
|
deba@2514
|
636 |
for (OutEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
637 |
_flow_value += (*_flow)[jt];
|
deba@2514
|
638 |
}
|
deba@2514
|
639 |
for (InEdgeIt jt(_graph, _source); jt != INVALID; ++jt) {
|
deba@2514
|
640 |
_flow_value -= (*_flow)[jt];
|
deba@2514
|
641 |
}
|
deba@2514
|
642 |
return true;
|
deba@2514
|
643 |
}
|
deba@2514
|
644 |
|
deba@2514
|
645 |
/// \brief Executes the algorithm
|
deba@2514
|
646 |
///
|
deba@2514
|
647 |
/// It runs augmenting phases by adding blocking flow until the
|
deba@2514
|
648 |
/// optimal solution is reached.
|
deba@2514
|
649 |
void start() {
|
deba@2514
|
650 |
while (augment());
|
deba@2514
|
651 |
}
|
deba@2514
|
652 |
|
deba@2514
|
653 |
/// \brief Augments the flow with a blocking flow on a layered
|
deba@2514
|
654 |
/// graph.
|
deba@2514
|
655 |
///
|
deba@2514
|
656 |
/// This function builds a layered graph and then find a blocking
|
deba@2514
|
657 |
/// flow on this graph. The number of the levels in the layered
|
deba@2514
|
658 |
/// graph is strictly increasing in each augmenting phase
|
deba@2514
|
659 |
/// therefore the number of the augmentings is at most \f$ n-1
|
deba@2514
|
660 |
/// \f$. The length of each phase is at most \f$ O(m \log(n))
|
deba@2514
|
661 |
/// \f$, that the overall time complexity is \f$ O(nm \log(n)) \f$.
|
deba@2514
|
662 |
/// \return %False when there is not residual path between the
|
deba@2514
|
663 |
/// source and the target so the current flow is a feasible and
|
deba@2514
|
664 |
/// optimal solution.
|
deba@2514
|
665 |
bool augment() {
|
deba@2514
|
666 |
Node n;
|
deba@2514
|
667 |
|
deba@2514
|
668 |
if (createLayeredGraph()) {
|
deba@2514
|
669 |
|
deba@2514
|
670 |
Timer bf_timer;
|
deba@2514
|
671 |
initEdges();
|
deba@2514
|
672 |
|
deba@2514
|
673 |
n = _dt->findRoot(_source);
|
deba@2514
|
674 |
while (true) {
|
deba@2514
|
675 |
Edge e;
|
deba@2514
|
676 |
if (n == _target) {
|
deba@2514
|
677 |
augmentPath();
|
deba@2514
|
678 |
} else if (!advance(n)) {
|
deba@2514
|
679 |
if (n != _source) {
|
deba@2514
|
680 |
retreat(n);
|
deba@2514
|
681 |
} else {
|
deba@2514
|
682 |
break;
|
deba@2514
|
683 |
}
|
deba@2514
|
684 |
}
|
deba@2514
|
685 |
n = _dt->findRoot(_source);
|
deba@2514
|
686 |
}
|
deba@2514
|
687 |
extractTrees();
|
deba@2514
|
688 |
|
deba@2514
|
689 |
return true;
|
deba@2514
|
690 |
} else {
|
deba@2514
|
691 |
return false;
|
deba@2514
|
692 |
}
|
deba@2514
|
693 |
}
|
deba@2514
|
694 |
|
deba@2514
|
695 |
/// \brief runs the algorithm.
|
deba@2514
|
696 |
///
|
deba@2514
|
697 |
/// It is just a shorthand for:
|
deba@2514
|
698 |
///
|
deba@2514
|
699 |
///\code
|
deba@2514
|
700 |
/// ek.init();
|
deba@2514
|
701 |
/// ek.start();
|
deba@2514
|
702 |
///\endcode
|
deba@2514
|
703 |
void run() {
|
deba@2514
|
704 |
init();
|
deba@2514
|
705 |
start();
|
deba@2514
|
706 |
}
|
deba@2514
|
707 |
|
deba@2514
|
708 |
/// @}
|
deba@2514
|
709 |
|
deba@2522
|
710 |
/// \name Query Functions
|
deba@2522
|
711 |
/// The result of the Dinitz-Sleator-Tarjan algorithm can be
|
deba@2522
|
712 |
/// obtained using these functions.
|
deba@2522
|
713 |
/// \n
|
deba@2514
|
714 |
/// Before the use of these functions,
|
deba@2514
|
715 |
/// either run() or start() must be called.
|
deba@2514
|
716 |
|
deba@2514
|
717 |
///@{
|
deba@2514
|
718 |
|
deba@2514
|
719 |
/// \brief Returns the value of the maximum flow.
|
deba@2514
|
720 |
///
|
deba@2514
|
721 |
/// Returns the value of the maximum flow by returning the excess
|
deba@2514
|
722 |
/// of the target node \c t. This value equals to the value of
|
deba@2514
|
723 |
/// the maximum flow already after the first phase.
|
deba@2514
|
724 |
Value flowValue() const {
|
deba@2514
|
725 |
return _flow_value;
|
deba@2514
|
726 |
}
|
deba@2514
|
727 |
|
deba@2514
|
728 |
|
deba@2514
|
729 |
/// \brief Returns the flow on the edge.
|
deba@2514
|
730 |
///
|
deba@2514
|
731 |
/// Sets the \c flowMap to the flow on the edges. This method can
|
deba@2514
|
732 |
/// be called after the second phase of algorithm.
|
deba@2514
|
733 |
Value flow(const Edge& edge) const {
|
deba@2514
|
734 |
return (*_flow)[edge];
|
deba@2514
|
735 |
}
|
deba@2514
|
736 |
|
deba@2514
|
737 |
/// \brief Returns true when the node is on the source side of minimum cut.
|
deba@2514
|
738 |
///
|
deba@2514
|
739 |
|
deba@2514
|
740 |
/// Returns true when the node is on the source side of minimum
|
deba@2514
|
741 |
/// cut. This method can be called both after running \ref
|
deba@2514
|
742 |
/// startFirstPhase() and \ref startSecondPhase().
|
deba@2514
|
743 |
bool minCut(const Node& node) const {
|
deba@2514
|
744 |
return (*_level)[node] == -2;
|
deba@2514
|
745 |
}
|
deba@2514
|
746 |
|
deba@2514
|
747 |
/// \brief Returns a minimum value cut.
|
deba@2514
|
748 |
///
|
deba@2514
|
749 |
/// Sets \c cut to the characteristic vector of a minimum value cut
|
deba@2514
|
750 |
/// It simply calls the minMinCut member.
|
deba@2514
|
751 |
/// \retval cut Write node bool map.
|
deba@2514
|
752 |
template <typename CutMap>
|
deba@2514
|
753 |
void minCutMap(CutMap& cutMap) const {
|
deba@2514
|
754 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@2514
|
755 |
cutMap.set(n, (*_level)[n] == -2);
|
deba@2514
|
756 |
}
|
deba@2514
|
757 |
cutMap.set(_source, true);
|
deba@2514
|
758 |
}
|
deba@2514
|
759 |
|
deba@2514
|
760 |
/// @}
|
deba@2514
|
761 |
|
deba@2514
|
762 |
};
|
deba@2514
|
763 |
}
|
deba@2514
|
764 |
|
deba@2514
|
765 |
#endif
|