gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge #306
0 1 0
merge default
0 files changed with 17 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_HAO_ORLIN_H
20 20
#define LEMON_HAO_ORLIN_H
21 21

	
22 22
#include <vector>
23 23
#include <list>
24 24
#include <limits>
25 25

	
26 26
#include <lemon/maps.h>
27 27
#include <lemon/core.h>
28 28
#include <lemon/tolerance.h>
29 29

	
30 30
/// \file
31 31
/// \ingroup min_cut
32 32
/// \brief Implementation of the Hao-Orlin algorithm.
33 33
///
34 34
/// Implementation of the Hao-Orlin algorithm for finding a minimum cut 
35 35
/// in a digraph.
36 36

	
37 37
namespace lemon {
38 38

	
39 39
  /// \ingroup min_cut
40 40
  ///
41 41
  /// \brief Hao-Orlin algorithm for finding a minimum cut in a digraph.
42 42
  ///
43 43
  /// This class implements the Hao-Orlin algorithm for finding a minimum
44 44
  /// value cut in a directed graph \f$D=(V,A)\f$. 
45 45
  /// It takes a fixed node \f$ source \in V \f$ and
46 46
  /// consists of two phases: in the first phase it determines a
47 47
  /// minimum cut with \f$ source \f$ on the source-side (i.e. a set
48 48
  /// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal outgoing
49 49
  /// capacity) and in the second phase it determines a minimum cut
50 50
  /// with \f$ source \f$ on the sink-side (i.e. a set
51 51
  /// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal outgoing
52 52
  /// capacity). Obviously, the smaller of these two cuts will be a
53 53
  /// minimum cut of \f$ D \f$. The algorithm is a modified
54 54
  /// preflow push-relabel algorithm. Our implementation calculates
55 55
  /// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
56 56
  /// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The
57 57
  /// purpose of such algorithm is e.g. testing network reliability.
58 58
  ///
59 59
  /// For an undirected graph you can run just the first phase of the
60 60
  /// algorithm or you can use the algorithm of Nagamochi and Ibaraki,
61 61
  /// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ 
62 62
  /// time. It is implemented in the NagamochiIbaraki algorithm class.
63 63
  ///
64 64
  /// \tparam GR The type of the digraph the algorithm runs on.
65 65
  /// \tparam CAP The type of the arc map containing the capacities,
66 66
  /// which can be any numreric type. The default map type is
67 67
  /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
68 68
  /// \tparam TOL Tolerance class for handling inexact computations. The
69 69
  /// default tolerance type is \ref Tolerance "Tolerance<CAP::Value>".
70 70
#ifdef DOXYGEN
71 71
  template <typename GR, typename CAP, typename TOL>
72 72
#else
73 73
  template <typename GR,
74 74
            typename CAP = typename GR::template ArcMap<int>,
75 75
            typename TOL = Tolerance<typename CAP::Value> >
76 76
#endif
77 77
  class HaoOrlin {
78 78
  public:
79 79
   
80 80
    /// The digraph type of the algorithm
81 81
    typedef GR Digraph;
82 82
    /// The capacity map type of the algorithm
83 83
    typedef CAP CapacityMap;
84 84
    /// The tolerance type of the algorithm
85 85
    typedef TOL Tolerance;
86 86

	
87 87
  private:
88 88

	
89 89
    typedef typename CapacityMap::Value Value;
90 90

	
91 91
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
92 92

	
93 93
    const Digraph& _graph;
94 94
    const CapacityMap* _capacity;
95 95

	
96 96
    typedef typename Digraph::template ArcMap<Value> FlowMap;
97 97
    FlowMap* _flow;
98 98

	
99 99
    Node _source;
100 100

	
101 101
    int _node_num;
102 102

	
103 103
    // Bucketing structure
104 104
    std::vector<Node> _first, _last;
105 105
    typename Digraph::template NodeMap<Node>* _next;
106 106
    typename Digraph::template NodeMap<Node>* _prev;
107 107
    typename Digraph::template NodeMap<bool>* _active;
108 108
    typename Digraph::template NodeMap<int>* _bucket;
109 109

	
110 110
    std::vector<bool> _dormant;
111 111

	
112 112
    std::list<std::list<int> > _sets;
113 113
    std::list<int>::iterator _highest;
114 114

	
115 115
    typedef typename Digraph::template NodeMap<Value> ExcessMap;
116 116
    ExcessMap* _excess;
117 117

	
118 118
    typedef typename Digraph::template NodeMap<bool> SourceSetMap;
119 119
    SourceSetMap* _source_set;
120 120

	
121 121
    Value _min_cut;
122 122

	
123 123
    typedef typename Digraph::template NodeMap<bool> MinCutMap;
124 124
    MinCutMap* _min_cut_map;
125 125

	
126 126
    Tolerance _tolerance;
127 127

	
128 128
  public:
129 129

	
130 130
    /// \brief Constructor
131 131
    ///
132 132
    /// Constructor of the algorithm class.
133 133
    HaoOrlin(const Digraph& graph, const CapacityMap& capacity,
134 134
             const Tolerance& tolerance = Tolerance()) :
135 135
      _graph(graph), _capacity(&capacity), _flow(0), _source(),
136 136
      _node_num(), _first(), _last(), _next(0), _prev(0),
137 137
      _active(0), _bucket(0), _dormant(), _sets(), _highest(),
138 138
      _excess(0), _source_set(0), _min_cut(), _min_cut_map(0),
139 139
      _tolerance(tolerance) {}
140 140

	
141 141
    ~HaoOrlin() {
142 142
      if (_min_cut_map) {
143 143
        delete _min_cut_map;
144 144
      }
145 145
      if (_source_set) {
146 146
        delete _source_set;
147 147
      }
148 148
      if (_excess) {
149 149
        delete _excess;
150 150
      }
151 151
      if (_next) {
152 152
        delete _next;
153 153
      }
154 154
      if (_prev) {
155 155
        delete _prev;
156 156
      }
157 157
      if (_active) {
158 158
        delete _active;
159 159
      }
160 160
      if (_bucket) {
161 161
        delete _bucket;
162 162
      }
163 163
      if (_flow) {
164 164
        delete _flow;
165 165
      }
166 166
    }
167 167

	
168
    /// \brief Set the tolerance used by the algorithm.
169
    ///
170
    /// This function sets the tolerance object used by the algorithm.
171
    /// \return <tt>(*this)</tt>
172
    HaoOrlin& tolerance(const Tolerance& tolerance) {
173
      _tolerance = tolerance;
174
      return *this;
175
    }
176

	
177
    /// \brief Returns a const reference to the tolerance.
178
    ///
179
    /// This function returns a const reference to the tolerance object
180
    /// used by the algorithm.
181
    const Tolerance& tolerance() const {
182
      return _tolerance;
183
    }
184

	
168 185
  private:
169 186

	
170 187
    void activate(const Node& i) {
171 188
      (*_active)[i] = true;
172 189

	
173 190
      int bucket = (*_bucket)[i];
174 191

	
175 192
      if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return;
176 193
      //unlace
177 194
      (*_next)[(*_prev)[i]] = (*_next)[i];
178 195
      if ((*_next)[i] != INVALID) {
179 196
        (*_prev)[(*_next)[i]] = (*_prev)[i];
180 197
      } else {
181 198
        _last[bucket] = (*_prev)[i];
182 199
      }
183 200
      //lace
184 201
      (*_next)[i] = _first[bucket];
185 202
      (*_prev)[_first[bucket]] = i;
186 203
      (*_prev)[i] = INVALID;
187 204
      _first[bucket] = i;
188 205
    }
189 206

	
190 207
    void deactivate(const Node& i) {
191 208
      (*_active)[i] = false;
192 209
      int bucket = (*_bucket)[i];
193 210

	
194 211
      if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return;
195 212

	
196 213
      //unlace
197 214
      (*_prev)[(*_next)[i]] = (*_prev)[i];
198 215
      if ((*_prev)[i] != INVALID) {
199 216
        (*_next)[(*_prev)[i]] = (*_next)[i];
200 217
      } else {
201 218
        _first[bucket] = (*_next)[i];
202 219
      }
203 220
      //lace
204 221
      (*_prev)[i] = _last[bucket];
205 222
      (*_next)[_last[bucket]] = i;
206 223
      (*_next)[i] = INVALID;
207 224
      _last[bucket] = i;
208 225
    }
209 226

	
210 227
    void addItem(const Node& i, int bucket) {
211 228
      (*_bucket)[i] = bucket;
212 229
      if (_last[bucket] != INVALID) {
213 230
        (*_prev)[i] = _last[bucket];
214 231
        (*_next)[_last[bucket]] = i;
215 232
        (*_next)[i] = INVALID;
216 233
        _last[bucket] = i;
217 234
      } else {
218 235
        (*_prev)[i] = INVALID;
219 236
        _first[bucket] = i;
220 237
        (*_next)[i] = INVALID;
221 238
        _last[bucket] = i;
222 239
      }
223 240
    }
224 241

	
225 242
    void findMinCutOut() {
226 243

	
227 244
      for (NodeIt n(_graph); n != INVALID; ++n) {
228 245
        (*_excess)[n] = 0;
229 246
        (*_source_set)[n] = false;
230 247
      }
231 248

	
232 249
      for (ArcIt a(_graph); a != INVALID; ++a) {
233 250
        (*_flow)[a] = 0;
234 251
      }
235 252

	
236 253
      int bucket_num = 0;
237 254
      std::vector<Node> queue(_node_num);
238 255
      int qfirst = 0, qlast = 0, qsep = 0;
239 256

	
240 257
      {
241 258
        typename Digraph::template NodeMap<bool> reached(_graph, false);
242 259

	
243 260
        reached[_source] = true;
244 261
        bool first_set = true;
245 262

	
246 263
        for (NodeIt t(_graph); t != INVALID; ++t) {
247 264
          if (reached[t]) continue;
248 265
          _sets.push_front(std::list<int>());
249 266

	
250 267
          queue[qlast++] = t;
251 268
          reached[t] = true;
252 269

	
253 270
          while (qfirst != qlast) {
254 271
            if (qsep == qfirst) {
255 272
              ++bucket_num;
256 273
              _sets.front().push_front(bucket_num);
257 274
              _dormant[bucket_num] = !first_set;
258 275
              _first[bucket_num] = _last[bucket_num] = INVALID;
259 276
              qsep = qlast;
260 277
            }
261 278

	
262 279
            Node n = queue[qfirst++];
263 280
            addItem(n, bucket_num);
264 281

	
265 282
            for (InArcIt a(_graph, n); a != INVALID; ++a) {
266 283
              Node u = _graph.source(a);
267 284
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
268 285
                reached[u] = true;
269 286
                queue[qlast++] = u;
270 287
              }
271 288
            }
272 289
          }
273 290
          first_set = false;
274 291
        }
275 292

	
276 293
        ++bucket_num;
277 294
        (*_bucket)[_source] = 0;
278 295
        _dormant[0] = true;
279 296
      }
280 297
      (*_source_set)[_source] = true;
281 298

	
282 299
      Node target = _last[_sets.back().back()];
283 300
      {
284 301
        for (OutArcIt a(_graph, _source); a != INVALID; ++a) {
285 302
          if (_tolerance.positive((*_capacity)[a])) {
286 303
            Node u = _graph.target(a);
287 304
            (*_flow)[a] = (*_capacity)[a];
288 305
            (*_excess)[u] += (*_capacity)[a];
289 306
            if (!(*_active)[u] && u != _source) {
290 307
              activate(u);
291 308
            }
292 309
          }
293 310
        }
294 311

	
295 312
        if ((*_active)[target]) {
296 313
          deactivate(target);
297 314
        }
298 315

	
299 316
        _highest = _sets.back().begin();
300 317
        while (_highest != _sets.back().end() &&
301 318
               !(*_active)[_first[*_highest]]) {
302 319
          ++_highest;
303 320
        }
304 321
      }
305 322

	
306 323
      while (true) {
307 324
        while (_highest != _sets.back().end()) {
308 325
          Node n = _first[*_highest];
309 326
          Value excess = (*_excess)[n];
310 327
          int next_bucket = _node_num;
311 328

	
312 329
          int under_bucket;
313 330
          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
314 331
            under_bucket = -1;
315 332
          } else {
316 333
            under_bucket = *(++std::list<int>::iterator(_highest));
317 334
          }
318 335

	
319 336
          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
320 337
            Node v = _graph.target(a);
321 338
            if (_dormant[(*_bucket)[v]]) continue;
322 339
            Value rem = (*_capacity)[a] - (*_flow)[a];
323 340
            if (!_tolerance.positive(rem)) continue;
324 341
            if ((*_bucket)[v] == under_bucket) {
325 342
              if (!(*_active)[v] && v != target) {
326 343
                activate(v);
327 344
              }
328 345
              if (!_tolerance.less(rem, excess)) {
329 346
                (*_flow)[a] += excess;
330 347
                (*_excess)[v] += excess;
331 348
                excess = 0;
332 349
                goto no_more_push;
333 350
              } else {
334 351
                excess -= rem;
335 352
                (*_excess)[v] += rem;
336 353
                (*_flow)[a] = (*_capacity)[a];
337 354
              }
338 355
            } else if (next_bucket > (*_bucket)[v]) {
339 356
              next_bucket = (*_bucket)[v];
340 357
            }
341 358
          }
342 359

	
343 360
          for (InArcIt a(_graph, n); a != INVALID; ++a) {
344 361
            Node v = _graph.source(a);
345 362
            if (_dormant[(*_bucket)[v]]) continue;
346 363
            Value rem = (*_flow)[a];
347 364
            if (!_tolerance.positive(rem)) continue;
348 365
            if ((*_bucket)[v] == under_bucket) {
349 366
              if (!(*_active)[v] && v != target) {
350 367
                activate(v);
351 368
              }
352 369
              if (!_tolerance.less(rem, excess)) {
353 370
                (*_flow)[a] -= excess;
354 371
                (*_excess)[v] += excess;
355 372
                excess = 0;
356 373
                goto no_more_push;
357 374
              } else {
358 375
                excess -= rem;
359 376
                (*_excess)[v] += rem;
0 comments (0 inline)