alpar@956
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
tapolcai@590
|
2 |
*
|
alpar@956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
tapolcai@590
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
tapolcai@590
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
tapolcai@590
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
tapolcai@590
|
8 |
*
|
tapolcai@590
|
9 |
* Permission to use, modify and distribute this software is granted
|
tapolcai@590
|
10 |
* provided that this copyright notice appears in all copies. For
|
tapolcai@590
|
11 |
* precise terms see the accompanying LICENSE file.
|
tapolcai@590
|
12 |
*
|
tapolcai@590
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
tapolcai@590
|
14 |
* express or implied, and with no claim as to its suitability for any
|
tapolcai@590
|
15 |
* purpose.
|
tapolcai@590
|
16 |
*
|
tapolcai@590
|
17 |
*/
|
tapolcai@590
|
18 |
|
tapolcai@590
|
19 |
#ifndef LEMON_GOMORY_HU_TREE_H
|
tapolcai@590
|
20 |
#define LEMON_GOMORY_HU_TREE_H
|
tapolcai@590
|
21 |
|
tapolcai@590
|
22 |
#include <limits>
|
tapolcai@590
|
23 |
|
alpar@591
|
24 |
#include <lemon/core.h>
|
tapolcai@590
|
25 |
#include <lemon/preflow.h>
|
tapolcai@590
|
26 |
#include <lemon/concept_check.h>
|
tapolcai@590
|
27 |
#include <lemon/concepts/maps.h>
|
tapolcai@590
|
28 |
|
tapolcai@590
|
29 |
/// \ingroup min_cut
|
alpar@956
|
30 |
/// \file
|
tapolcai@590
|
31 |
/// \brief Gomory-Hu cut tree in graphs.
|
tapolcai@590
|
32 |
|
tapolcai@590
|
33 |
namespace lemon {
|
tapolcai@590
|
34 |
|
tapolcai@590
|
35 |
/// \ingroup min_cut
|
tapolcai@590
|
36 |
///
|
tapolcai@590
|
37 |
/// \brief Gomory-Hu cut tree algorithm
|
tapolcai@590
|
38 |
///
|
kpeter@593
|
39 |
/// The Gomory-Hu tree is a tree on the node set of a given graph, but it
|
kpeter@593
|
40 |
/// may contain edges which are not in the original graph. It has the
|
alpar@956
|
41 |
/// property that the minimum capacity edge of the path between two nodes
|
kpeter@593
|
42 |
/// in this tree has the same weight as the minimum cut in the graph
|
alpar@591
|
43 |
/// between these nodes. Moreover the components obtained by removing
|
alpar@591
|
44 |
/// this edge from the tree determine the corresponding minimum cut.
|
alpar@591
|
45 |
/// Therefore once this tree is computed, the minimum cut between any pair
|
alpar@591
|
46 |
/// of nodes can easily be obtained.
|
alpar@956
|
47 |
///
|
alpar@591
|
48 |
/// The algorithm calculates \e n-1 distinct minimum cuts (currently with
|
kpeter@643
|
49 |
/// the \ref Preflow algorithm), thus it has \f$O(n^3\sqrt{e})\f$ overall
|
kpeter@643
|
50 |
/// time complexity. It calculates a rooted Gomory-Hu tree.
|
kpeter@643
|
51 |
/// The structure of the tree and the edge weights can be
|
kpeter@643
|
52 |
/// obtained using \c predNode(), \c predValue() and \c rootDist().
|
kpeter@643
|
53 |
/// The functions \c minCutMap() and \c minCutValue() calculate
|
kpeter@593
|
54 |
/// the minimum cut and the minimum cut value between any two nodes
|
kpeter@593
|
55 |
/// in the graph. You can also list (iterate on) the nodes and the
|
kpeter@593
|
56 |
/// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
|
alpar@591
|
57 |
///
|
kpeter@593
|
58 |
/// \tparam GR The type of the undirected graph the algorithm runs on.
|
kpeter@643
|
59 |
/// \tparam CAP The type of the edge map containing the capacities.
|
kpeter@643
|
60 |
/// The default map type is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
|
kpeter@593
|
61 |
#ifdef DOXYGEN
|
alpar@591
|
62 |
template <typename GR,
|
alpar@956
|
63 |
typename CAP>
|
kpeter@593
|
64 |
#else
|
kpeter@593
|
65 |
template <typename GR,
|
alpar@956
|
66 |
typename CAP = typename GR::template EdgeMap<int> >
|
kpeter@593
|
67 |
#endif
|
alpar@592
|
68 |
class GomoryHu {
|
tapolcai@590
|
69 |
public:
|
tapolcai@590
|
70 |
|
kpeter@643
|
71 |
/// The graph type of the algorithm
|
alpar@591
|
72 |
typedef GR Graph;
|
kpeter@643
|
73 |
/// The capacity map type of the algorithm
|
alpar@591
|
74 |
typedef CAP Capacity;
|
tapolcai@590
|
75 |
/// The value type of capacities
|
tapolcai@590
|
76 |
typedef typename Capacity::Value Value;
|
alpar@956
|
77 |
|
tapolcai@590
|
78 |
private:
|
tapolcai@590
|
79 |
|
tapolcai@590
|
80 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
tapolcai@590
|
81 |
|
tapolcai@590
|
82 |
const Graph& _graph;
|
tapolcai@590
|
83 |
const Capacity& _capacity;
|
tapolcai@590
|
84 |
|
tapolcai@590
|
85 |
Node _root;
|
tapolcai@590
|
86 |
typename Graph::template NodeMap<Node>* _pred;
|
tapolcai@590
|
87 |
typename Graph::template NodeMap<Value>* _weight;
|
tapolcai@590
|
88 |
typename Graph::template NodeMap<int>* _order;
|
tapolcai@590
|
89 |
|
tapolcai@590
|
90 |
void createStructures() {
|
tapolcai@590
|
91 |
if (!_pred) {
|
alpar@956
|
92 |
_pred = new typename Graph::template NodeMap<Node>(_graph);
|
tapolcai@590
|
93 |
}
|
tapolcai@590
|
94 |
if (!_weight) {
|
alpar@956
|
95 |
_weight = new typename Graph::template NodeMap<Value>(_graph);
|
tapolcai@590
|
96 |
}
|
tapolcai@590
|
97 |
if (!_order) {
|
alpar@956
|
98 |
_order = new typename Graph::template NodeMap<int>(_graph);
|
tapolcai@590
|
99 |
}
|
tapolcai@590
|
100 |
}
|
tapolcai@590
|
101 |
|
tapolcai@590
|
102 |
void destroyStructures() {
|
tapolcai@590
|
103 |
if (_pred) {
|
alpar@956
|
104 |
delete _pred;
|
tapolcai@590
|
105 |
}
|
tapolcai@590
|
106 |
if (_weight) {
|
alpar@956
|
107 |
delete _weight;
|
tapolcai@590
|
108 |
}
|
tapolcai@590
|
109 |
if (_order) {
|
alpar@956
|
110 |
delete _order;
|
tapolcai@590
|
111 |
}
|
tapolcai@590
|
112 |
}
|
alpar@956
|
113 |
|
tapolcai@590
|
114 |
public:
|
tapolcai@590
|
115 |
|
tapolcai@590
|
116 |
/// \brief Constructor
|
tapolcai@590
|
117 |
///
|
kpeter@643
|
118 |
/// Constructor.
|
kpeter@593
|
119 |
/// \param graph The undirected graph the algorithm runs on.
|
kpeter@593
|
120 |
/// \param capacity The edge capacity map.
|
alpar@956
|
121 |
GomoryHu(const Graph& graph, const Capacity& capacity)
|
tapolcai@590
|
122 |
: _graph(graph), _capacity(capacity),
|
alpar@956
|
123 |
_pred(0), _weight(0), _order(0)
|
tapolcai@590
|
124 |
{
|
tapolcai@590
|
125 |
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
|
tapolcai@590
|
126 |
}
|
tapolcai@590
|
127 |
|
tapolcai@590
|
128 |
|
tapolcai@590
|
129 |
/// \brief Destructor
|
tapolcai@590
|
130 |
///
|
kpeter@643
|
131 |
/// Destructor.
|
alpar@592
|
132 |
~GomoryHu() {
|
tapolcai@590
|
133 |
destroyStructures();
|
tapolcai@590
|
134 |
}
|
tapolcai@590
|
135 |
|
kpeter@593
|
136 |
private:
|
alpar@956
|
137 |
|
kpeter@593
|
138 |
// Initialize the internal data structures
|
tapolcai@590
|
139 |
void init() {
|
tapolcai@590
|
140 |
createStructures();
|
tapolcai@590
|
141 |
|
tapolcai@590
|
142 |
_root = NodeIt(_graph);
|
tapolcai@590
|
143 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@628
|
144 |
(*_pred)[n] = _root;
|
kpeter@628
|
145 |
(*_order)[n] = -1;
|
tapolcai@590
|
146 |
}
|
kpeter@628
|
147 |
(*_pred)[_root] = INVALID;
|
alpar@956
|
148 |
(*_weight)[_root] = std::numeric_limits<Value>::max();
|
tapolcai@590
|
149 |
}
|
tapolcai@590
|
150 |
|
tapolcai@590
|
151 |
|
kpeter@593
|
152 |
// Start the algorithm
|
tapolcai@590
|
153 |
void start() {
|
tapolcai@590
|
154 |
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
|
tapolcai@590
|
155 |
|
tapolcai@590
|
156 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@956
|
157 |
if (n == _root) continue;
|
tapolcai@590
|
158 |
|
alpar@956
|
159 |
Node pn = (*_pred)[n];
|
alpar@956
|
160 |
fa.source(n);
|
alpar@956
|
161 |
fa.target(pn);
|
tapolcai@590
|
162 |
|
alpar@956
|
163 |
fa.runMinCut();
|
tapolcai@590
|
164 |
|
alpar@956
|
165 |
(*_weight)[n] = fa.flowValue();
|
tapolcai@590
|
166 |
|
alpar@956
|
167 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
|
alpar@956
|
168 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
|
alpar@956
|
169 |
(*_pred)[nn] = n;
|
alpar@956
|
170 |
}
|
alpar@956
|
171 |
}
|
alpar@956
|
172 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
|
alpar@956
|
173 |
(*_pred)[n] = (*_pred)[pn];
|
alpar@956
|
174 |
(*_pred)[pn] = n;
|
alpar@956
|
175 |
(*_weight)[n] = (*_weight)[pn];
|
alpar@956
|
176 |
(*_weight)[pn] = fa.flowValue();
|
alpar@956
|
177 |
}
|
tapolcai@590
|
178 |
}
|
tapolcai@590
|
179 |
|
kpeter@628
|
180 |
(*_order)[_root] = 0;
|
tapolcai@590
|
181 |
int index = 1;
|
tapolcai@590
|
182 |
|
tapolcai@590
|
183 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@956
|
184 |
std::vector<Node> st;
|
alpar@956
|
185 |
Node nn = n;
|
alpar@956
|
186 |
while ((*_order)[nn] == -1) {
|
alpar@956
|
187 |
st.push_back(nn);
|
alpar@956
|
188 |
nn = (*_pred)[nn];
|
alpar@956
|
189 |
}
|
alpar@956
|
190 |
while (!st.empty()) {
|
alpar@956
|
191 |
(*_order)[st.back()] = index++;
|
alpar@956
|
192 |
st.pop_back();
|
alpar@956
|
193 |
}
|
tapolcai@590
|
194 |
}
|
tapolcai@590
|
195 |
}
|
tapolcai@590
|
196 |
|
kpeter@593
|
197 |
public:
|
kpeter@593
|
198 |
|
alpar@591
|
199 |
///\name Execution Control
|
alpar@956
|
200 |
|
alpar@591
|
201 |
///@{
|
alpar@591
|
202 |
|
alpar@591
|
203 |
/// \brief Run the Gomory-Hu algorithm.
|
tapolcai@590
|
204 |
///
|
alpar@591
|
205 |
/// This function runs the Gomory-Hu algorithm.
|
tapolcai@590
|
206 |
void run() {
|
tapolcai@590
|
207 |
init();
|
tapolcai@590
|
208 |
start();
|
tapolcai@590
|
209 |
}
|
alpar@956
|
210 |
|
alpar@591
|
211 |
/// @}
|
tapolcai@590
|
212 |
|
alpar@591
|
213 |
///\name Query Functions
|
alpar@591
|
214 |
///The results of the algorithm can be obtained using these
|
alpar@591
|
215 |
///functions.\n
|
kpeter@643
|
216 |
///\ref run() should be called before using them.\n
|
kpeter@593
|
217 |
///See also \ref MinCutNodeIt and \ref MinCutEdgeIt.
|
alpar@591
|
218 |
|
alpar@591
|
219 |
///@{
|
alpar@591
|
220 |
|
alpar@591
|
221 |
/// \brief Return the predecessor node in the Gomory-Hu tree.
|
tapolcai@590
|
222 |
///
|
kpeter@643
|
223 |
/// This function returns the predecessor node of the given node
|
kpeter@643
|
224 |
/// in the Gomory-Hu tree.
|
kpeter@643
|
225 |
/// If \c node is the root of the tree, then it returns \c INVALID.
|
kpeter@643
|
226 |
///
|
kpeter@643
|
227 |
/// \pre \ref run() must be called before using this function.
|
kpeter@643
|
228 |
Node predNode(const Node& node) const {
|
tapolcai@590
|
229 |
return (*_pred)[node];
|
tapolcai@590
|
230 |
}
|
tapolcai@590
|
231 |
|
alpar@591
|
232 |
/// \brief Return the weight of the predecessor edge in the
|
tapolcai@590
|
233 |
/// Gomory-Hu tree.
|
tapolcai@590
|
234 |
///
|
alpar@956
|
235 |
/// This function returns the weight of the predecessor edge of the
|
kpeter@643
|
236 |
/// given node in the Gomory-Hu tree.
|
kpeter@643
|
237 |
/// If \c node is the root of the tree, the result is undefined.
|
kpeter@643
|
238 |
///
|
kpeter@643
|
239 |
/// \pre \ref run() must be called before using this function.
|
kpeter@643
|
240 |
Value predValue(const Node& node) const {
|
tapolcai@590
|
241 |
return (*_weight)[node];
|
tapolcai@590
|
242 |
}
|
tapolcai@590
|
243 |
|
kpeter@643
|
244 |
/// \brief Return the distance from the root node in the Gomory-Hu tree.
|
kpeter@643
|
245 |
///
|
kpeter@643
|
246 |
/// This function returns the distance of the given node from the root
|
kpeter@643
|
247 |
/// node in the Gomory-Hu tree.
|
kpeter@643
|
248 |
///
|
kpeter@643
|
249 |
/// \pre \ref run() must be called before using this function.
|
kpeter@643
|
250 |
int rootDist(const Node& node) const {
|
kpeter@643
|
251 |
return (*_order)[node];
|
kpeter@643
|
252 |
}
|
kpeter@643
|
253 |
|
alpar@591
|
254 |
/// \brief Return the minimum cut value between two nodes
|
tapolcai@590
|
255 |
///
|
kpeter@643
|
256 |
/// This function returns the minimum cut value between the nodes
|
alpar@956
|
257 |
/// \c s and \c t.
|
kpeter@643
|
258 |
/// It finds the nearest common ancestor of the given nodes in the
|
kpeter@643
|
259 |
/// Gomory-Hu tree and calculates the minimum weight edge on the
|
kpeter@643
|
260 |
/// paths to the ancestor.
|
kpeter@643
|
261 |
///
|
kpeter@643
|
262 |
/// \pre \ref run() must be called before using this function.
|
tapolcai@590
|
263 |
Value minCutValue(const Node& s, const Node& t) const {
|
tapolcai@590
|
264 |
Node sn = s, tn = t;
|
tapolcai@590
|
265 |
Value value = std::numeric_limits<Value>::max();
|
alpar@956
|
266 |
|
tapolcai@590
|
267 |
while (sn != tn) {
|
alpar@956
|
268 |
if ((*_order)[sn] < (*_order)[tn]) {
|
alpar@956
|
269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn];
|
alpar@956
|
270 |
tn = (*_pred)[tn];
|
alpar@956
|
271 |
} else {
|
alpar@956
|
272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn];
|
alpar@956
|
273 |
sn = (*_pred)[sn];
|
alpar@956
|
274 |
}
|
tapolcai@590
|
275 |
}
|
tapolcai@590
|
276 |
return value;
|
tapolcai@590
|
277 |
}
|
tapolcai@590
|
278 |
|
alpar@591
|
279 |
/// \brief Return the minimum cut between two nodes
|
tapolcai@590
|
280 |
///
|
alpar@591
|
281 |
/// This function returns the minimum cut between the nodes \c s and \c t
|
kpeter@593
|
282 |
/// in the \c cutMap parameter by setting the nodes in the component of
|
kpeter@593
|
283 |
/// \c s to \c true and the other nodes to \c false.
|
alpar@591
|
284 |
///
|
kpeter@643
|
285 |
/// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt.
|
kpeter@643
|
286 |
///
|
kpeter@643
|
287 |
/// \param s The base node.
|
kpeter@643
|
288 |
/// \param t The node you want to separate from node \c s.
|
kpeter@643
|
289 |
/// \param cutMap The cut will be returned in this map.
|
kpeter@643
|
290 |
/// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap
|
kpeter@643
|
291 |
/// "ReadWriteMap" on the graph nodes.
|
kpeter@643
|
292 |
///
|
kpeter@643
|
293 |
/// \return The value of the minimum cut between \c s and \c t.
|
kpeter@643
|
294 |
///
|
kpeter@643
|
295 |
/// \pre \ref run() must be called before using this function.
|
tapolcai@590
|
296 |
template <typename CutMap>
|
kpeter@833
|
297 |
Value minCutMap(const Node& s,
|
alpar@591
|
298 |
const Node& t,
|
alpar@591
|
299 |
CutMap& cutMap
|
alpar@591
|
300 |
) const {
|
tapolcai@590
|
301 |
Node sn = s, tn = t;
|
alpar@591
|
302 |
bool s_root=false;
|
tapolcai@590
|
303 |
Node rn = INVALID;
|
tapolcai@590
|
304 |
Value value = std::numeric_limits<Value>::max();
|
alpar@956
|
305 |
|
tapolcai@590
|
306 |
while (sn != tn) {
|
alpar@956
|
307 |
if ((*_order)[sn] < (*_order)[tn]) {
|
alpar@956
|
308 |
if ((*_weight)[tn] <= value) {
|
alpar@956
|
309 |
rn = tn;
|
alpar@591
|
310 |
s_root = false;
|
alpar@956
|
311 |
value = (*_weight)[tn];
|
alpar@956
|
312 |
}
|
alpar@956
|
313 |
tn = (*_pred)[tn];
|
alpar@956
|
314 |
} else {
|
alpar@956
|
315 |
if ((*_weight)[sn] <= value) {
|
alpar@956
|
316 |
rn = sn;
|
alpar@591
|
317 |
s_root = true;
|
alpar@956
|
318 |
value = (*_weight)[sn];
|
alpar@956
|
319 |
}
|
alpar@956
|
320 |
sn = (*_pred)[sn];
|
alpar@956
|
321 |
}
|
tapolcai@590
|
322 |
}
|
tapolcai@590
|
323 |
|
tapolcai@590
|
324 |
typename Graph::template NodeMap<bool> reached(_graph, false);
|
kpeter@628
|
325 |
reached[_root] = true;
|
alpar@591
|
326 |
cutMap.set(_root, !s_root);
|
kpeter@628
|
327 |
reached[rn] = true;
|
alpar@591
|
328 |
cutMap.set(rn, s_root);
|
tapolcai@590
|
329 |
|
alpar@591
|
330 |
std::vector<Node> st;
|
tapolcai@590
|
331 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@956
|
332 |
st.clear();
|
alpar@591
|
333 |
Node nn = n;
|
alpar@956
|
334 |
while (!reached[nn]) {
|
alpar@956
|
335 |
st.push_back(nn);
|
alpar@956
|
336 |
nn = (*_pred)[nn];
|
alpar@956
|
337 |
}
|
alpar@956
|
338 |
while (!st.empty()) {
|
alpar@956
|
339 |
cutMap.set(st.back(), cutMap[nn]);
|
alpar@956
|
340 |
st.pop_back();
|
alpar@956
|
341 |
}
|
tapolcai@590
|
342 |
}
|
alpar@956
|
343 |
|
tapolcai@590
|
344 |
return value;
|
tapolcai@590
|
345 |
}
|
tapolcai@590
|
346 |
|
alpar@591
|
347 |
///@}
|
alpar@591
|
348 |
|
alpar@591
|
349 |
friend class MinCutNodeIt;
|
alpar@591
|
350 |
|
alpar@591
|
351 |
/// Iterate on the nodes of a minimum cut
|
alpar@956
|
352 |
|
alpar@591
|
353 |
/// This iterator class lists the nodes of a minimum cut found by
|
kpeter@643
|
354 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class
|
alpar@592
|
355 |
/// and call its \ref GomoryHu::run() "run()" method.
|
alpar@591
|
356 |
///
|
alpar@591
|
357 |
/// This example counts the nodes in the minimum cut separating \c s from
|
alpar@591
|
358 |
/// \c t.
|
alpar@591
|
359 |
/// \code
|
kpeter@760
|
360 |
/// GomoryHu<Graph> gom(g, capacities);
|
alpar@591
|
361 |
/// gom.run();
|
kpeter@593
|
362 |
/// int cnt=0;
|
kpeter@760
|
363 |
/// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
|
alpar@591
|
364 |
/// \endcode
|
alpar@591
|
365 |
class MinCutNodeIt
|
alpar@591
|
366 |
{
|
alpar@591
|
367 |
bool _side;
|
alpar@591
|
368 |
typename Graph::NodeIt _node_it;
|
alpar@591
|
369 |
typename Graph::template NodeMap<bool> _cut;
|
alpar@591
|
370 |
public:
|
alpar@591
|
371 |
/// Constructor
|
alpar@591
|
372 |
|
kpeter@593
|
373 |
/// Constructor.
|
alpar@591
|
374 |
///
|
alpar@592
|
375 |
MinCutNodeIt(GomoryHu const &gomory,
|
alpar@592
|
376 |
///< The GomoryHu class. You must call its
|
alpar@591
|
377 |
/// run() method
|
kpeter@593
|
378 |
/// before initializing this iterator.
|
kpeter@593
|
379 |
const Node& s, ///< The base node.
|
alpar@591
|
380 |
const Node& t,
|
kpeter@593
|
381 |
///< The node you want to separate from node \c s.
|
alpar@591
|
382 |
bool side=true
|
alpar@591
|
383 |
///< If it is \c true (default) then the iterator lists
|
alpar@591
|
384 |
/// the nodes of the component containing \c s,
|
alpar@591
|
385 |
/// otherwise it lists the other component.
|
alpar@591
|
386 |
/// \note As the minimum cut is not always unique,
|
alpar@591
|
387 |
/// \code
|
alpar@591
|
388 |
/// MinCutNodeIt(gomory, s, t, true);
|
alpar@591
|
389 |
/// \endcode
|
alpar@591
|
390 |
/// and
|
alpar@591
|
391 |
/// \code
|
alpar@591
|
392 |
/// MinCutNodeIt(gomory, t, s, false);
|
alpar@591
|
393 |
/// \endcode
|
alpar@591
|
394 |
/// does not necessarily give the same set of nodes.
|
kpeter@833
|
395 |
/// However, it is ensured that
|
alpar@591
|
396 |
/// \code
|
alpar@591
|
397 |
/// MinCutNodeIt(gomory, s, t, true);
|
alpar@591
|
398 |
/// \endcode
|
alpar@591
|
399 |
/// and
|
alpar@591
|
400 |
/// \code
|
alpar@591
|
401 |
/// MinCutNodeIt(gomory, s, t, false);
|
alpar@591
|
402 |
/// \endcode
|
alpar@591
|
403 |
/// together list each node exactly once.
|
alpar@591
|
404 |
)
|
alpar@591
|
405 |
: _side(side), _cut(gomory._graph)
|
alpar@591
|
406 |
{
|
alpar@591
|
407 |
gomory.minCutMap(s,t,_cut);
|
alpar@591
|
408 |
for(_node_it=typename Graph::NodeIt(gomory._graph);
|
alpar@591
|
409 |
_node_it!=INVALID && _cut[_node_it]!=_side;
|
alpar@591
|
410 |
++_node_it) {}
|
alpar@591
|
411 |
}
|
kpeter@593
|
412 |
/// Conversion to \c Node
|
alpar@591
|
413 |
|
kpeter@593
|
414 |
/// Conversion to \c Node.
|
alpar@591
|
415 |
///
|
alpar@591
|
416 |
operator typename Graph::Node() const
|
alpar@591
|
417 |
{
|
alpar@591
|
418 |
return _node_it;
|
alpar@591
|
419 |
}
|
alpar@591
|
420 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
alpar@591
|
421 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
alpar@591
|
422 |
/// Next node
|
alpar@591
|
423 |
|
kpeter@593
|
424 |
/// Next node.
|
alpar@591
|
425 |
///
|
alpar@591
|
426 |
MinCutNodeIt &operator++()
|
alpar@591
|
427 |
{
|
alpar@591
|
428 |
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
|
alpar@591
|
429 |
return *this;
|
alpar@591
|
430 |
}
|
alpar@591
|
431 |
/// Postfix incrementation
|
alpar@591
|
432 |
|
kpeter@593
|
433 |
/// Postfix incrementation.
|
alpar@591
|
434 |
///
|
alpar@591
|
435 |
/// \warning This incrementation
|
kpeter@593
|
436 |
/// returns a \c Node, not a \c MinCutNodeIt, as one may
|
alpar@591
|
437 |
/// expect.
|
alpar@591
|
438 |
typename Graph::Node operator++(int)
|
alpar@591
|
439 |
{
|
alpar@591
|
440 |
typename Graph::Node n=*this;
|
alpar@591
|
441 |
++(*this);
|
alpar@591
|
442 |
return n;
|
alpar@591
|
443 |
}
|
alpar@591
|
444 |
};
|
alpar@956
|
445 |
|
alpar@591
|
446 |
friend class MinCutEdgeIt;
|
alpar@956
|
447 |
|
alpar@591
|
448 |
/// Iterate on the edges of a minimum cut
|
alpar@956
|
449 |
|
alpar@591
|
450 |
/// This iterator class lists the edges of a minimum cut found by
|
kpeter@643
|
451 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class
|
alpar@592
|
452 |
/// and call its \ref GomoryHu::run() "run()" method.
|
alpar@591
|
453 |
///
|
alpar@591
|
454 |
/// This example computes the value of the minimum cut separating \c s from
|
alpar@591
|
455 |
/// \c t.
|
alpar@591
|
456 |
/// \code
|
kpeter@760
|
457 |
/// GomoryHu<Graph> gom(g, capacities);
|
alpar@591
|
458 |
/// gom.run();
|
alpar@591
|
459 |
/// int value=0;
|
kpeter@760
|
460 |
/// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
|
alpar@591
|
461 |
/// value+=capacities[e];
|
alpar@591
|
462 |
/// \endcode
|
kpeter@643
|
463 |
/// The result will be the same as the value returned by
|
kpeter@643
|
464 |
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)".
|
alpar@591
|
465 |
class MinCutEdgeIt
|
alpar@591
|
466 |
{
|
alpar@591
|
467 |
bool _side;
|
alpar@591
|
468 |
const Graph &_graph;
|
alpar@591
|
469 |
typename Graph::NodeIt _node_it;
|
alpar@591
|
470 |
typename Graph::OutArcIt _arc_it;
|
alpar@591
|
471 |
typename Graph::template NodeMap<bool> _cut;
|
alpar@591
|
472 |
void step()
|
alpar@591
|
473 |
{
|
alpar@591
|
474 |
++_arc_it;
|
alpar@591
|
475 |
while(_node_it!=INVALID && _arc_it==INVALID)
|
alpar@591
|
476 |
{
|
alpar@591
|
477 |
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
|
alpar@591
|
478 |
if(_node_it!=INVALID)
|
alpar@591
|
479 |
_arc_it=typename Graph::OutArcIt(_graph,_node_it);
|
alpar@591
|
480 |
}
|
alpar@591
|
481 |
}
|
alpar@956
|
482 |
|
alpar@591
|
483 |
public:
|
kpeter@643
|
484 |
/// Constructor
|
kpeter@643
|
485 |
|
kpeter@643
|
486 |
/// Constructor.
|
kpeter@643
|
487 |
///
|
alpar@592
|
488 |
MinCutEdgeIt(GomoryHu const &gomory,
|
alpar@592
|
489 |
///< The GomoryHu class. You must call its
|
alpar@591
|
490 |
/// run() method
|
kpeter@593
|
491 |
/// before initializing this iterator.
|
kpeter@593
|
492 |
const Node& s, ///< The base node.
|
alpar@591
|
493 |
const Node& t,
|
kpeter@593
|
494 |
///< The node you want to separate from node \c s.
|
alpar@591
|
495 |
bool side=true
|
alpar@591
|
496 |
///< If it is \c true (default) then the listed arcs
|
alpar@591
|
497 |
/// will be oriented from the
|
kpeter@643
|
498 |
/// nodes of the component containing \c s,
|
alpar@591
|
499 |
/// otherwise they will be oriented in the opposite
|
alpar@591
|
500 |
/// direction.
|
alpar@591
|
501 |
)
|
alpar@591
|
502 |
: _graph(gomory._graph), _cut(_graph)
|
alpar@591
|
503 |
{
|
alpar@591
|
504 |
gomory.minCutMap(s,t,_cut);
|
alpar@591
|
505 |
if(!side)
|
alpar@591
|
506 |
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
|
alpar@591
|
507 |
_cut[n]=!_cut[n];
|
alpar@591
|
508 |
|
alpar@591
|
509 |
for(_node_it=typename Graph::NodeIt(_graph);
|
alpar@591
|
510 |
_node_it!=INVALID && !_cut[_node_it];
|
alpar@591
|
511 |
++_node_it) {}
|
alpar@591
|
512 |
_arc_it = _node_it!=INVALID ?
|
alpar@591
|
513 |
typename Graph::OutArcIt(_graph,_node_it) : INVALID;
|
alpar@591
|
514 |
while(_node_it!=INVALID && _arc_it == INVALID)
|
alpar@591
|
515 |
{
|
alpar@591
|
516 |
for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
|
alpar@591
|
517 |
if(_node_it!=INVALID)
|
alpar@591
|
518 |
_arc_it= typename Graph::OutArcIt(_graph,_node_it);
|
alpar@591
|
519 |
}
|
alpar@591
|
520 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
|
alpar@591
|
521 |
}
|
kpeter@593
|
522 |
/// Conversion to \c Arc
|
alpar@591
|
523 |
|
kpeter@593
|
524 |
/// Conversion to \c Arc.
|
alpar@591
|
525 |
///
|
alpar@591
|
526 |
operator typename Graph::Arc() const
|
alpar@591
|
527 |
{
|
alpar@591
|
528 |
return _arc_it;
|
alpar@591
|
529 |
}
|
kpeter@593
|
530 |
/// Conversion to \c Edge
|
alpar@591
|
531 |
|
kpeter@593
|
532 |
/// Conversion to \c Edge.
|
alpar@591
|
533 |
///
|
alpar@591
|
534 |
operator typename Graph::Edge() const
|
alpar@591
|
535 |
{
|
alpar@591
|
536 |
return _arc_it;
|
alpar@591
|
537 |
}
|
alpar@591
|
538 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
alpar@591
|
539 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
alpar@591
|
540 |
/// Next edge
|
alpar@591
|
541 |
|
kpeter@593
|
542 |
/// Next edge.
|
alpar@591
|
543 |
///
|
alpar@591
|
544 |
MinCutEdgeIt &operator++()
|
alpar@591
|
545 |
{
|
alpar@591
|
546 |
step();
|
alpar@591
|
547 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
|
alpar@591
|
548 |
return *this;
|
alpar@591
|
549 |
}
|
alpar@591
|
550 |
/// Postfix incrementation
|
alpar@956
|
551 |
|
kpeter@593
|
552 |
/// Postfix incrementation.
|
alpar@591
|
553 |
///
|
alpar@591
|
554 |
/// \warning This incrementation
|
kpeter@593
|
555 |
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
|
alpar@591
|
556 |
typename Graph::Arc operator++(int)
|
alpar@591
|
557 |
{
|
alpar@591
|
558 |
typename Graph::Arc e=*this;
|
alpar@591
|
559 |
++(*this);
|
alpar@591
|
560 |
return e;
|
alpar@591
|
561 |
}
|
alpar@591
|
562 |
};
|
alpar@591
|
563 |
|
tapolcai@590
|
564 |
};
|
tapolcai@590
|
565 |
|
tapolcai@590
|
566 |
}
|
tapolcai@590
|
567 |
|
tapolcai@590
|
568 |
#endif
|