lemon/gomory_hu_tree.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 25 Feb 2009 11:10:52 +0000
changeset 532 ccd2d3a3001e
parent 531 924887566bf2
permissions -rw-r--r--
Cut iterators for GomoryHuTree + doc cleanup + bug fixes (#66)
tapolcai@531
     1
/* -*- C++ -*-
tapolcai@531
     2
 *
tapolcai@531
     3
 * This file is a part of LEMON, a generic C++ optimization library
tapolcai@531
     4
 *
tapolcai@531
     5
 * Copyright (C) 2003-2008
tapolcai@531
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
tapolcai@531
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
tapolcai@531
     8
 *
tapolcai@531
     9
 * Permission to use, modify and distribute this software is granted
tapolcai@531
    10
 * provided that this copyright notice appears in all copies. For
tapolcai@531
    11
 * precise terms see the accompanying LICENSE file.
tapolcai@531
    12
 *
tapolcai@531
    13
 * This software is provided "AS IS" with no warranty of any kind,
tapolcai@531
    14
 * express or implied, and with no claim as to its suitability for any
tapolcai@531
    15
 * purpose.
tapolcai@531
    16
 *
tapolcai@531
    17
 */
tapolcai@531
    18
tapolcai@531
    19
#ifndef LEMON_GOMORY_HU_TREE_H
tapolcai@531
    20
#define LEMON_GOMORY_HU_TREE_H
tapolcai@531
    21
tapolcai@531
    22
#include <limits>
tapolcai@531
    23
alpar@532
    24
#include <lemon/core.h>
tapolcai@531
    25
#include <lemon/preflow.h>
tapolcai@531
    26
#include <lemon/concept_check.h>
tapolcai@531
    27
#include <lemon/concepts/maps.h>
tapolcai@531
    28
tapolcai@531
    29
/// \ingroup min_cut
tapolcai@531
    30
/// \file 
tapolcai@531
    31
/// \brief Gomory-Hu cut tree in graphs.
tapolcai@531
    32
tapolcai@531
    33
namespace lemon {
tapolcai@531
    34
tapolcai@531
    35
  /// \ingroup min_cut
tapolcai@531
    36
  ///
tapolcai@531
    37
  /// \brief Gomory-Hu cut tree algorithm
tapolcai@531
    38
  ///
alpar@532
    39
  /// The Gomory-Hu tree is a tree on the node set of the graph, but it
alpar@532
    40
  /// may contain edges which are not in the original digraph. It has the
alpar@532
    41
  /// property that the minimum capacity edge of the path between two nodes 
alpar@532
    42
  /// in this tree has the same weight as the minimum cut in the digraph
alpar@532
    43
  /// between these nodes. Moreover the components obtained by removing
alpar@532
    44
  /// this edge from the tree determine the corresponding minimum cut.
alpar@532
    45
  ///
alpar@532
    46
  /// Therefore once this tree is computed, the minimum cut between any pair
alpar@532
    47
  /// of nodes can easily be obtained.
tapolcai@531
    48
  /// 
alpar@532
    49
  /// The algorithm calculates \e n-1 distinct minimum cuts (currently with
alpar@532
    50
  /// the \ref Preflow algorithm), therefore the algorithm has
tapolcai@531
    51
  /// \f$(O(n^3\sqrt{e})\f$ overall time complexity. It calculates a
alpar@532
    52
  /// rooted Gomory-Hu tree, its structure and the weights can be obtained
alpar@532
    53
  /// by \c predNode(), \c predValue() and \c rootDist().
alpar@532
    54
  /// 
alpar@532
    55
  /// The members \c minCutMap() and \c minCutValue() calculate
tapolcai@531
    56
  /// the minimum cut and the minimum cut value between any two node
alpar@532
    57
  /// in the digraph. You can also list (iterate on) the nodes and the
alpar@532
    58
  /// edges of the cuts using MinCutNodeIt and MinCutEdgeIt.
alpar@532
    59
  ///
alpar@532
    60
  /// \tparam GR The undirected graph data structure the algorithm will run on
alpar@532
    61
  /// \tparam CAP type of the EdgeMap describing the Edge capacities.
alpar@532
    62
  /// it is typename GR::template EdgeMap<int> by default.
alpar@532
    63
  template <typename GR,
alpar@532
    64
	    typename CAP = typename GR::template EdgeMap<int>
alpar@532
    65
            >
tapolcai@531
    66
  class GomoryHuTree {
tapolcai@531
    67
  public:
tapolcai@531
    68
tapolcai@531
    69
    /// The graph type
alpar@532
    70
    typedef GR Graph;
alpar@532
    71
    /// The type if the edge capacity map
alpar@532
    72
    typedef CAP Capacity;
tapolcai@531
    73
    /// The value type of capacities
tapolcai@531
    74
    typedef typename Capacity::Value Value;
tapolcai@531
    75
    
tapolcai@531
    76
  private:
tapolcai@531
    77
tapolcai@531
    78
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
tapolcai@531
    79
tapolcai@531
    80
    const Graph& _graph;
tapolcai@531
    81
    const Capacity& _capacity;
tapolcai@531
    82
tapolcai@531
    83
    Node _root;
tapolcai@531
    84
    typename Graph::template NodeMap<Node>* _pred;
tapolcai@531
    85
    typename Graph::template NodeMap<Value>* _weight;
tapolcai@531
    86
    typename Graph::template NodeMap<int>* _order;
tapolcai@531
    87
tapolcai@531
    88
    void createStructures() {
tapolcai@531
    89
      if (!_pred) {
tapolcai@531
    90
	_pred = new typename Graph::template NodeMap<Node>(_graph);
tapolcai@531
    91
      }
tapolcai@531
    92
      if (!_weight) {
tapolcai@531
    93
	_weight = new typename Graph::template NodeMap<Value>(_graph);
tapolcai@531
    94
      }
tapolcai@531
    95
      if (!_order) {
tapolcai@531
    96
	_order = new typename Graph::template NodeMap<int>(_graph);
tapolcai@531
    97
      }
tapolcai@531
    98
    }
tapolcai@531
    99
tapolcai@531
   100
    void destroyStructures() {
tapolcai@531
   101
      if (_pred) {
tapolcai@531
   102
	delete _pred;
tapolcai@531
   103
      }
tapolcai@531
   104
      if (_weight) {
tapolcai@531
   105
	delete _weight;
tapolcai@531
   106
      }
tapolcai@531
   107
      if (_order) {
tapolcai@531
   108
	delete _order;
tapolcai@531
   109
      }
tapolcai@531
   110
    }
tapolcai@531
   111
  
tapolcai@531
   112
  public:
tapolcai@531
   113
tapolcai@531
   114
    /// \brief Constructor
tapolcai@531
   115
    ///
tapolcai@531
   116
    /// Constructor
alpar@532
   117
    /// \param graph The graph the algorithm will run on.
tapolcai@531
   118
    /// \param capacity The capacity map.
tapolcai@531
   119
    GomoryHuTree(const Graph& graph, const Capacity& capacity) 
tapolcai@531
   120
      : _graph(graph), _capacity(capacity),
tapolcai@531
   121
	_pred(0), _weight(0), _order(0) 
tapolcai@531
   122
    {
tapolcai@531
   123
      checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
tapolcai@531
   124
    }
tapolcai@531
   125
tapolcai@531
   126
tapolcai@531
   127
    /// \brief Destructor
tapolcai@531
   128
    ///
tapolcai@531
   129
    /// Destructor
tapolcai@531
   130
    ~GomoryHuTree() {
tapolcai@531
   131
      destroyStructures();
tapolcai@531
   132
    }
tapolcai@531
   133
alpar@532
   134
    // \brief Initialize the internal data structures.
alpar@532
   135
    //
alpar@532
   136
    // This function initializes the internal data structures.
alpar@532
   137
    //
tapolcai@531
   138
    void init() {
tapolcai@531
   139
      createStructures();
tapolcai@531
   140
tapolcai@531
   141
      _root = NodeIt(_graph);
tapolcai@531
   142
      for (NodeIt n(_graph); n != INVALID; ++n) {
tapolcai@531
   143
	_pred->set(n, _root);
tapolcai@531
   144
	_order->set(n, -1);
tapolcai@531
   145
      }
tapolcai@531
   146
      _pred->set(_root, INVALID);
tapolcai@531
   147
      _weight->set(_root, std::numeric_limits<Value>::max()); 
tapolcai@531
   148
    }
tapolcai@531
   149
tapolcai@531
   150
alpar@532
   151
    // \brief Start the algorithm
alpar@532
   152
    //
alpar@532
   153
    // This function starts the algorithm.
alpar@532
   154
    //
alpar@532
   155
    // \pre \ref init() must be called before using this function.
alpar@532
   156
    //
tapolcai@531
   157
    void start() {
tapolcai@531
   158
      Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
tapolcai@531
   159
tapolcai@531
   160
      for (NodeIt n(_graph); n != INVALID; ++n) {
tapolcai@531
   161
	if (n == _root) continue;
tapolcai@531
   162
tapolcai@531
   163
	Node pn = (*_pred)[n];
tapolcai@531
   164
	fa.source(n);
tapolcai@531
   165
	fa.target(pn);
tapolcai@531
   166
tapolcai@531
   167
	fa.runMinCut();
tapolcai@531
   168
tapolcai@531
   169
	_weight->set(n, fa.flowValue());
tapolcai@531
   170
tapolcai@531
   171
	for (NodeIt nn(_graph); nn != INVALID; ++nn) {
tapolcai@531
   172
	  if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
tapolcai@531
   173
	    _pred->set(nn, n);
tapolcai@531
   174
	  }
tapolcai@531
   175
	}
tapolcai@531
   176
	if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
tapolcai@531
   177
	  _pred->set(n, (*_pred)[pn]);
tapolcai@531
   178
	  _pred->set(pn, n);
tapolcai@531
   179
	  _weight->set(n, (*_weight)[pn]);
tapolcai@531
   180
	  _weight->set(pn, fa.flowValue());	
tapolcai@531
   181
	}
tapolcai@531
   182
      }
tapolcai@531
   183
tapolcai@531
   184
      _order->set(_root, 0);
tapolcai@531
   185
      int index = 1;
tapolcai@531
   186
tapolcai@531
   187
      for (NodeIt n(_graph); n != INVALID; ++n) {
tapolcai@531
   188
	std::vector<Node> st;
tapolcai@531
   189
	Node nn = n;
tapolcai@531
   190
	while ((*_order)[nn] == -1) {
tapolcai@531
   191
	  st.push_back(nn);
tapolcai@531
   192
	  nn = (*_pred)[nn];
tapolcai@531
   193
	}
tapolcai@531
   194
	while (!st.empty()) {
tapolcai@531
   195
	  _order->set(st.back(), index++);
tapolcai@531
   196
	  st.pop_back();
tapolcai@531
   197
	}
tapolcai@531
   198
      }
tapolcai@531
   199
    }
tapolcai@531
   200
alpar@532
   201
    ///\name Execution Control
alpar@532
   202
 
alpar@532
   203
    ///@{
alpar@532
   204
alpar@532
   205
    /// \brief Run the Gomory-Hu algorithm.
tapolcai@531
   206
    ///
alpar@532
   207
    /// This function runs the Gomory-Hu algorithm.
tapolcai@531
   208
    void run() {
tapolcai@531
   209
      init();
tapolcai@531
   210
      start();
tapolcai@531
   211
    }
alpar@532
   212
    
alpar@532
   213
    /// @}
tapolcai@531
   214
alpar@532
   215
    ///\name Query Functions
alpar@532
   216
    ///The results of the algorithm can be obtained using these
alpar@532
   217
    ///functions.\n
alpar@532
   218
    ///The \ref run() "run()" should be called before using them.\n
alpar@532
   219
    ///See also MinCutNodeIt and MinCutEdgeIt
alpar@532
   220
alpar@532
   221
    ///@{
alpar@532
   222
alpar@532
   223
    /// \brief Return the predecessor node in the Gomory-Hu tree.
tapolcai@531
   224
    ///
alpar@532
   225
    /// This function returns the predecessor node in the Gomory-Hu tree.
alpar@532
   226
    /// If the node is
tapolcai@531
   227
    /// the root of the Gomory-Hu tree, then it returns \c INVALID.
tapolcai@531
   228
    Node predNode(const Node& node) {
tapolcai@531
   229
      return (*_pred)[node];
tapolcai@531
   230
    }
tapolcai@531
   231
alpar@532
   232
    /// \brief Return the distance from the root node in the Gomory-Hu tree.
alpar@532
   233
    ///
alpar@532
   234
    /// This function returns the distance of \c node from the root node
alpar@532
   235
    /// in the Gomory-Hu tree.
alpar@532
   236
    int rootDist(const Node& node) {
alpar@532
   237
      return (*_order)[node];
alpar@532
   238
    }
alpar@532
   239
alpar@532
   240
    /// \brief Return the weight of the predecessor edge in the
tapolcai@531
   241
    /// Gomory-Hu tree.
tapolcai@531
   242
    ///
alpar@532
   243
    /// This function returns the weight of the predecessor edge in the
alpar@532
   244
    /// Gomory-Hu tree.  If the node is the root, the result is undefined.
tapolcai@531
   245
    Value predValue(const Node& node) {
tapolcai@531
   246
      return (*_weight)[node];
tapolcai@531
   247
    }
tapolcai@531
   248
alpar@532
   249
    /// \brief Return the minimum cut value between two nodes
tapolcai@531
   250
    ///
alpar@532
   251
    /// This function returns the minimum cut value between two nodes. The
tapolcai@531
   252
    /// algorithm finds the nearest common ancestor in the Gomory-Hu
tapolcai@531
   253
    /// tree and calculates the minimum weight arc on the paths to
tapolcai@531
   254
    /// the ancestor.
tapolcai@531
   255
    Value minCutValue(const Node& s, const Node& t) const {
tapolcai@531
   256
      Node sn = s, tn = t;
tapolcai@531
   257
      Value value = std::numeric_limits<Value>::max();
tapolcai@531
   258
      
tapolcai@531
   259
      while (sn != tn) {
tapolcai@531
   260
	if ((*_order)[sn] < (*_order)[tn]) {
alpar@532
   261
	  if ((*_weight)[tn] <= value) value = (*_weight)[tn];
tapolcai@531
   262
	  tn = (*_pred)[tn];
tapolcai@531
   263
	} else {
alpar@532
   264
	  if ((*_weight)[sn] <= value) value = (*_weight)[sn];
tapolcai@531
   265
	  sn = (*_pred)[sn];
tapolcai@531
   266
	}
tapolcai@531
   267
      }
tapolcai@531
   268
      return value;
tapolcai@531
   269
    }
tapolcai@531
   270
alpar@532
   271
    /// \brief Return the minimum cut between two nodes
tapolcai@531
   272
    ///
alpar@532
   273
    /// This function returns the minimum cut between the nodes \c s and \c t
alpar@532
   274
    /// the \r cutMap parameter by setting the nodes in the component of
alpar@532
   275
    /// \c \s to true and the other nodes to false.
alpar@532
   276
    ///
alpar@532
   277
    /// The \c cutMap should be \ref concepts::ReadWriteMap
tapolcai@531
   278
    /// "ReadWriteMap".
alpar@532
   279
    ///
alpar@532
   280
    /// For higher level interfaces, see MinCutNodeIt and MinCutEdgeIt
tapolcai@531
   281
    template <typename CutMap>
alpar@532
   282
    Value minCutMap(const Node& s, ///< Base node
alpar@532
   283
                    const Node& t,
alpar@532
   284
                    ///< The node you want to separate from Node s.
alpar@532
   285
                    CutMap& cutMap
alpar@532
   286
                    ///< The cut will be return in this map.
alpar@532
   287
                    /// It must be a \c bool \ref concepts::ReadWriteMap
alpar@532
   288
                    /// "ReadWriteMap" on the graph nodes.
alpar@532
   289
                    ) const {
tapolcai@531
   290
      Node sn = s, tn = t;
alpar@532
   291
      bool s_root=false;
tapolcai@531
   292
      Node rn = INVALID;
tapolcai@531
   293
      Value value = std::numeric_limits<Value>::max();
tapolcai@531
   294
      
tapolcai@531
   295
      while (sn != tn) {
tapolcai@531
   296
	if ((*_order)[sn] < (*_order)[tn]) {
alpar@532
   297
	  if ((*_weight)[tn] <= value) {
tapolcai@531
   298
	    rn = tn;
alpar@532
   299
            s_root = false;
tapolcai@531
   300
	    value = (*_weight)[tn];
tapolcai@531
   301
	  }
tapolcai@531
   302
	  tn = (*_pred)[tn];
tapolcai@531
   303
	} else {
alpar@532
   304
	  if ((*_weight)[sn] <= value) {
tapolcai@531
   305
	    rn = sn;
alpar@532
   306
            s_root = true;
tapolcai@531
   307
	    value = (*_weight)[sn];
tapolcai@531
   308
	  }
tapolcai@531
   309
	  sn = (*_pred)[sn];
tapolcai@531
   310
	}
tapolcai@531
   311
      }
tapolcai@531
   312
tapolcai@531
   313
      typename Graph::template NodeMap<bool> reached(_graph, false);
tapolcai@531
   314
      reached.set(_root, true);
alpar@532
   315
      cutMap.set(_root, !s_root);
tapolcai@531
   316
      reached.set(rn, true);
alpar@532
   317
      cutMap.set(rn, s_root);
tapolcai@531
   318
alpar@532
   319
      std::vector<Node> st;
tapolcai@531
   320
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@532
   321
	st.clear();
alpar@532
   322
        Node nn = n;
tapolcai@531
   323
	while (!reached[nn]) {
tapolcai@531
   324
	  st.push_back(nn);
tapolcai@531
   325
	  nn = (*_pred)[nn];
tapolcai@531
   326
	}
tapolcai@531
   327
	while (!st.empty()) {
tapolcai@531
   328
	  cutMap.set(st.back(), cutMap[nn]);
tapolcai@531
   329
	  st.pop_back();
tapolcai@531
   330
	}
tapolcai@531
   331
      }
tapolcai@531
   332
      
tapolcai@531
   333
      return value;
tapolcai@531
   334
    }
tapolcai@531
   335
alpar@532
   336
    ///@}
alpar@532
   337
alpar@532
   338
    friend class MinCutNodeIt;
alpar@532
   339
alpar@532
   340
    /// Iterate on the nodes of a minimum cut
alpar@532
   341
    
alpar@532
   342
    /// This iterator class lists the nodes of a minimum cut found by
alpar@532
   343
    /// GomoryHuTree. Before using it, you must allocate a GomoryHuTree class,
alpar@532
   344
    /// and call its \ref GomoryHuTree::run() "run()" method.
alpar@532
   345
    ///
alpar@532
   346
    /// This example counts the nodes in the minimum cut separating \c s from
alpar@532
   347
    /// \c t.
alpar@532
   348
    /// \code
alpar@532
   349
    /// GomoruHuTree<Graph> gom(g, capacities);
alpar@532
   350
    /// gom.run();
alpar@532
   351
    /// int sum=0;
alpar@532
   352
    /// for(GomoruHuTree<Graph>::MinCutNodeIt n(gom,s,t);n!=INVALID;++n) ++sum;
alpar@532
   353
    /// \endcode
alpar@532
   354
    class MinCutNodeIt
alpar@532
   355
    {
alpar@532
   356
      bool _side;
alpar@532
   357
      typename Graph::NodeIt _node_it;
alpar@532
   358
      typename Graph::template NodeMap<bool> _cut;
alpar@532
   359
    public:
alpar@532
   360
      /// Constructor
alpar@532
   361
alpar@532
   362
      /// Constructor
alpar@532
   363
      ///
alpar@532
   364
      MinCutNodeIt(GomoryHuTree const &gomory,
alpar@532
   365
                   ///< The GomoryHuTree class. You must call its
alpar@532
   366
                   ///  run() method
alpar@532
   367
                   ///  before initializing this iterator
alpar@532
   368
                   const Node& s, ///< Base node
alpar@532
   369
                   const Node& t,
alpar@532
   370
                   ///< The node you want to separate from Node s.
alpar@532
   371
                   bool side=true
alpar@532
   372
                   ///< If it is \c true (default) then the iterator lists
alpar@532
   373
                   ///  the nodes of the component containing \c s,
alpar@532
   374
                   ///  otherwise it lists the other component.
alpar@532
   375
                   /// \note As the minimum cut is not always unique,
alpar@532
   376
                   /// \code
alpar@532
   377
                   /// MinCutNodeIt(gomory, s, t, true);
alpar@532
   378
                   /// \endcode
alpar@532
   379
                   /// and
alpar@532
   380
                   /// \code
alpar@532
   381
                   /// MinCutNodeIt(gomory, t, s, false);
alpar@532
   382
                   /// \endcode
alpar@532
   383
                   /// does not necessarily give the same set of nodes.
alpar@532
   384
                   /// However it is ensured that
alpar@532
   385
                   /// \code
alpar@532
   386
                   /// MinCutNodeIt(gomory, s, t, true);
alpar@532
   387
                   /// \endcode
alpar@532
   388
                   /// and
alpar@532
   389
                   /// \code
alpar@532
   390
                   /// MinCutNodeIt(gomory, s, t, false);
alpar@532
   391
                   /// \endcode
alpar@532
   392
                   /// together list each node exactly once.
alpar@532
   393
                   )
alpar@532
   394
        : _side(side), _cut(gomory._graph)
alpar@532
   395
      {
alpar@532
   396
        gomory.minCutMap(s,t,_cut);
alpar@532
   397
        for(_node_it=typename Graph::NodeIt(gomory._graph);
alpar@532
   398
            _node_it!=INVALID && _cut[_node_it]!=_side;
alpar@532
   399
            ++_node_it) {}
alpar@532
   400
      }
alpar@532
   401
      /// Conversion to Node
alpar@532
   402
alpar@532
   403
      /// Conversion to Node
alpar@532
   404
      ///
alpar@532
   405
      operator typename Graph::Node() const
alpar@532
   406
      {
alpar@532
   407
        return _node_it;
alpar@532
   408
      }
alpar@532
   409
      bool operator==(Invalid) { return _node_it==INVALID; }
alpar@532
   410
      bool operator!=(Invalid) { return _node_it!=INVALID; }
alpar@532
   411
      /// Next node
alpar@532
   412
alpar@532
   413
      /// Next node
alpar@532
   414
      ///
alpar@532
   415
      MinCutNodeIt &operator++()
alpar@532
   416
      {
alpar@532
   417
        for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
alpar@532
   418
        return *this;
alpar@532
   419
      }
alpar@532
   420
      /// Postfix incrementation
alpar@532
   421
alpar@532
   422
      /// Postfix incrementation
alpar@532
   423
      ///
alpar@532
   424
      /// \warning This incrementation
alpar@532
   425
      /// returns a \c Node, not a \ref MinCutNodeIt, as one may
alpar@532
   426
      /// expect.
alpar@532
   427
      typename Graph::Node operator++(int)
alpar@532
   428
      {
alpar@532
   429
        typename Graph::Node n=*this;
alpar@532
   430
        ++(*this);
alpar@532
   431
        return n;
alpar@532
   432
      }
alpar@532
   433
    };
alpar@532
   434
    
alpar@532
   435
    friend class MinCutEdgeIt;
alpar@532
   436
    
alpar@532
   437
    /// Iterate on the edges of a minimum cut
alpar@532
   438
    
alpar@532
   439
    /// This iterator class lists the edges of a minimum cut found by
alpar@532
   440
    /// GomoryHuTree. Before using it, you must allocate a GomoryHuTree class,
alpar@532
   441
    /// and call its \ref GomoryHuTree::run() "run()" method.
alpar@532
   442
    ///
alpar@532
   443
    /// This example computes the value of the minimum cut separating \c s from
alpar@532
   444
    /// \c t.
alpar@532
   445
    /// \code
alpar@532
   446
    /// GomoruHuTree<Graph> gom(g, capacities);
alpar@532
   447
    /// gom.run();
alpar@532
   448
    /// int value=0;
alpar@532
   449
    /// for(GomoruHuTree<Graph>::MinCutEdgeIt e(gom,s,t);e!=INVALID;++e)
alpar@532
   450
    ///   value+=capacities[e];
alpar@532
   451
    /// \endcode
alpar@532
   452
    /// the result will be the same as it is returned by
alpar@532
   453
    /// \ref GomoryHuTree::minCostValue() "gom.minCostValue(s,t)"
alpar@532
   454
    class MinCutEdgeIt
alpar@532
   455
    {
alpar@532
   456
      bool _side;
alpar@532
   457
      const Graph &_graph;
alpar@532
   458
      typename Graph::NodeIt _node_it;
alpar@532
   459
      typename Graph::OutArcIt _arc_it;
alpar@532
   460
      typename Graph::template NodeMap<bool> _cut;
alpar@532
   461
      void step()
alpar@532
   462
      {
alpar@532
   463
        ++_arc_it;
alpar@532
   464
        while(_node_it!=INVALID && _arc_it==INVALID)
alpar@532
   465
          {
alpar@532
   466
            for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
alpar@532
   467
            if(_node_it!=INVALID)
alpar@532
   468
              _arc_it=typename Graph::OutArcIt(_graph,_node_it);
alpar@532
   469
          }
alpar@532
   470
      }
alpar@532
   471
      
alpar@532
   472
    public:
alpar@532
   473
      MinCutEdgeIt(GomoryHuTree const &gomory,
alpar@532
   474
                   ///< The GomoryHuTree class. You must call its
alpar@532
   475
                   ///  run() method
alpar@532
   476
                   ///  before initializing this iterator
alpar@532
   477
                   const Node& s,  ///< Base node
alpar@532
   478
                   const Node& t,
alpar@532
   479
                   ///< The node you want to separate from Node s.
alpar@532
   480
                   bool side=true
alpar@532
   481
                   ///< If it is \c true (default) then the listed arcs
alpar@532
   482
                   ///  will be oriented from the
alpar@532
   483
                   ///  the nodes of the component containing \c s,
alpar@532
   484
                   ///  otherwise they will be oriented in the opposite
alpar@532
   485
                   ///  direction.
alpar@532
   486
                   )
alpar@532
   487
        : _graph(gomory._graph), _cut(_graph)
alpar@532
   488
      {
alpar@532
   489
        gomory.minCutMap(s,t,_cut);
alpar@532
   490
        if(!side)
alpar@532
   491
          for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
alpar@532
   492
            _cut[n]=!_cut[n];
alpar@532
   493
alpar@532
   494
        for(_node_it=typename Graph::NodeIt(_graph);
alpar@532
   495
            _node_it!=INVALID && !_cut[_node_it];
alpar@532
   496
            ++_node_it) {}
alpar@532
   497
        _arc_it = _node_it!=INVALID ?
alpar@532
   498
          typename Graph::OutArcIt(_graph,_node_it) : INVALID;
alpar@532
   499
        while(_node_it!=INVALID && _arc_it == INVALID)
alpar@532
   500
          {
alpar@532
   501
            for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
alpar@532
   502
            if(_node_it!=INVALID)
alpar@532
   503
              _arc_it= typename Graph::OutArcIt(_graph,_node_it);
alpar@532
   504
          }
alpar@532
   505
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
alpar@532
   506
      }
alpar@532
   507
      /// Conversion to Arc
alpar@532
   508
alpar@532
   509
      /// Conversion to Arc
alpar@532
   510
      ///
alpar@532
   511
      operator typename Graph::Arc() const
alpar@532
   512
      {
alpar@532
   513
        return _arc_it;
alpar@532
   514
      }
alpar@532
   515
      /// Conversion to Edge
alpar@532
   516
alpar@532
   517
      /// Conversion to Edge
alpar@532
   518
      ///
alpar@532
   519
      operator typename Graph::Edge() const
alpar@532
   520
      {
alpar@532
   521
        return _arc_it;
alpar@532
   522
      }
alpar@532
   523
      bool operator==(Invalid) { return _node_it==INVALID; }
alpar@532
   524
      bool operator!=(Invalid) { return _node_it!=INVALID; }
alpar@532
   525
      /// Next edge
alpar@532
   526
alpar@532
   527
      /// Next edge
alpar@532
   528
      ///
alpar@532
   529
      MinCutEdgeIt &operator++()
alpar@532
   530
      {
alpar@532
   531
        step();
alpar@532
   532
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
alpar@532
   533
        return *this;
alpar@532
   534
      }
alpar@532
   535
      /// Postfix incrementation
alpar@532
   536
      
alpar@532
   537
      /// Postfix incrementation
alpar@532
   538
      ///
alpar@532
   539
      /// \warning This incrementation
alpar@532
   540
      /// returns a \c Arc, not a \ref MinCutEdgeIt, as one may
alpar@532
   541
      /// expect.
alpar@532
   542
      typename Graph::Arc operator++(int)
alpar@532
   543
      {
alpar@532
   544
        typename Graph::Arc e=*this;
alpar@532
   545
        ++(*this);
alpar@532
   546
        return e;
alpar@532
   547
      }
alpar@532
   548
    };
alpar@532
   549
tapolcai@531
   550
  };
tapolcai@531
   551
tapolcai@531
   552
}
tapolcai@531
   553
tapolcai@531
   554
#endif