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 768 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;
0 comments (0 inline)