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 6144 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;
360 377
                (*_flow)[a] = 0;
361 378
              }
362 379
            } else if (next_bucket > (*_bucket)[v]) {
363 380
              next_bucket = (*_bucket)[v];
364 381
            }
365 382
          }
366 383

	
367 384
        no_more_push:
368 385

	
369 386
          (*_excess)[n] = excess;
370 387

	
371 388
          if (excess != 0) {
372 389
            if ((*_next)[n] == INVALID) {
373 390
              typename std::list<std::list<int> >::iterator new_set =
374 391
                _sets.insert(--_sets.end(), std::list<int>());
375 392
              new_set->splice(new_set->end(), _sets.back(),
376 393
                              _sets.back().begin(), ++_highest);
377 394
              for (std::list<int>::iterator it = new_set->begin();
378 395
                   it != new_set->end(); ++it) {
379 396
                _dormant[*it] = true;
380 397
              }
381 398
              while (_highest != _sets.back().end() &&
382 399
                     !(*_active)[_first[*_highest]]) {
383 400
                ++_highest;
384 401
              }
385 402
            } else if (next_bucket == _node_num) {
386 403
              _first[(*_bucket)[n]] = (*_next)[n];
387 404
              (*_prev)[(*_next)[n]] = INVALID;
388 405

	
389 406
              std::list<std::list<int> >::iterator new_set =
390 407
                _sets.insert(--_sets.end(), std::list<int>());
391 408

	
392 409
              new_set->push_front(bucket_num);
393 410
              (*_bucket)[n] = bucket_num;
394 411
              _first[bucket_num] = _last[bucket_num] = n;
395 412
              (*_next)[n] = INVALID;
396 413
              (*_prev)[n] = INVALID;
397 414
              _dormant[bucket_num] = true;
398 415
              ++bucket_num;
399 416

	
400 417
              while (_highest != _sets.back().end() &&
401 418
                     !(*_active)[_first[*_highest]]) {
402 419
                ++_highest;
403 420
              }
404 421
            } else {
405 422
              _first[*_highest] = (*_next)[n];
406 423
              (*_prev)[(*_next)[n]] = INVALID;
407 424

	
408 425
              while (next_bucket != *_highest) {
409 426
                --_highest;
410 427
              }
411 428

	
412 429
              if (_highest == _sets.back().begin()) {
413 430
                _sets.back().push_front(bucket_num);
414 431
                _dormant[bucket_num] = false;
415 432
                _first[bucket_num] = _last[bucket_num] = INVALID;
416 433
                ++bucket_num;
417 434
              }
418 435
              --_highest;
419 436

	
420 437
              (*_bucket)[n] = *_highest;
421 438
              (*_next)[n] = _first[*_highest];
422 439
              if (_first[*_highest] != INVALID) {
423 440
                (*_prev)[_first[*_highest]] = n;
424 441
              } else {
425 442
                _last[*_highest] = n;
426 443
              }
427 444
              _first[*_highest] = n;
428 445
            }
429 446
          } else {
430 447

	
431 448
            deactivate(n);
432 449
            if (!(*_active)[_first[*_highest]]) {
433 450
              ++_highest;
434 451
              if (_highest != _sets.back().end() &&
435 452
                  !(*_active)[_first[*_highest]]) {
436 453
                _highest = _sets.back().end();
437 454
              }
438 455
            }
439 456
          }
440 457
        }
441 458

	
442 459
        if ((*_excess)[target] < _min_cut) {
443 460
          _min_cut = (*_excess)[target];
444 461
          for (NodeIt i(_graph); i != INVALID; ++i) {
445 462
            (*_min_cut_map)[i] = true;
446 463
          }
447 464
          for (std::list<int>::iterator it = _sets.back().begin();
448 465
               it != _sets.back().end(); ++it) {
449 466
            Node n = _first[*it];
450 467
            while (n != INVALID) {
451 468
              (*_min_cut_map)[n] = false;
452 469
              n = (*_next)[n];
453 470
            }
454 471
          }
455 472
        }
456 473

	
457 474
        {
458 475
          Node new_target;
459 476
          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
460 477
            if ((*_next)[target] == INVALID) {
461 478
              _last[(*_bucket)[target]] = (*_prev)[target];
462 479
              new_target = (*_prev)[target];
463 480
            } else {
464 481
              (*_prev)[(*_next)[target]] = (*_prev)[target];
465 482
              new_target = (*_next)[target];
466 483
            }
467 484
            if ((*_prev)[target] == INVALID) {
468 485
              _first[(*_bucket)[target]] = (*_next)[target];
469 486
            } else {
470 487
              (*_next)[(*_prev)[target]] = (*_next)[target];
471 488
            }
472 489
          } else {
473 490
            _sets.back().pop_back();
474 491
            if (_sets.back().empty()) {
475 492
              _sets.pop_back();
476 493
              if (_sets.empty())
477 494
                break;
478 495
              for (std::list<int>::iterator it = _sets.back().begin();
479 496
                   it != _sets.back().end(); ++it) {
480 497
                _dormant[*it] = false;
481 498
              }
482 499
            }
483 500
            new_target = _last[_sets.back().back()];
484 501
          }
485 502

	
486 503
          (*_bucket)[target] = 0;
487 504

	
488 505
          (*_source_set)[target] = true;
489 506
          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
490 507
            Value rem = (*_capacity)[a] - (*_flow)[a];
491 508
            if (!_tolerance.positive(rem)) continue;
492 509
            Node v = _graph.target(a);
493 510
            if (!(*_active)[v] && !(*_source_set)[v]) {
494 511
              activate(v);
495 512
            }
496 513
            (*_excess)[v] += rem;
497 514
            (*_flow)[a] = (*_capacity)[a];
498 515
          }
499 516

	
500 517
          for (InArcIt a(_graph, target); a != INVALID; ++a) {
501 518
            Value rem = (*_flow)[a];
502 519
            if (!_tolerance.positive(rem)) continue;
503 520
            Node v = _graph.source(a);
504 521
            if (!(*_active)[v] && !(*_source_set)[v]) {
505 522
              activate(v);
506 523
            }
507 524
            (*_excess)[v] += rem;
508 525
            (*_flow)[a] = 0;
509 526
          }
510 527

	
511 528
          target = new_target;
512 529
          if ((*_active)[target]) {
513 530
            deactivate(target);
514 531
          }
515 532

	
516 533
          _highest = _sets.back().begin();
517 534
          while (_highest != _sets.back().end() &&
518 535
                 !(*_active)[_first[*_highest]]) {
519 536
            ++_highest;
520 537
          }
521 538
        }
522 539
      }
523 540
    }
524 541

	
525 542
    void findMinCutIn() {
526 543

	
527 544
      for (NodeIt n(_graph); n != INVALID; ++n) {
528 545
        (*_excess)[n] = 0;
529 546
        (*_source_set)[n] = false;
530 547
      }
531 548

	
532 549
      for (ArcIt a(_graph); a != INVALID; ++a) {
533 550
        (*_flow)[a] = 0;
534 551
      }
535 552

	
536 553
      int bucket_num = 0;
537 554
      std::vector<Node> queue(_node_num);
538 555
      int qfirst = 0, qlast = 0, qsep = 0;
539 556

	
540 557
      {
541 558
        typename Digraph::template NodeMap<bool> reached(_graph, false);
542 559

	
543 560
        reached[_source] = true;
544 561

	
545 562
        bool first_set = true;
546 563

	
547 564
        for (NodeIt t(_graph); t != INVALID; ++t) {
548 565
          if (reached[t]) continue;
549 566
          _sets.push_front(std::list<int>());
550 567

	
551 568
          queue[qlast++] = t;
552 569
          reached[t] = true;
553 570

	
554 571
          while (qfirst != qlast) {
555 572
            if (qsep == qfirst) {
556 573
              ++bucket_num;
557 574
              _sets.front().push_front(bucket_num);
558 575
              _dormant[bucket_num] = !first_set;
559 576
              _first[bucket_num] = _last[bucket_num] = INVALID;
560 577
              qsep = qlast;
561 578
            }
562 579

	
563 580
            Node n = queue[qfirst++];
564 581
            addItem(n, bucket_num);
565 582

	
566 583
            for (OutArcIt a(_graph, n); a != INVALID; ++a) {
567 584
              Node u = _graph.target(a);
568 585
              if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
569 586
                reached[u] = true;
570 587
                queue[qlast++] = u;
571 588
              }
572 589
            }
573 590
          }
574 591
          first_set = false;
575 592
        }
576 593

	
577 594
        ++bucket_num;
578 595
        (*_bucket)[_source] = 0;
579 596
        _dormant[0] = true;
580 597
      }
581 598
      (*_source_set)[_source] = true;
582 599

	
583 600
      Node target = _last[_sets.back().back()];
584 601
      {
585 602
        for (InArcIt a(_graph, _source); a != INVALID; ++a) {
586 603
          if (_tolerance.positive((*_capacity)[a])) {
587 604
            Node u = _graph.source(a);
588 605
            (*_flow)[a] = (*_capacity)[a];
589 606
            (*_excess)[u] += (*_capacity)[a];
590 607
            if (!(*_active)[u] && u != _source) {
591 608
              activate(u);
592 609
            }
593 610
          }
594 611
        }
595 612
        if ((*_active)[target]) {
596 613
          deactivate(target);
597 614
        }
598 615

	
599 616
        _highest = _sets.back().begin();
600 617
        while (_highest != _sets.back().end() &&
601 618
               !(*_active)[_first[*_highest]]) {
602 619
          ++_highest;
603 620
        }
604 621
      }
605 622

	
606 623

	
607 624
      while (true) {
608 625
        while (_highest != _sets.back().end()) {
609 626
          Node n = _first[*_highest];
610 627
          Value excess = (*_excess)[n];
611 628
          int next_bucket = _node_num;
612 629

	
613 630
          int under_bucket;
614 631
          if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
615 632
            under_bucket = -1;
616 633
          } else {
617 634
            under_bucket = *(++std::list<int>::iterator(_highest));
618 635
          }
619 636

	
620 637
          for (InArcIt a(_graph, n); a != INVALID; ++a) {
621 638
            Node v = _graph.source(a);
622 639
            if (_dormant[(*_bucket)[v]]) continue;
623 640
            Value rem = (*_capacity)[a] - (*_flow)[a];
624 641
            if (!_tolerance.positive(rem)) continue;
625 642
            if ((*_bucket)[v] == under_bucket) {
626 643
              if (!(*_active)[v] && v != target) {
627 644
                activate(v);
628 645
              }
629 646
              if (!_tolerance.less(rem, excess)) {
630 647
                (*_flow)[a] += excess;
631 648
                (*_excess)[v] += excess;
632 649
                excess = 0;
633 650
                goto no_more_push;
634 651
              } else {
635 652
                excess -= rem;
636 653
                (*_excess)[v] += rem;
637 654
                (*_flow)[a] = (*_capacity)[a];
638 655
              }
639 656
            } else if (next_bucket > (*_bucket)[v]) {
640 657
              next_bucket = (*_bucket)[v];
641 658
            }
642 659
          }
643 660

	
644 661
          for (OutArcIt a(_graph, n); a != INVALID; ++a) {
645 662
            Node v = _graph.target(a);
646 663
            if (_dormant[(*_bucket)[v]]) continue;
647 664
            Value rem = (*_flow)[a];
648 665
            if (!_tolerance.positive(rem)) continue;
649 666
            if ((*_bucket)[v] == under_bucket) {
650 667
              if (!(*_active)[v] && v != target) {
651 668
                activate(v);
652 669
              }
653 670
              if (!_tolerance.less(rem, excess)) {
654 671
                (*_flow)[a] -= excess;
655 672
                (*_excess)[v] += excess;
656 673
                excess = 0;
657 674
                goto no_more_push;
658 675
              } else {
659 676
                excess -= rem;
660 677
                (*_excess)[v] += rem;
661 678
                (*_flow)[a] = 0;
662 679
              }
663 680
            } else if (next_bucket > (*_bucket)[v]) {
664 681
              next_bucket = (*_bucket)[v];
665 682
            }
666 683
          }
667 684

	
668 685
        no_more_push:
669 686

	
670 687
          (*_excess)[n] = excess;
671 688

	
672 689
          if (excess != 0) {
673 690
            if ((*_next)[n] == INVALID) {
674 691
              typename std::list<std::list<int> >::iterator new_set =
675 692
                _sets.insert(--_sets.end(), std::list<int>());
676 693
              new_set->splice(new_set->end(), _sets.back(),
677 694
                              _sets.back().begin(), ++_highest);
678 695
              for (std::list<int>::iterator it = new_set->begin();
679 696
                   it != new_set->end(); ++it) {
680 697
                _dormant[*it] = true;
681 698
              }
682 699
              while (_highest != _sets.back().end() &&
683 700
                     !(*_active)[_first[*_highest]]) {
684 701
                ++_highest;
685 702
              }
686 703
            } else if (next_bucket == _node_num) {
687 704
              _first[(*_bucket)[n]] = (*_next)[n];
688 705
              (*_prev)[(*_next)[n]] = INVALID;
689 706

	
690 707
              std::list<std::list<int> >::iterator new_set =
691 708
                _sets.insert(--_sets.end(), std::list<int>());
692 709

	
693 710
              new_set->push_front(bucket_num);
694 711
              (*_bucket)[n] = bucket_num;
695 712
              _first[bucket_num] = _last[bucket_num] = n;
696 713
              (*_next)[n] = INVALID;
697 714
              (*_prev)[n] = INVALID;
698 715
              _dormant[bucket_num] = true;
699 716
              ++bucket_num;
700 717

	
701 718
              while (_highest != _sets.back().end() &&
702 719
                     !(*_active)[_first[*_highest]]) {
703 720
                ++_highest;
704 721
              }
705 722
            } else {
706 723
              _first[*_highest] = (*_next)[n];
707 724
              (*_prev)[(*_next)[n]] = INVALID;
708 725

	
709 726
              while (next_bucket != *_highest) {
710 727
                --_highest;
711 728
              }
712 729
              if (_highest == _sets.back().begin()) {
713 730
                _sets.back().push_front(bucket_num);
714 731
                _dormant[bucket_num] = false;
715 732
                _first[bucket_num] = _last[bucket_num] = INVALID;
716 733
                ++bucket_num;
717 734
              }
718 735
              --_highest;
719 736

	
720 737
              (*_bucket)[n] = *_highest;
721 738
              (*_next)[n] = _first[*_highest];
722 739
              if (_first[*_highest] != INVALID) {
723 740
                (*_prev)[_first[*_highest]] = n;
724 741
              } else {
725 742
                _last[*_highest] = n;
726 743
              }
727 744
              _first[*_highest] = n;
728 745
            }
729 746
          } else {
730 747

	
731 748
            deactivate(n);
732 749
            if (!(*_active)[_first[*_highest]]) {
733 750
              ++_highest;
734 751
              if (_highest != _sets.back().end() &&
735 752
                  !(*_active)[_first[*_highest]]) {
736 753
                _highest = _sets.back().end();
737 754
              }
738 755
            }
739 756
          }
740 757
        }
741 758

	
742 759
        if ((*_excess)[target] < _min_cut) {
743 760
          _min_cut = (*_excess)[target];
744 761
          for (NodeIt i(_graph); i != INVALID; ++i) {
745 762
            (*_min_cut_map)[i] = false;
746 763
          }
747 764
          for (std::list<int>::iterator it = _sets.back().begin();
748 765
               it != _sets.back().end(); ++it) {
749 766
            Node n = _first[*it];
750 767
            while (n != INVALID) {
751 768
              (*_min_cut_map)[n] = true;
752 769
              n = (*_next)[n];
753 770
            }
754 771
          }
755 772
        }
756 773

	
757 774
        {
758 775
          Node new_target;
759 776
          if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
760 777
            if ((*_next)[target] == INVALID) {
761 778
              _last[(*_bucket)[target]] = (*_prev)[target];
762 779
              new_target = (*_prev)[target];
763 780
            } else {
764 781
              (*_prev)[(*_next)[target]] = (*_prev)[target];
765 782
              new_target = (*_next)[target];
766 783
            }
767 784
            if ((*_prev)[target] == INVALID) {
768 785
              _first[(*_bucket)[target]] = (*_next)[target];
769 786
            } else {
770 787
              (*_next)[(*_prev)[target]] = (*_next)[target];
771 788
            }
772 789
          } else {
773 790
            _sets.back().pop_back();
774 791
            if (_sets.back().empty()) {
775 792
              _sets.pop_back();
776 793
              if (_sets.empty())
777 794
                break;
778 795
              for (std::list<int>::iterator it = _sets.back().begin();
779 796
                   it != _sets.back().end(); ++it) {
780 797
                _dormant[*it] = false;
781 798
              }
782 799
            }
783 800
            new_target = _last[_sets.back().back()];
784 801
          }
785 802

	
786 803
          (*_bucket)[target] = 0;
787 804

	
788 805
          (*_source_set)[target] = true;
789 806
          for (InArcIt a(_graph, target); a != INVALID; ++a) {
790 807
            Value rem = (*_capacity)[a] - (*_flow)[a];
791 808
            if (!_tolerance.positive(rem)) continue;
792 809
            Node v = _graph.source(a);
793 810
            if (!(*_active)[v] && !(*_source_set)[v]) {
794 811
              activate(v);
795 812
            }
796 813
            (*_excess)[v] += rem;
797 814
            (*_flow)[a] = (*_capacity)[a];
798 815
          }
799 816

	
800 817
          for (OutArcIt a(_graph, target); a != INVALID; ++a) {
801 818
            Value rem = (*_flow)[a];
802 819
            if (!_tolerance.positive(rem)) continue;
803 820
            Node v = _graph.target(a);
804 821
            if (!(*_active)[v] && !(*_source_set)[v]) {
805 822
              activate(v);
806 823
            }
807 824
            (*_excess)[v] += rem;
808 825
            (*_flow)[a] = 0;
809 826
          }
810 827

	
811 828
          target = new_target;
812 829
          if ((*_active)[target]) {
813 830
            deactivate(target);
814 831
          }
815 832

	
816 833
          _highest = _sets.back().begin();
817 834
          while (_highest != _sets.back().end() &&
818 835
                 !(*_active)[_first[*_highest]]) {
819 836
            ++_highest;
820 837
          }
821 838
        }
822 839
      }
823 840
    }
824 841

	
825 842
  public:
826 843

	
827 844
    /// \name Execution Control
828 845
    /// The simplest way to execute the algorithm is to use
829 846
    /// one of the member functions called \ref run().
830 847
    /// \n
831 848
    /// If you need better control on the execution,
832 849
    /// you have to call one of the \ref init() functions first, then
833 850
    /// \ref calculateOut() and/or \ref calculateIn().
834 851

	
835 852
    /// @{
836 853

	
837 854
    /// \brief Initialize the internal data structures.
838 855
    ///
839 856
    /// This function initializes the internal data structures. It creates
840 857
    /// the maps and some bucket structures for the algorithm.
841 858
    /// The first node is used as the source node for the push-relabel
842 859
    /// algorithm.
843 860
    void init() {
844 861
      init(NodeIt(_graph));
845 862
    }
846 863

	
847 864
    /// \brief Initialize the internal data structures.
848 865
    ///
849 866
    /// This function initializes the internal data structures. It creates
850 867
    /// the maps and some bucket structures for the algorithm. 
851 868
    /// The given node is used as the source node for the push-relabel
852 869
    /// algorithm.
853 870
    void init(const Node& source) {
854 871
      _source = source;
855 872

	
856 873
      _node_num = countNodes(_graph);
857 874

	
858 875
      _first.resize(_node_num);
859 876
      _last.resize(_node_num);
860 877

	
861 878
      _dormant.resize(_node_num);
862 879

	
863 880
      if (!_flow) {
864 881
        _flow = new FlowMap(_graph);
865 882
      }
866 883
      if (!_next) {
867 884
        _next = new typename Digraph::template NodeMap<Node>(_graph);
868 885
      }
869 886
      if (!_prev) {
870 887
        _prev = new typename Digraph::template NodeMap<Node>(_graph);
871 888
      }
872 889
      if (!_active) {
873 890
        _active = new typename Digraph::template NodeMap<bool>(_graph);
874 891
      }
875 892
      if (!_bucket) {
876 893
        _bucket = new typename Digraph::template NodeMap<int>(_graph);
877 894
      }
878 895
      if (!_excess) {
879 896
        _excess = new ExcessMap(_graph);
880 897
      }
881 898
      if (!_source_set) {
882 899
        _source_set = new SourceSetMap(_graph);
883 900
      }
884 901
      if (!_min_cut_map) {
885 902
        _min_cut_map = new MinCutMap(_graph);
886 903
      }
887 904

	
888 905
      _min_cut = std::numeric_limits<Value>::max();
889 906
    }
890 907

	
891 908

	
892 909
    /// \brief Calculate a minimum cut with \f$ source \f$ on the
893 910
    /// source-side.
894 911
    ///
895 912
    /// This function calculates a minimum cut with \f$ source \f$ on the
896 913
    /// source-side (i.e. a set \f$ X\subsetneq V \f$ with
897 914
    /// \f$ source \in X \f$ and minimal outgoing capacity).
898 915
    ///
899 916
    /// \pre \ref init() must be called before using this function.
900 917
    void calculateOut() {
901 918
      findMinCutOut();
902 919
    }
903 920

	
904 921
    /// \brief Calculate a minimum cut with \f$ source \f$ on the
905 922
    /// sink-side.
906 923
    ///
907 924
    /// This function calculates a minimum cut with \f$ source \f$ on the
908 925
    /// sink-side (i.e. a set \f$ X\subsetneq V \f$ with
909 926
    /// \f$ source \notin X \f$ and minimal outgoing capacity).
910 927
    ///
911 928
    /// \pre \ref init() must be called before using this function.
912 929
    void calculateIn() {
913 930
      findMinCutIn();
914 931
    }
915 932

	
916 933

	
917 934
    /// \brief Run the algorithm.
918 935
    ///
919 936
    /// This function runs the algorithm. It finds nodes \c source and
920 937
    /// \c target arbitrarily and then calls \ref init(), \ref calculateOut()
921 938
    /// and \ref calculateIn().
922 939
    void run() {
923 940
      init();
924 941
      calculateOut();
925 942
      calculateIn();
926 943
    }
927 944

	
928 945
    /// \brief Run the algorithm.
929 946
    ///
930 947
    /// This function runs the algorithm. It uses the given \c source node, 
931 948
    /// finds a proper \c target node and then calls the \ref init(),
932 949
    /// \ref calculateOut() and \ref calculateIn().
933 950
    void run(const Node& s) {
934 951
      init(s);
935 952
      calculateOut();
936 953
      calculateIn();
937 954
    }
938 955

	
939 956
    /// @}
940 957

	
941 958
    /// \name Query Functions
942 959
    /// The result of the %HaoOrlin algorithm
943 960
    /// can be obtained using these functions.\n
944 961
    /// \ref run(), \ref calculateOut() or \ref calculateIn() 
945 962
    /// should be called before using them.
946 963

	
947 964
    /// @{
948 965

	
949 966
    /// \brief Return the value of the minimum cut.
950 967
    ///
951 968
    /// This function returns the value of the minimum cut.
952 969
    ///
953 970
    /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
954 971
    /// must be called before using this function.
955 972
    Value minCutValue() const {
956 973
      return _min_cut;
957 974
    }
958 975

	
959 976

	
960 977
    /// \brief Return a minimum cut.
961 978
    ///
962 979
    /// This function sets \c cutMap to the characteristic vector of a
963 980
    /// minimum value cut: it will give a non-empty set \f$ X\subsetneq V \f$
964 981
    /// with minimal outgoing capacity (i.e. \c cutMap will be \c true exactly
965 982
    /// for the nodes of \f$ X \f$).
966 983
    ///
967 984
    /// \param cutMap A \ref concepts::WriteMap "writable" node map with
968 985
    /// \c bool (or convertible) value type.
969 986
    ///
970 987
    /// \return The value of the minimum cut.
971 988
    ///
972 989
    /// \pre \ref run(), \ref calculateOut() or \ref calculateIn() 
973 990
    /// must be called before using this function.
974 991
    template <typename CutMap>
975 992
    Value minCutMap(CutMap& cutMap) const {
976 993
      for (NodeIt it(_graph); it != INVALID; ++it) {
977 994
        cutMap.set(it, (*_min_cut_map)[it]);
978 995
      }
979 996
      return _min_cut;
980 997
    }
981 998

	
982 999
    /// @}
983 1000

	
984 1001
  }; //class HaoOrlin
985 1002

	
986 1003
} //namespace lemon
987 1004

	
988 1005
#endif //LEMON_HAO_ORLIN_H
0 comments (0 inline)