gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 4 0
merge default
0 files changed with 25 insertions and 11 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -405,109 +405,111 @@
405 405
      _supply = ↦
406 406
      return *this;
407 407
    }
408 408

	
409 409
    /// \brief Sets the flow map.
410 410
    ///
411 411
    /// Sets the flow map.
412 412
    /// If you don't use this function before calling \ref run() or
413 413
    /// \ref init(), an instance will be allocated automatically.
414 414
    /// The destructor deallocates this automatically allocated map,
415 415
    /// of course.
416 416
    /// \return <tt>(*this)</tt>
417 417
    Circulation& flowMap(FlowMap& map) {
418 418
      if (_local_flow) {
419 419
        delete _flow;
420 420
        _local_flow = false;
421 421
      }
422 422
      _flow = &map;
423 423
      return *this;
424 424
    }
425 425

	
426 426
    /// \brief Sets the elevator used by algorithm.
427 427
    ///
428 428
    /// Sets the elevator used by algorithm.
429 429
    /// If you don't use this function before calling \ref run() or
430 430
    /// \ref init(), an instance will be allocated automatically.
431 431
    /// The destructor deallocates this automatically allocated elevator,
432 432
    /// of course.
433 433
    /// \return <tt>(*this)</tt>
434 434
    Circulation& elevator(Elevator& elevator) {
435 435
      if (_local_level) {
436 436
        delete _level;
437 437
        _local_level = false;
438 438
      }
439 439
      _level = &elevator;
440 440
      return *this;
441 441
    }
442 442

	
443 443
    /// \brief Returns a const reference to the elevator.
444 444
    ///
445 445
    /// Returns a const reference to the elevator.
446 446
    ///
447 447
    /// \pre Either \ref run() or \ref init() must be called before
448 448
    /// using this function.
449 449
    const Elevator& elevator() const {
450 450
      return *_level;
451 451
    }
452 452

	
453
    /// \brief Sets the tolerance used by algorithm.
453
    /// \brief Sets the tolerance used by the algorithm.
454 454
    ///
455
    /// Sets the tolerance used by algorithm.
456
    Circulation& tolerance(const Tolerance& tolerance) const {
455
    /// Sets the tolerance object used by the algorithm.
456
    /// \return <tt>(*this)</tt>
457
    Circulation& tolerance(const Tolerance& tolerance) {
457 458
      _tol = tolerance;
458 459
      return *this;
459 460
    }
460 461

	
461 462
    /// \brief Returns a const reference to the tolerance.
462 463
    ///
463
    /// Returns a const reference to the tolerance.
464
    /// Returns a const reference to the tolerance object used by
465
    /// the algorithm.
464 466
    const Tolerance& tolerance() const {
465
      return tolerance;
467
      return _tol;
466 468
    }
467 469

	
468 470
    /// \name Execution Control
469 471
    /// The simplest way to execute the algorithm is to call \ref run().\n
470 472
    /// If you need more control on the initial solution or the execution,
471 473
    /// first you have to call one of the \ref init() functions, then
472 474
    /// the \ref start() function.
473 475

	
474 476
    ///@{
475 477

	
476 478
    /// Initializes the internal data structures.
477 479

	
478 480
    /// Initializes the internal data structures and sets all flow values
479 481
    /// to the lower bound.
480 482
    void init()
481 483
    {
482 484
      LEMON_DEBUG(checkBoundMaps(),
483 485
        "Upper bounds must be greater or equal to the lower bounds");
484 486

	
485 487
      createStructures();
486 488

	
487 489
      for(NodeIt n(_g);n!=INVALID;++n) {
488 490
        (*_excess)[n] = (*_supply)[n];
489 491
      }
490 492

	
491 493
      for (ArcIt e(_g);e!=INVALID;++e) {
492 494
        _flow->set(e, (*_lo)[e]);
493 495
        (*_excess)[_g.target(e)] += (*_flow)[e];
494 496
        (*_excess)[_g.source(e)] -= (*_flow)[e];
495 497
      }
496 498

	
497 499
      // global relabeling tested, but in general case it provides
498 500
      // worse performance for random digraphs
499 501
      _level->initStart();
500 502
      for(NodeIt n(_g);n!=INVALID;++n)
501 503
        _level->initAddItem(n);
502 504
      _level->initFinish();
503 505
      for(NodeIt n(_g);n!=INVALID;++n)
504 506
        if(_tol.positive((*_excess)[n]))
505 507
          _level->activate(n);
506 508
    }
507 509

	
508 510
    /// Initializes the internal data structures using a greedy approach.
509 511

	
510 512
    /// Initializes the internal data structures using a greedy approach
511 513
    /// to construct the initial solution.
512 514
    void greedyInit()
513 515
    {
Ignore white space 6 line context
... ...
@@ -52,97 +52,97 @@
52 52
    ///
53 53
    /// The type of the map that stores the flow values.
54 54
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
55 55
    typedef typename Digraph::template ArcMap<Value> FlowMap;
56 56

	
57 57
    /// \brief Instantiates a FlowMap.
58 58
    ///
59 59
    /// This function instantiates a \ref FlowMap.
60 60
    /// \param digraph The digraph for which we would like to define
61 61
    /// the flow map.
62 62
    static FlowMap* createFlowMap(const Digraph& digraph) {
63 63
      return new FlowMap(digraph);
64 64
    }
65 65

	
66 66
    /// \brief The elevator type used by Preflow algorithm.
67 67
    ///
68 68
    /// The elevator type used by Preflow algorithm.
69 69
    ///
70 70
    /// \sa Elevator
71 71
    /// \sa LinkedElevator
72 72
    typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator;
73 73

	
74 74
    /// \brief Instantiates an Elevator.
75 75
    ///
76 76
    /// This function instantiates an \ref Elevator.
77 77
    /// \param digraph The digraph for which we would like to define
78 78
    /// the elevator.
79 79
    /// \param max_level The maximum level of the elevator.
80 80
    static Elevator* createElevator(const Digraph& digraph, int max_level) {
81 81
      return new Elevator(digraph, max_level);
82 82
    }
83 83

	
84 84
    /// \brief The tolerance used by the algorithm
85 85
    ///
86 86
    /// The tolerance used by the algorithm to handle inexact computation.
87 87
    typedef lemon::Tolerance<Value> Tolerance;
88 88

	
89 89
  };
90 90

	
91 91

	
92 92
  /// \ingroup max_flow
93 93
  ///
94 94
  /// \brief %Preflow algorithm class.
95 95
  ///
96 96
  /// This class provides an implementation of Goldberg-Tarjan's \e preflow
97 97
  /// \e push-relabel algorithm producing a \ref max_flow
98 98
  /// "flow of maximum value" in a digraph.
99 99
  /// The preflow algorithms are the fastest known maximum
100
  /// flow algorithms. The current implementation use a mixture of the
100
  /// flow algorithms. The current implementation uses a mixture of the
101 101
  /// \e "highest label" and the \e "bound decrease" heuristics.
102 102
  /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
103 103
  ///
104 104
  /// The algorithm consists of two phases. After the first phase
105 105
  /// the maximum flow value and the minimum cut is obtained. The
106 106
  /// second phase constructs a feasible maximum flow on each arc.
107 107
  ///
108 108
  /// \tparam GR The type of the digraph the algorithm runs on.
109 109
  /// \tparam CAP The type of the capacity map. The default map
110 110
  /// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
111 111
#ifdef DOXYGEN
112 112
  template <typename GR, typename CAP, typename TR>
113 113
#else
114 114
  template <typename GR,
115 115
            typename CAP = typename GR::template ArcMap<int>,
116 116
            typename TR = PreflowDefaultTraits<GR, CAP> >
117 117
#endif
118 118
  class Preflow {
119 119
  public:
120 120

	
121 121
    ///The \ref PreflowDefaultTraits "traits class" of the algorithm.
122 122
    typedef TR Traits;
123 123
    ///The type of the digraph the algorithm runs on.
124 124
    typedef typename Traits::Digraph Digraph;
125 125
    ///The type of the capacity map.
126 126
    typedef typename Traits::CapacityMap CapacityMap;
127 127
    ///The type of the flow values.
128 128
    typedef typename Traits::Value Value;
129 129

	
130 130
    ///The type of the flow map.
131 131
    typedef typename Traits::FlowMap FlowMap;
132 132
    ///The type of the elevator.
133 133
    typedef typename Traits::Elevator Elevator;
134 134
    ///The type of the tolerance.
135 135
    typedef typename Traits::Tolerance Tolerance;
136 136

	
137 137
  private:
138 138

	
139 139
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
140 140

	
141 141
    const Digraph& _graph;
142 142
    const CapacityMap* _capacity;
143 143

	
144 144
    int _node_num;
145 145

	
146 146
    Node _source, _target;
147 147

	
148 148
    FlowMap* _flow;
... ...
@@ -326,109 +326,111 @@
326 326
      return *this;
327 327
    }
328 328

	
329 329
    /// \brief Sets the source node.
330 330
    ///
331 331
    /// Sets the source node.
332 332
    /// \return <tt>(*this)</tt>
333 333
    Preflow& source(const Node& node) {
334 334
      _source = node;
335 335
      return *this;
336 336
    }
337 337

	
338 338
    /// \brief Sets the target node.
339 339
    ///
340 340
    /// Sets the target node.
341 341
    /// \return <tt>(*this)</tt>
342 342
    Preflow& target(const Node& node) {
343 343
      _target = node;
344 344
      return *this;
345 345
    }
346 346

	
347 347
    /// \brief Sets the elevator used by algorithm.
348 348
    ///
349 349
    /// Sets the elevator used by algorithm.
350 350
    /// If you don't use this function before calling \ref run() or
351 351
    /// \ref init(), an instance will be allocated automatically.
352 352
    /// The destructor deallocates this automatically allocated elevator,
353 353
    /// of course.
354 354
    /// \return <tt>(*this)</tt>
355 355
    Preflow& elevator(Elevator& elevator) {
356 356
      if (_local_level) {
357 357
        delete _level;
358 358
        _local_level = false;
359 359
      }
360 360
      _level = &elevator;
361 361
      return *this;
362 362
    }
363 363

	
364 364
    /// \brief Returns a const reference to the elevator.
365 365
    ///
366 366
    /// Returns a const reference to the elevator.
367 367
    ///
368 368
    /// \pre Either \ref run() or \ref init() must be called before
369 369
    /// using this function.
370 370
    const Elevator& elevator() const {
371 371
      return *_level;
372 372
    }
373 373

	
374
    /// \brief Sets the tolerance used by algorithm.
374
    /// \brief Sets the tolerance used by the algorithm.
375 375
    ///
376
    /// Sets the tolerance used by algorithm.
377
    Preflow& tolerance(const Tolerance& tolerance) const {
376
    /// Sets the tolerance object used by the algorithm.
377
    /// \return <tt>(*this)</tt>
378
    Preflow& tolerance(const Tolerance& tolerance) {
378 379
      _tolerance = tolerance;
379 380
      return *this;
380 381
    }
381 382

	
382 383
    /// \brief Returns a const reference to the tolerance.
383 384
    ///
384
    /// Returns a const reference to the tolerance.
385
    /// Returns a const reference to the tolerance object used by
386
    /// the algorithm.
385 387
    const Tolerance& tolerance() const {
386
      return tolerance;
388
      return _tolerance;
387 389
    }
388 390

	
389 391
    /// \name Execution Control
390 392
    /// The simplest way to execute the preflow algorithm is to use
391 393
    /// \ref run() or \ref runMinCut().\n
392 394
    /// If you need more control on the initial solution or the execution,
393 395
    /// first you have to call one of the \ref init() functions, then
394 396
    /// \ref startFirstPhase() and if you need it \ref startSecondPhase().
395 397

	
396 398
    ///@{
397 399

	
398 400
    /// \brief Initializes the internal data structures.
399 401
    ///
400 402
    /// Initializes the internal data structures and sets the initial
401 403
    /// flow to zero on each arc.
402 404
    void init() {
403 405
      createStructures();
404 406

	
405 407
      _phase = true;
406 408
      for (NodeIt n(_graph); n != INVALID; ++n) {
407 409
        (*_excess)[n] = 0;
408 410
      }
409 411

	
410 412
      for (ArcIt e(_graph); e != INVALID; ++e) {
411 413
        _flow->set(e, 0);
412 414
      }
413 415

	
414 416
      typename Digraph::template NodeMap<bool> reached(_graph, false);
415 417

	
416 418
      _level->initStart();
417 419
      _level->initAddItem(_target);
418 420

	
419 421
      std::vector<Node> queue;
420 422
      reached[_source] = true;
421 423

	
422 424
      queue.push_back(_target);
423 425
      reached[_target] = true;
424 426
      while (!queue.empty()) {
425 427
        _level->initNewLevel();
426 428
        std::vector<Node> nqueue;
427 429
        for (int i = 0; i < int(queue.size()); ++i) {
428 430
          Node n = queue[i];
429 431
          for (InArcIt e(_graph, n); e != INVALID; ++e) {
430 432
            Node u = _graph.source(e);
431 433
            if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
432 434
              reached[u] = true;
433 435
              _level->initAddItem(u);
434 436
              nqueue.push_back(u);
Ignore white space 6 line context
... ...
@@ -42,96 +42,101 @@
42 42
  "0 2  2  6\n"
43 43
  "1 3  4  7\n"
44 44
  "1 4  0  5\n"
45 45
  "2 4  1  3\n"
46 46
  "3 5  3  8\n"
47 47
  "4 5  3  7\n"
48 48
  "@attributes\n"
49 49
  "source 0\n"
50 50
  "sink   5\n";
51 51

	
52 52
void checkCirculationCompile()
53 53
{
54 54
  typedef int VType;
55 55
  typedef concepts::Digraph Digraph;
56 56

	
57 57
  typedef Digraph::Node Node;
58 58
  typedef Digraph::Arc Arc;
59 59
  typedef concepts::ReadMap<Arc,VType> CapMap;
60 60
  typedef concepts::ReadMap<Node,VType> SupplyMap;
61 61
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
62 62
  typedef concepts::WriteMap<Node,bool> BarrierMap;
63 63

	
64 64
  typedef Elevator<Digraph, Digraph::Node> Elev;
65 65
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
66 66

	
67 67
  Digraph g;
68 68
  Node n;
69 69
  Arc a;
70 70
  CapMap lcap, ucap;
71 71
  SupplyMap supply;
72 72
  FlowMap flow;
73 73
  BarrierMap bar;
74 74
  VType v;
75 75
  bool b;
76 76

	
77 77
  typedef Circulation<Digraph, CapMap, CapMap, SupplyMap>
78 78
            ::SetFlowMap<FlowMap>
79 79
            ::SetElevator<Elev>
80 80
            ::SetStandardElevator<LinkedElev>
81 81
            ::Create CirculationType;
82 82
  CirculationType circ_test(g, lcap, ucap, supply);
83 83
  const CirculationType& const_circ_test = circ_test;
84 84
   
85 85
  circ_test
86 86
    .lowerMap(lcap)
87 87
    .upperMap(ucap)
88 88
    .supplyMap(supply)
89 89
    .flowMap(flow);
90
  
91
  const CirculationType::Elevator& elev = const_circ_test.elevator();
92
  circ_test.elevator(const_cast<CirculationType::Elevator&>(elev));
93
  CirculationType::Tolerance tol = const_circ_test.tolerance();
94
  circ_test.tolerance(tol);
90 95

	
91 96
  circ_test.init();
92 97
  circ_test.greedyInit();
93 98
  circ_test.start();
94 99
  circ_test.run();
95 100

	
96 101
  v = const_circ_test.flow(a);
97 102
  const FlowMap& fm = const_circ_test.flowMap();
98 103
  b = const_circ_test.barrier(n);
99 104
  const_circ_test.barrierMap(bar);
100 105
  
101 106
  ignore_unused_variable_warning(fm);
102 107
}
103 108

	
104 109
template <class G, class LM, class UM, class DM>
105 110
void checkCirculation(const G& g, const LM& lm, const UM& um,
106 111
                      const DM& dm, bool find)
107 112
{
108 113
  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
109 114
  bool ret = circ.run();
110 115
  if (find) {
111 116
    check(ret, "A feasible solution should have been found.");
112 117
    check(circ.checkFlow(), "The found flow is corrupt.");
113 118
    check(!circ.checkBarrier(), "A barrier should not have been found.");
114 119
  } else {
115 120
    check(!ret, "A feasible solution should not have been found.");
116 121
    check(circ.checkBarrier(), "The found barrier is corrupt.");
117 122
  }
118 123
}
119 124

	
120 125
int main (int, char*[])
121 126
{
122 127
  typedef ListDigraph Digraph;
123 128
  DIGRAPH_TYPEDEFS(Digraph);
124 129

	
125 130
  Digraph g;
126 131
  IntArcMap lo(g), up(g);
127 132
  IntNodeMap delta(g, 0);
128 133
  Node s, t;
129 134

	
130 135
  std::istringstream input(test_lgf);
131 136
  DigraphReader<Digraph>(g,input).
132 137
    arcMap("lcap", lo).
133 138
    arcMap("ucap", up).
134 139
    node("source",s).
135 140
    node("sink",t).
136 141
    run();
137 142

	
Ignore white space 6 line context
... ...
@@ -49,96 +49,101 @@
49 49
  "1 2 3     8\n"
50 50
  "1 3 4     8\n"
51 51
  "2 5 5     5\n"
52 52
  "3 2 6     5\n"
53 53
  "3 5 7     5\n"
54 54
  "3 6 8     5\n"
55 55
  "4 3 9     3\n"
56 56
  "5 7 10    3\n"
57 57
  "5 6 11    10\n"
58 58
  "5 8 12    10\n"
59 59
  "6 8 13    8\n"
60 60
  "8 9 14    20\n"
61 61
  "8 1 15    5\n"
62 62
  "9 5 16    5\n"
63 63
  "@attributes\n"
64 64
  "source 1\n"
65 65
  "target 8\n";
66 66

	
67 67
void checkPreflowCompile()
68 68
{
69 69
  typedef int VType;
70 70
  typedef concepts::Digraph Digraph;
71 71

	
72 72
  typedef Digraph::Node Node;
73 73
  typedef Digraph::Arc Arc;
74 74
  typedef concepts::ReadMap<Arc,VType> CapMap;
75 75
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
76 76
  typedef concepts::WriteMap<Node,bool> CutMap;
77 77

	
78 78
  typedef Elevator<Digraph, Digraph::Node> Elev;
79 79
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
80 80

	
81 81
  Digraph g;
82 82
  Node n;
83 83
  Arc e;
84 84
  CapMap cap;
85 85
  FlowMap flow;
86 86
  CutMap cut;
87 87
  VType v;
88 88
  bool b;
89 89

	
90 90
  typedef Preflow<Digraph, CapMap>
91 91
            ::SetFlowMap<FlowMap>
92 92
            ::SetElevator<Elev>
93 93
            ::SetStandardElevator<LinkedElev>
94 94
            ::Create PreflowType;
95 95
  PreflowType preflow_test(g, cap, n, n);
96 96
  const PreflowType& const_preflow_test = preflow_test;
97
  
98
  const PreflowType::Elevator& elev = const_preflow_test.elevator();
99
  preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev));
100
  PreflowType::Tolerance tol = const_preflow_test.tolerance();
101
  preflow_test.tolerance(tol);
97 102

	
98 103
  preflow_test
99 104
    .capacityMap(cap)
100 105
    .flowMap(flow)
101 106
    .source(n)
102 107
    .target(n);
103 108

	
104 109
  preflow_test.init();
105 110
  preflow_test.init(cap);
106 111
  preflow_test.startFirstPhase();
107 112
  preflow_test.startSecondPhase();
108 113
  preflow_test.run();
109 114
  preflow_test.runMinCut();
110 115

	
111 116
  v = const_preflow_test.flowValue();
112 117
  v = const_preflow_test.flow(e);
113 118
  const FlowMap& fm = const_preflow_test.flowMap();
114 119
  b = const_preflow_test.minCut(n);
115 120
  const_preflow_test.minCutMap(cut);
116 121
  
117 122
  ignore_unused_variable_warning(fm);
118 123
}
119 124

	
120 125
int cutValue (const SmartDigraph& g,
121 126
              const SmartDigraph::NodeMap<bool>& cut,
122 127
              const SmartDigraph::ArcMap<int>& cap) {
123 128

	
124 129
  int c=0;
125 130
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
126 131
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
127 132
  }
128 133
  return c;
129 134
}
130 135

	
131 136
bool checkFlow(const SmartDigraph& g,
132 137
               const SmartDigraph::ArcMap<int>& flow,
133 138
               const SmartDigraph::ArcMap<int>& cap,
134 139
               SmartDigraph::Node s, SmartDigraph::Node t) {
135 140

	
136 141
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
137 142
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
138 143
  }
139 144

	
140 145
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
141 146
    if (n == s || n == t) continue;
142 147
    int sum = 0;
143 148
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
144 149
      sum += flow[e];
0 comments (0 inline)