tapolcai@590
|
1 |
/* -*- C++ -*-
|
tapolcai@590
|
2 |
*
|
tapolcai@590
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
tapolcai@590
|
4 |
*
|
tapolcai@590
|
5 |
* Copyright (C) 2003-2008
|
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
|
tapolcai@590
|
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@591
|
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.
|
tapolcai@590
|
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,
|
kpeter@593
|
63 |
typename CAP>
|
kpeter@593
|
64 |
#else
|
kpeter@593
|
65 |
template <typename GR,
|
kpeter@593
|
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;
|
tapolcai@590
|
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) {
|
tapolcai@590
|
92 |
_pred = new typename Graph::template NodeMap<Node>(_graph);
|
tapolcai@590
|
93 |
}
|
tapolcai@590
|
94 |
if (!_weight) {
|
tapolcai@590
|
95 |
_weight = new typename Graph::template NodeMap<Value>(_graph);
|
tapolcai@590
|
96 |
}
|
tapolcai@590
|
97 |
if (!_order) {
|
tapolcai@590
|
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) {
|
tapolcai@590
|
104 |
delete _pred;
|
tapolcai@590
|
105 |
}
|
tapolcai@590
|
106 |
if (_weight) {
|
tapolcai@590
|
107 |
delete _weight;
|
tapolcai@590
|
108 |
}
|
tapolcai@590
|
109 |
if (_order) {
|
tapolcai@590
|
110 |
delete _order;
|
tapolcai@590
|
111 |
}
|
tapolcai@590
|
112 |
}
|
tapolcai@590
|
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@592
|
121 |
GomoryHu(const Graph& graph, const Capacity& capacity)
|
tapolcai@590
|
122 |
: _graph(graph), _capacity(capacity),
|
tapolcai@590
|
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:
|
kpeter@593
|
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;
|
kpeter@628
|
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) {
|
tapolcai@590
|
157 |
if (n == _root) continue;
|
tapolcai@590
|
158 |
|
tapolcai@590
|
159 |
Node pn = (*_pred)[n];
|
tapolcai@590
|
160 |
fa.source(n);
|
tapolcai@590
|
161 |
fa.target(pn);
|
tapolcai@590
|
162 |
|
tapolcai@590
|
163 |
fa.runMinCut();
|
tapolcai@590
|
164 |
|
kpeter@628
|
165 |
(*_weight)[n] = fa.flowValue();
|
tapolcai@590
|
166 |
|
tapolcai@590
|
167 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
|
tapolcai@590
|
168 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
|
kpeter@628
|
169 |
(*_pred)[nn] = n;
|
tapolcai@590
|
170 |
}
|
tapolcai@590
|
171 |
}
|
tapolcai@590
|
172 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
|
kpeter@628
|
173 |
(*_pred)[n] = (*_pred)[pn];
|
kpeter@628
|
174 |
(*_pred)[pn] = n;
|
kpeter@628
|
175 |
(*_weight)[n] = (*_weight)[pn];
|
kpeter@628
|
176 |
(*_weight)[pn] = fa.flowValue();
|
tapolcai@590
|
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) {
|
tapolcai@590
|
184 |
std::vector<Node> st;
|
tapolcai@590
|
185 |
Node nn = n;
|
tapolcai@590
|
186 |
while ((*_order)[nn] == -1) {
|
tapolcai@590
|
187 |
st.push_back(nn);
|
tapolcai@590
|
188 |
nn = (*_pred)[nn];
|
tapolcai@590
|
189 |
}
|
tapolcai@590
|
190 |
while (!st.empty()) {
|
kpeter@628
|
191 |
(*_order)[st.back()] = index++;
|
tapolcai@590
|
192 |
st.pop_back();
|
tapolcai@590
|
193 |
}
|
tapolcai@590
|
194 |
}
|
tapolcai@590
|
195 |
}
|
tapolcai@590
|
196 |
|
kpeter@593
|
197 |
public:
|
kpeter@593
|
198 |
|
alpar@591
|
199 |
///\name Execution Control
|
alpar@591
|
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@591
|
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 |
///
|
kpeter@643
|
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
|
kpeter@643
|
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();
|
tapolcai@590
|
266 |
|
tapolcai@590
|
267 |
while (sn != tn) {
|
tapolcai@590
|
268 |
if ((*_order)[sn] < (*_order)[tn]) {
|
alpar@591
|
269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn];
|
tapolcai@590
|
270 |
tn = (*_pred)[tn];
|
tapolcai@590
|
271 |
} else {
|
alpar@591
|
272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn];
|
tapolcai@590
|
273 |
sn = (*_pred)[sn];
|
tapolcai@590
|
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@643
|
297 |
Value minCutMap(const Node& s, ///<
|
alpar@591
|
298 |
const Node& t,
|
kpeter@643
|
299 |
///<
|
alpar@591
|
300 |
CutMap& cutMap
|
kpeter@643
|
301 |
///<
|
alpar@591
|
302 |
) const {
|
tapolcai@590
|
303 |
Node sn = s, tn = t;
|
alpar@591
|
304 |
bool s_root=false;
|
tapolcai@590
|
305 |
Node rn = INVALID;
|
tapolcai@590
|
306 |
Value value = std::numeric_limits<Value>::max();
|
tapolcai@590
|
307 |
|
tapolcai@590
|
308 |
while (sn != tn) {
|
tapolcai@590
|
309 |
if ((*_order)[sn] < (*_order)[tn]) {
|
alpar@591
|
310 |
if ((*_weight)[tn] <= value) {
|
tapolcai@590
|
311 |
rn = tn;
|
alpar@591
|
312 |
s_root = false;
|
tapolcai@590
|
313 |
value = (*_weight)[tn];
|
tapolcai@590
|
314 |
}
|
tapolcai@590
|
315 |
tn = (*_pred)[tn];
|
tapolcai@590
|
316 |
} else {
|
alpar@591
|
317 |
if ((*_weight)[sn] <= value) {
|
tapolcai@590
|
318 |
rn = sn;
|
alpar@591
|
319 |
s_root = true;
|
tapolcai@590
|
320 |
value = (*_weight)[sn];
|
tapolcai@590
|
321 |
}
|
tapolcai@590
|
322 |
sn = (*_pred)[sn];
|
tapolcai@590
|
323 |
}
|
tapolcai@590
|
324 |
}
|
tapolcai@590
|
325 |
|
tapolcai@590
|
326 |
typename Graph::template NodeMap<bool> reached(_graph, false);
|
kpeter@628
|
327 |
reached[_root] = true;
|
alpar@591
|
328 |
cutMap.set(_root, !s_root);
|
kpeter@628
|
329 |
reached[rn] = true;
|
alpar@591
|
330 |
cutMap.set(rn, s_root);
|
tapolcai@590
|
331 |
|
alpar@591
|
332 |
std::vector<Node> st;
|
tapolcai@590
|
333 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
alpar@591
|
334 |
st.clear();
|
alpar@591
|
335 |
Node nn = n;
|
tapolcai@590
|
336 |
while (!reached[nn]) {
|
tapolcai@590
|
337 |
st.push_back(nn);
|
tapolcai@590
|
338 |
nn = (*_pred)[nn];
|
tapolcai@590
|
339 |
}
|
tapolcai@590
|
340 |
while (!st.empty()) {
|
tapolcai@590
|
341 |
cutMap.set(st.back(), cutMap[nn]);
|
tapolcai@590
|
342 |
st.pop_back();
|
tapolcai@590
|
343 |
}
|
tapolcai@590
|
344 |
}
|
tapolcai@590
|
345 |
|
tapolcai@590
|
346 |
return value;
|
tapolcai@590
|
347 |
}
|
tapolcai@590
|
348 |
|
alpar@591
|
349 |
///@}
|
alpar@591
|
350 |
|
alpar@591
|
351 |
friend class MinCutNodeIt;
|
alpar@591
|
352 |
|
alpar@591
|
353 |
/// Iterate on the nodes of a minimum cut
|
alpar@591
|
354 |
|
alpar@591
|
355 |
/// This iterator class lists the nodes of a minimum cut found by
|
kpeter@643
|
356 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class
|
alpar@592
|
357 |
/// and call its \ref GomoryHu::run() "run()" method.
|
alpar@591
|
358 |
///
|
alpar@591
|
359 |
/// This example counts the nodes in the minimum cut separating \c s from
|
alpar@591
|
360 |
/// \c t.
|
alpar@591
|
361 |
/// \code
|
alpar@592
|
362 |
/// GomoruHu<Graph> gom(g, capacities);
|
alpar@591
|
363 |
/// gom.run();
|
kpeter@593
|
364 |
/// int cnt=0;
|
kpeter@593
|
365 |
/// for(GomoruHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
|
alpar@591
|
366 |
/// \endcode
|
alpar@591
|
367 |
class MinCutNodeIt
|
alpar@591
|
368 |
{
|
alpar@591
|
369 |
bool _side;
|
alpar@591
|
370 |
typename Graph::NodeIt _node_it;
|
alpar@591
|
371 |
typename Graph::template NodeMap<bool> _cut;
|
alpar@591
|
372 |
public:
|
alpar@591
|
373 |
/// Constructor
|
alpar@591
|
374 |
|
kpeter@593
|
375 |
/// Constructor.
|
alpar@591
|
376 |
///
|
alpar@592
|
377 |
MinCutNodeIt(GomoryHu const &gomory,
|
alpar@592
|
378 |
///< The GomoryHu class. You must call its
|
alpar@591
|
379 |
/// run() method
|
kpeter@593
|
380 |
/// before initializing this iterator.
|
kpeter@593
|
381 |
const Node& s, ///< The base node.
|
alpar@591
|
382 |
const Node& t,
|
kpeter@593
|
383 |
///< The node you want to separate from node \c s.
|
alpar@591
|
384 |
bool side=true
|
alpar@591
|
385 |
///< If it is \c true (default) then the iterator lists
|
alpar@591
|
386 |
/// the nodes of the component containing \c s,
|
alpar@591
|
387 |
/// otherwise it lists the other component.
|
alpar@591
|
388 |
/// \note As the minimum cut is not always unique,
|
alpar@591
|
389 |
/// \code
|
alpar@591
|
390 |
/// MinCutNodeIt(gomory, s, t, true);
|
alpar@591
|
391 |
/// \endcode
|
alpar@591
|
392 |
/// and
|
alpar@591
|
393 |
/// \code
|
alpar@591
|
394 |
/// MinCutNodeIt(gomory, t, s, false);
|
alpar@591
|
395 |
/// \endcode
|
alpar@591
|
396 |
/// does not necessarily give the same set of nodes.
|
alpar@591
|
397 |
/// However it is ensured that
|
alpar@591
|
398 |
/// \code
|
alpar@591
|
399 |
/// MinCutNodeIt(gomory, s, t, true);
|
alpar@591
|
400 |
/// \endcode
|
alpar@591
|
401 |
/// and
|
alpar@591
|
402 |
/// \code
|
alpar@591
|
403 |
/// MinCutNodeIt(gomory, s, t, false);
|
alpar@591
|
404 |
/// \endcode
|
alpar@591
|
405 |
/// together list each node exactly once.
|
alpar@591
|
406 |
)
|
alpar@591
|
407 |
: _side(side), _cut(gomory._graph)
|
alpar@591
|
408 |
{
|
alpar@591
|
409 |
gomory.minCutMap(s,t,_cut);
|
alpar@591
|
410 |
for(_node_it=typename Graph::NodeIt(gomory._graph);
|
alpar@591
|
411 |
_node_it!=INVALID && _cut[_node_it]!=_side;
|
alpar@591
|
412 |
++_node_it) {}
|
alpar@591
|
413 |
}
|
kpeter@593
|
414 |
/// Conversion to \c Node
|
alpar@591
|
415 |
|
kpeter@593
|
416 |
/// Conversion to \c Node.
|
alpar@591
|
417 |
///
|
alpar@591
|
418 |
operator typename Graph::Node() const
|
alpar@591
|
419 |
{
|
alpar@591
|
420 |
return _node_it;
|
alpar@591
|
421 |
}
|
alpar@591
|
422 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
alpar@591
|
423 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
alpar@591
|
424 |
/// Next node
|
alpar@591
|
425 |
|
kpeter@593
|
426 |
/// Next node.
|
alpar@591
|
427 |
///
|
alpar@591
|
428 |
MinCutNodeIt &operator++()
|
alpar@591
|
429 |
{
|
alpar@591
|
430 |
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
|
alpar@591
|
431 |
return *this;
|
alpar@591
|
432 |
}
|
alpar@591
|
433 |
/// Postfix incrementation
|
alpar@591
|
434 |
|
kpeter@593
|
435 |
/// Postfix incrementation.
|
alpar@591
|
436 |
///
|
alpar@591
|
437 |
/// \warning This incrementation
|
kpeter@593
|
438 |
/// returns a \c Node, not a \c MinCutNodeIt, as one may
|
alpar@591
|
439 |
/// expect.
|
alpar@591
|
440 |
typename Graph::Node operator++(int)
|
alpar@591
|
441 |
{
|
alpar@591
|
442 |
typename Graph::Node n=*this;
|
alpar@591
|
443 |
++(*this);
|
alpar@591
|
444 |
return n;
|
alpar@591
|
445 |
}
|
alpar@591
|
446 |
};
|
alpar@591
|
447 |
|
alpar@591
|
448 |
friend class MinCutEdgeIt;
|
alpar@591
|
449 |
|
alpar@591
|
450 |
/// Iterate on the edges of a minimum cut
|
alpar@591
|
451 |
|
alpar@591
|
452 |
/// This iterator class lists the edges of a minimum cut found by
|
kpeter@643
|
453 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class
|
alpar@592
|
454 |
/// and call its \ref GomoryHu::run() "run()" method.
|
alpar@591
|
455 |
///
|
alpar@591
|
456 |
/// This example computes the value of the minimum cut separating \c s from
|
alpar@591
|
457 |
/// \c t.
|
alpar@591
|
458 |
/// \code
|
alpar@592
|
459 |
/// GomoruHu<Graph> gom(g, capacities);
|
alpar@591
|
460 |
/// gom.run();
|
alpar@591
|
461 |
/// int value=0;
|
kpeter@593
|
462 |
/// for(GomoruHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
|
alpar@591
|
463 |
/// value+=capacities[e];
|
alpar@591
|
464 |
/// \endcode
|
kpeter@643
|
465 |
/// The result will be the same as the value returned by
|
kpeter@643
|
466 |
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)".
|
alpar@591
|
467 |
class MinCutEdgeIt
|
alpar@591
|
468 |
{
|
alpar@591
|
469 |
bool _side;
|
alpar@591
|
470 |
const Graph &_graph;
|
alpar@591
|
471 |
typename Graph::NodeIt _node_it;
|
alpar@591
|
472 |
typename Graph::OutArcIt _arc_it;
|
alpar@591
|
473 |
typename Graph::template NodeMap<bool> _cut;
|
alpar@591
|
474 |
void step()
|
alpar@591
|
475 |
{
|
alpar@591
|
476 |
++_arc_it;
|
alpar@591
|
477 |
while(_node_it!=INVALID && _arc_it==INVALID)
|
alpar@591
|
478 |
{
|
alpar@591
|
479 |
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
|
alpar@591
|
480 |
if(_node_it!=INVALID)
|
alpar@591
|
481 |
_arc_it=typename Graph::OutArcIt(_graph,_node_it);
|
alpar@591
|
482 |
}
|
alpar@591
|
483 |
}
|
alpar@591
|
484 |
|
alpar@591
|
485 |
public:
|
kpeter@643
|
486 |
/// Constructor
|
kpeter@643
|
487 |
|
kpeter@643
|
488 |
/// Constructor.
|
kpeter@643
|
489 |
///
|
alpar@592
|
490 |
MinCutEdgeIt(GomoryHu const &gomory,
|
alpar@592
|
491 |
///< The GomoryHu class. You must call its
|
alpar@591
|
492 |
/// run() method
|
kpeter@593
|
493 |
/// before initializing this iterator.
|
kpeter@593
|
494 |
const Node& s, ///< The base node.
|
alpar@591
|
495 |
const Node& t,
|
kpeter@593
|
496 |
///< The node you want to separate from node \c s.
|
alpar@591
|
497 |
bool side=true
|
alpar@591
|
498 |
///< If it is \c true (default) then the listed arcs
|
alpar@591
|
499 |
/// will be oriented from the
|
kpeter@643
|
500 |
/// nodes of the component containing \c s,
|
alpar@591
|
501 |
/// otherwise they will be oriented in the opposite
|
alpar@591
|
502 |
/// direction.
|
alpar@591
|
503 |
)
|
alpar@591
|
504 |
: _graph(gomory._graph), _cut(_graph)
|
alpar@591
|
505 |
{
|
alpar@591
|
506 |
gomory.minCutMap(s,t,_cut);
|
alpar@591
|
507 |
if(!side)
|
alpar@591
|
508 |
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
|
alpar@591
|
509 |
_cut[n]=!_cut[n];
|
alpar@591
|
510 |
|
alpar@591
|
511 |
for(_node_it=typename Graph::NodeIt(_graph);
|
alpar@591
|
512 |
_node_it!=INVALID && !_cut[_node_it];
|
alpar@591
|
513 |
++_node_it) {}
|
alpar@591
|
514 |
_arc_it = _node_it!=INVALID ?
|
alpar@591
|
515 |
typename Graph::OutArcIt(_graph,_node_it) : INVALID;
|
alpar@591
|
516 |
while(_node_it!=INVALID && _arc_it == INVALID)
|
alpar@591
|
517 |
{
|
alpar@591
|
518 |
for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
|
alpar@591
|
519 |
if(_node_it!=INVALID)
|
alpar@591
|
520 |
_arc_it= typename Graph::OutArcIt(_graph,_node_it);
|
alpar@591
|
521 |
}
|
alpar@591
|
522 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
|
alpar@591
|
523 |
}
|
kpeter@593
|
524 |
/// Conversion to \c Arc
|
alpar@591
|
525 |
|
kpeter@593
|
526 |
/// Conversion to \c Arc.
|
alpar@591
|
527 |
///
|
alpar@591
|
528 |
operator typename Graph::Arc() const
|
alpar@591
|
529 |
{
|
alpar@591
|
530 |
return _arc_it;
|
alpar@591
|
531 |
}
|
kpeter@593
|
532 |
/// Conversion to \c Edge
|
alpar@591
|
533 |
|
kpeter@593
|
534 |
/// Conversion to \c Edge.
|
alpar@591
|
535 |
///
|
alpar@591
|
536 |
operator typename Graph::Edge() const
|
alpar@591
|
537 |
{
|
alpar@591
|
538 |
return _arc_it;
|
alpar@591
|
539 |
}
|
alpar@591
|
540 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
alpar@591
|
541 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
alpar@591
|
542 |
/// Next edge
|
alpar@591
|
543 |
|
kpeter@593
|
544 |
/// Next edge.
|
alpar@591
|
545 |
///
|
alpar@591
|
546 |
MinCutEdgeIt &operator++()
|
alpar@591
|
547 |
{
|
alpar@591
|
548 |
step();
|
alpar@591
|
549 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
|
alpar@591
|
550 |
return *this;
|
alpar@591
|
551 |
}
|
alpar@591
|
552 |
/// Postfix incrementation
|
alpar@591
|
553 |
|
kpeter@593
|
554 |
/// Postfix incrementation.
|
alpar@591
|
555 |
///
|
alpar@591
|
556 |
/// \warning This incrementation
|
kpeter@593
|
557 |
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
|
alpar@591
|
558 |
typename Graph::Arc operator++(int)
|
alpar@591
|
559 |
{
|
alpar@591
|
560 |
typename Graph::Arc e=*this;
|
alpar@591
|
561 |
++(*this);
|
alpar@591
|
562 |
return e;
|
alpar@591
|
563 |
}
|
alpar@591
|
564 |
};
|
alpar@591
|
565 |
|
tapolcai@590
|
566 |
};
|
tapolcai@590
|
567 |
|
tapolcai@590
|
568 |
}
|
tapolcai@590
|
569 |
|
tapolcai@590
|
570 |
#endif
|