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