lemon/gomory_hu.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 14 Apr 2009 10:35:38 +0200
changeset 573 aa1804409f29
parent 534 d6b40ebb2617
child 588 293551ad254f
permissions -rw-r--r--
Exploit that the standard maps are reference maps (#190)
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
  ///
kpeter@534
    39
  /// The Gomory-Hu tree is a tree on the node set of a given graph, but it
kpeter@534
    40
  /// may contain edges which are not in the original graph. It has the
alpar@532
    41
  /// property that the minimum capacity edge of the path between two nodes 
kpeter@534
    42
  /// in this tree has the same weight as the minimum cut in the graph
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
kpeter@534
    56
  /// the minimum cut and the minimum cut value between any two nodes
kpeter@534
    57
  /// in the graph. You can also list (iterate on) the nodes and the
kpeter@534
    58
  /// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt.
alpar@532
    59
  ///
kpeter@534
    60
  /// \tparam GR The type of the undirected graph the algorithm runs on.
kpeter@534
    61
  /// \tparam CAP The type of the edge map describing the edge capacities.
kpeter@534
    62
  /// It is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>" by default.
kpeter@534
    63
#ifdef DOXYGEN
alpar@532
    64
  template <typename GR,
kpeter@534
    65
	    typename CAP>
kpeter@534
    66
#else
kpeter@534
    67
  template <typename GR,
kpeter@534
    68
	    typename CAP = typename GR::template EdgeMap<int> >
kpeter@534
    69
#endif
alpar@533
    70
  class GomoryHu {
tapolcai@531
    71
  public:
tapolcai@531
    72
tapolcai@531
    73
    /// The graph type
alpar@532
    74
    typedef GR Graph;
kpeter@534
    75
    /// The type of the edge capacity map
alpar@532
    76
    typedef CAP Capacity;
tapolcai@531
    77
    /// The value type of capacities
tapolcai@531
    78
    typedef typename Capacity::Value Value;
tapolcai@531
    79
    
tapolcai@531
    80
  private:
tapolcai@531
    81
tapolcai@531
    82
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
tapolcai@531
    83
tapolcai@531
    84
    const Graph& _graph;
tapolcai@531
    85
    const Capacity& _capacity;
tapolcai@531
    86
tapolcai@531
    87
    Node _root;
tapolcai@531
    88
    typename Graph::template NodeMap<Node>* _pred;
tapolcai@531
    89
    typename Graph::template NodeMap<Value>* _weight;
tapolcai@531
    90
    typename Graph::template NodeMap<int>* _order;
tapolcai@531
    91
tapolcai@531
    92
    void createStructures() {
tapolcai@531
    93
      if (!_pred) {
tapolcai@531
    94
	_pred = new typename Graph::template NodeMap<Node>(_graph);
tapolcai@531
    95
      }
tapolcai@531
    96
      if (!_weight) {
tapolcai@531
    97
	_weight = new typename Graph::template NodeMap<Value>(_graph);
tapolcai@531
    98
      }
tapolcai@531
    99
      if (!_order) {
tapolcai@531
   100
	_order = new typename Graph::template NodeMap<int>(_graph);
tapolcai@531
   101
      }
tapolcai@531
   102
    }
tapolcai@531
   103
tapolcai@531
   104
    void destroyStructures() {
tapolcai@531
   105
      if (_pred) {
tapolcai@531
   106
	delete _pred;
tapolcai@531
   107
      }
tapolcai@531
   108
      if (_weight) {
tapolcai@531
   109
	delete _weight;
tapolcai@531
   110
      }
tapolcai@531
   111
      if (_order) {
tapolcai@531
   112
	delete _order;
tapolcai@531
   113
      }
tapolcai@531
   114
    }
tapolcai@531
   115
  
tapolcai@531
   116
  public:
tapolcai@531
   117
tapolcai@531
   118
    /// \brief Constructor
tapolcai@531
   119
    ///
tapolcai@531
   120
    /// Constructor
kpeter@534
   121
    /// \param graph The undirected graph the algorithm runs on.
kpeter@534
   122
    /// \param capacity The edge capacity map.
alpar@533
   123
    GomoryHu(const Graph& graph, const Capacity& capacity) 
tapolcai@531
   124
      : _graph(graph), _capacity(capacity),
tapolcai@531
   125
	_pred(0), _weight(0), _order(0) 
tapolcai@531
   126
    {
tapolcai@531
   127
      checkConcept<concepts::ReadMap<Edge, Value>, Capacity>();
tapolcai@531
   128
    }
tapolcai@531
   129
tapolcai@531
   130
tapolcai@531
   131
    /// \brief Destructor
tapolcai@531
   132
    ///
tapolcai@531
   133
    /// Destructor
alpar@533
   134
    ~GomoryHu() {
tapolcai@531
   135
      destroyStructures();
tapolcai@531
   136
    }
tapolcai@531
   137
kpeter@534
   138
  private:
kpeter@534
   139
  
kpeter@534
   140
    // Initialize the internal data structures
tapolcai@531
   141
    void init() {
tapolcai@531
   142
      createStructures();
tapolcai@531
   143
tapolcai@531
   144
      _root = NodeIt(_graph);
tapolcai@531
   145
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@573
   146
        (*_pred)[n] = _root;
kpeter@573
   147
        (*_order)[n] = -1;
tapolcai@531
   148
      }
kpeter@573
   149
      (*_pred)[_root] = INVALID;
kpeter@573
   150
      (*_weight)[_root] = std::numeric_limits<Value>::max(); 
tapolcai@531
   151
    }
tapolcai@531
   152
tapolcai@531
   153
kpeter@534
   154
    // Start the algorithm
tapolcai@531
   155
    void start() {
tapolcai@531
   156
      Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID);
tapolcai@531
   157
tapolcai@531
   158
      for (NodeIt n(_graph); n != INVALID; ++n) {
tapolcai@531
   159
	if (n == _root) continue;
tapolcai@531
   160
tapolcai@531
   161
	Node pn = (*_pred)[n];
tapolcai@531
   162
	fa.source(n);
tapolcai@531
   163
	fa.target(pn);
tapolcai@531
   164
tapolcai@531
   165
	fa.runMinCut();
tapolcai@531
   166
kpeter@573
   167
	(*_weight)[n] = fa.flowValue();
tapolcai@531
   168
tapolcai@531
   169
	for (NodeIt nn(_graph); nn != INVALID; ++nn) {
tapolcai@531
   170
	  if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
kpeter@573
   171
	    (*_pred)[nn] = n;
tapolcai@531
   172
	  }
tapolcai@531
   173
	}
tapolcai@531
   174
	if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
kpeter@573
   175
	  (*_pred)[n] = (*_pred)[pn];
kpeter@573
   176
	  (*_pred)[pn] = n;
kpeter@573
   177
	  (*_weight)[n] = (*_weight)[pn];
kpeter@573
   178
	  (*_weight)[pn] = fa.flowValue();
tapolcai@531
   179
	}
tapolcai@531
   180
      }
tapolcai@531
   181
kpeter@573
   182
      (*_order)[_root] = 0;
tapolcai@531
   183
      int index = 1;
tapolcai@531
   184
tapolcai@531
   185
      for (NodeIt n(_graph); n != INVALID; ++n) {
tapolcai@531
   186
	std::vector<Node> st;
tapolcai@531
   187
	Node nn = n;
tapolcai@531
   188
	while ((*_order)[nn] == -1) {
tapolcai@531
   189
	  st.push_back(nn);
tapolcai@531
   190
	  nn = (*_pred)[nn];
tapolcai@531
   191
	}
tapolcai@531
   192
	while (!st.empty()) {
kpeter@573
   193
	  (*_order)[st.back()] = index++;
tapolcai@531
   194
	  st.pop_back();
tapolcai@531
   195
	}
tapolcai@531
   196
      }
tapolcai@531
   197
    }
tapolcai@531
   198
kpeter@534
   199
  public:
kpeter@534
   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
kpeter@534
   218
    ///\ref run() "run()" should be called before using them.\n
kpeter@534
   219
    ///See also \ref MinCutNodeIt and \ref 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
kpeter@534
   253
    /// tree and calculates the minimum weight edge 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
kpeter@534
   274
    /// in the \c cutMap parameter by setting the nodes in the component of
kpeter@534
   275
    /// \c s to \c true and the other nodes to \c false.
alpar@532
   276
    ///
kpeter@534
   277
    /// For higher level interfaces, see MinCutNodeIt and MinCutEdgeIt.
tapolcai@531
   278
    template <typename CutMap>
kpeter@534
   279
    Value minCutMap(const Node& s, ///< The base node.
alpar@532
   280
                    const Node& t,
kpeter@534
   281
                    ///< The node you want to separate from node \c s.
alpar@532
   282
                    CutMap& cutMap
kpeter@534
   283
                    ///< The cut will be returned in this map.
kpeter@534
   284
                    /// It must be a \c bool (or convertible) 
kpeter@534
   285
                    /// \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@534
   286
                    /// on the graph nodes.
alpar@532
   287
                    ) const {
tapolcai@531
   288
      Node sn = s, tn = t;
alpar@532
   289
      bool s_root=false;
tapolcai@531
   290
      Node rn = INVALID;
tapolcai@531
   291
      Value value = std::numeric_limits<Value>::max();
tapolcai@531
   292
      
tapolcai@531
   293
      while (sn != tn) {
tapolcai@531
   294
	if ((*_order)[sn] < (*_order)[tn]) {
alpar@532
   295
	  if ((*_weight)[tn] <= value) {
tapolcai@531
   296
	    rn = tn;
alpar@532
   297
            s_root = false;
tapolcai@531
   298
	    value = (*_weight)[tn];
tapolcai@531
   299
	  }
tapolcai@531
   300
	  tn = (*_pred)[tn];
tapolcai@531
   301
	} else {
alpar@532
   302
	  if ((*_weight)[sn] <= value) {
tapolcai@531
   303
	    rn = sn;
alpar@532
   304
            s_root = true;
tapolcai@531
   305
	    value = (*_weight)[sn];
tapolcai@531
   306
	  }
tapolcai@531
   307
	  sn = (*_pred)[sn];
tapolcai@531
   308
	}
tapolcai@531
   309
      }
tapolcai@531
   310
tapolcai@531
   311
      typename Graph::template NodeMap<bool> reached(_graph, false);
kpeter@573
   312
      reached[_root] = true;
alpar@532
   313
      cutMap.set(_root, !s_root);
kpeter@573
   314
      reached[rn] = true;
alpar@532
   315
      cutMap.set(rn, s_root);
tapolcai@531
   316
alpar@532
   317
      std::vector<Node> st;
tapolcai@531
   318
      for (NodeIt n(_graph); n != INVALID; ++n) {
alpar@532
   319
	st.clear();
alpar@532
   320
        Node nn = n;
tapolcai@531
   321
	while (!reached[nn]) {
tapolcai@531
   322
	  st.push_back(nn);
tapolcai@531
   323
	  nn = (*_pred)[nn];
tapolcai@531
   324
	}
tapolcai@531
   325
	while (!st.empty()) {
tapolcai@531
   326
	  cutMap.set(st.back(), cutMap[nn]);
tapolcai@531
   327
	  st.pop_back();
tapolcai@531
   328
	}
tapolcai@531
   329
      }
tapolcai@531
   330
      
tapolcai@531
   331
      return value;
tapolcai@531
   332
    }
tapolcai@531
   333
alpar@532
   334
    ///@}
alpar@532
   335
alpar@532
   336
    friend class MinCutNodeIt;
alpar@532
   337
alpar@532
   338
    /// Iterate on the nodes of a minimum cut
alpar@532
   339
    
alpar@532
   340
    /// This iterator class lists the nodes of a minimum cut found by
alpar@533
   341
    /// GomoryHu. Before using it, you must allocate a GomoryHu class,
alpar@533
   342
    /// and call its \ref GomoryHu::run() "run()" method.
alpar@532
   343
    ///
alpar@532
   344
    /// This example counts the nodes in the minimum cut separating \c s from
alpar@532
   345
    /// \c t.
alpar@532
   346
    /// \code
alpar@533
   347
    /// GomoruHu<Graph> gom(g, capacities);
alpar@532
   348
    /// gom.run();
kpeter@534
   349
    /// int cnt=0;
kpeter@534
   350
    /// for(GomoruHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt;
alpar@532
   351
    /// \endcode
alpar@532
   352
    class MinCutNodeIt
alpar@532
   353
    {
alpar@532
   354
      bool _side;
alpar@532
   355
      typename Graph::NodeIt _node_it;
alpar@532
   356
      typename Graph::template NodeMap<bool> _cut;
alpar@532
   357
    public:
alpar@532
   358
      /// Constructor
alpar@532
   359
kpeter@534
   360
      /// Constructor.
alpar@532
   361
      ///
alpar@533
   362
      MinCutNodeIt(GomoryHu const &gomory,
alpar@533
   363
                   ///< The GomoryHu class. You must call its
alpar@532
   364
                   ///  run() method
kpeter@534
   365
                   ///  before initializing this iterator.
kpeter@534
   366
                   const Node& s, ///< The base node.
alpar@532
   367
                   const Node& t,
kpeter@534
   368
                   ///< The node you want to separate from node \c s.
alpar@532
   369
                   bool side=true
alpar@532
   370
                   ///< If it is \c true (default) then the iterator lists
alpar@532
   371
                   ///  the nodes of the component containing \c s,
alpar@532
   372
                   ///  otherwise it lists the other component.
alpar@532
   373
                   /// \note As the minimum cut is not always unique,
alpar@532
   374
                   /// \code
alpar@532
   375
                   /// MinCutNodeIt(gomory, s, t, true);
alpar@532
   376
                   /// \endcode
alpar@532
   377
                   /// and
alpar@532
   378
                   /// \code
alpar@532
   379
                   /// MinCutNodeIt(gomory, t, s, false);
alpar@532
   380
                   /// \endcode
alpar@532
   381
                   /// does not necessarily give the same set of nodes.
alpar@532
   382
                   /// However it is ensured that
alpar@532
   383
                   /// \code
alpar@532
   384
                   /// MinCutNodeIt(gomory, s, t, true);
alpar@532
   385
                   /// \endcode
alpar@532
   386
                   /// and
alpar@532
   387
                   /// \code
alpar@532
   388
                   /// MinCutNodeIt(gomory, s, t, false);
alpar@532
   389
                   /// \endcode
alpar@532
   390
                   /// together list each node exactly once.
alpar@532
   391
                   )
alpar@532
   392
        : _side(side), _cut(gomory._graph)
alpar@532
   393
      {
alpar@532
   394
        gomory.minCutMap(s,t,_cut);
alpar@532
   395
        for(_node_it=typename Graph::NodeIt(gomory._graph);
alpar@532
   396
            _node_it!=INVALID && _cut[_node_it]!=_side;
alpar@532
   397
            ++_node_it) {}
alpar@532
   398
      }
kpeter@534
   399
      /// Conversion to \c Node
alpar@532
   400
kpeter@534
   401
      /// Conversion to \c Node.
alpar@532
   402
      ///
alpar@532
   403
      operator typename Graph::Node() const
alpar@532
   404
      {
alpar@532
   405
        return _node_it;
alpar@532
   406
      }
alpar@532
   407
      bool operator==(Invalid) { return _node_it==INVALID; }
alpar@532
   408
      bool operator!=(Invalid) { return _node_it!=INVALID; }
alpar@532
   409
      /// Next node
alpar@532
   410
kpeter@534
   411
      /// Next node.
alpar@532
   412
      ///
alpar@532
   413
      MinCutNodeIt &operator++()
alpar@532
   414
      {
alpar@532
   415
        for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
alpar@532
   416
        return *this;
alpar@532
   417
      }
alpar@532
   418
      /// Postfix incrementation
alpar@532
   419
kpeter@534
   420
      /// Postfix incrementation.
alpar@532
   421
      ///
alpar@532
   422
      /// \warning This incrementation
kpeter@534
   423
      /// returns a \c Node, not a \c MinCutNodeIt, as one may
alpar@532
   424
      /// expect.
alpar@532
   425
      typename Graph::Node operator++(int)
alpar@532
   426
      {
alpar@532
   427
        typename Graph::Node n=*this;
alpar@532
   428
        ++(*this);
alpar@532
   429
        return n;
alpar@532
   430
      }
alpar@532
   431
    };
alpar@532
   432
    
alpar@532
   433
    friend class MinCutEdgeIt;
alpar@532
   434
    
alpar@532
   435
    /// Iterate on the edges of a minimum cut
alpar@532
   436
    
alpar@532
   437
    /// This iterator class lists the edges of a minimum cut found by
alpar@533
   438
    /// GomoryHu. Before using it, you must allocate a GomoryHu class,
alpar@533
   439
    /// and call its \ref GomoryHu::run() "run()" method.
alpar@532
   440
    ///
alpar@532
   441
    /// This example computes the value of the minimum cut separating \c s from
alpar@532
   442
    /// \c t.
alpar@532
   443
    /// \code
alpar@533
   444
    /// GomoruHu<Graph> gom(g, capacities);
alpar@532
   445
    /// gom.run();
alpar@532
   446
    /// int value=0;
kpeter@534
   447
    /// for(GomoruHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e)
alpar@532
   448
    ///   value+=capacities[e];
alpar@532
   449
    /// \endcode
alpar@532
   450
    /// the result will be the same as it is returned by
kpeter@534
   451
    /// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)"
alpar@532
   452
    class MinCutEdgeIt
alpar@532
   453
    {
alpar@532
   454
      bool _side;
alpar@532
   455
      const Graph &_graph;
alpar@532
   456
      typename Graph::NodeIt _node_it;
alpar@532
   457
      typename Graph::OutArcIt _arc_it;
alpar@532
   458
      typename Graph::template NodeMap<bool> _cut;
alpar@532
   459
      void step()
alpar@532
   460
      {
alpar@532
   461
        ++_arc_it;
alpar@532
   462
        while(_node_it!=INVALID && _arc_it==INVALID)
alpar@532
   463
          {
alpar@532
   464
            for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
alpar@532
   465
            if(_node_it!=INVALID)
alpar@532
   466
              _arc_it=typename Graph::OutArcIt(_graph,_node_it);
alpar@532
   467
          }
alpar@532
   468
      }
alpar@532
   469
      
alpar@532
   470
    public:
alpar@533
   471
      MinCutEdgeIt(GomoryHu const &gomory,
alpar@533
   472
                   ///< The GomoryHu class. You must call its
alpar@532
   473
                   ///  run() method
kpeter@534
   474
                   ///  before initializing this iterator.
kpeter@534
   475
                   const Node& s,  ///< The base node.
alpar@532
   476
                   const Node& t,
kpeter@534
   477
                   ///< The node you want to separate from node \c s.
alpar@532
   478
                   bool side=true
alpar@532
   479
                   ///< If it is \c true (default) then the listed arcs
alpar@532
   480
                   ///  will be oriented from the
alpar@532
   481
                   ///  the nodes of the component containing \c s,
alpar@532
   482
                   ///  otherwise they will be oriented in the opposite
alpar@532
   483
                   ///  direction.
alpar@532
   484
                   )
alpar@532
   485
        : _graph(gomory._graph), _cut(_graph)
alpar@532
   486
      {
alpar@532
   487
        gomory.minCutMap(s,t,_cut);
alpar@532
   488
        if(!side)
alpar@532
   489
          for(typename Graph::NodeIt n(_graph);n!=INVALID;++n)
alpar@532
   490
            _cut[n]=!_cut[n];
alpar@532
   491
alpar@532
   492
        for(_node_it=typename Graph::NodeIt(_graph);
alpar@532
   493
            _node_it!=INVALID && !_cut[_node_it];
alpar@532
   494
            ++_node_it) {}
alpar@532
   495
        _arc_it = _node_it!=INVALID ?
alpar@532
   496
          typename Graph::OutArcIt(_graph,_node_it) : INVALID;
alpar@532
   497
        while(_node_it!=INVALID && _arc_it == INVALID)
alpar@532
   498
          {
alpar@532
   499
            for(++_node_it; _node_it!=INVALID&&!_cut[_node_it]; ++_node_it) {}
alpar@532
   500
            if(_node_it!=INVALID)
alpar@532
   501
              _arc_it= typename Graph::OutArcIt(_graph,_node_it);
alpar@532
   502
          }
alpar@532
   503
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
alpar@532
   504
      }
kpeter@534
   505
      /// Conversion to \c Arc
alpar@532
   506
kpeter@534
   507
      /// Conversion to \c Arc.
alpar@532
   508
      ///
alpar@532
   509
      operator typename Graph::Arc() const
alpar@532
   510
      {
alpar@532
   511
        return _arc_it;
alpar@532
   512
      }
kpeter@534
   513
      /// Conversion to \c Edge
alpar@532
   514
kpeter@534
   515
      /// Conversion to \c Edge.
alpar@532
   516
      ///
alpar@532
   517
      operator typename Graph::Edge() const
alpar@532
   518
      {
alpar@532
   519
        return _arc_it;
alpar@532
   520
      }
alpar@532
   521
      bool operator==(Invalid) { return _node_it==INVALID; }
alpar@532
   522
      bool operator!=(Invalid) { return _node_it!=INVALID; }
alpar@532
   523
      /// Next edge
alpar@532
   524
kpeter@534
   525
      /// Next edge.
alpar@532
   526
      ///
alpar@532
   527
      MinCutEdgeIt &operator++()
alpar@532
   528
      {
alpar@532
   529
        step();
alpar@532
   530
        while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step();
alpar@532
   531
        return *this;
alpar@532
   532
      }
alpar@532
   533
      /// Postfix incrementation
alpar@532
   534
      
kpeter@534
   535
      /// Postfix incrementation.
alpar@532
   536
      ///
alpar@532
   537
      /// \warning This incrementation
kpeter@534
   538
      /// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect.
alpar@532
   539
      typename Graph::Arc operator++(int)
alpar@532
   540
      {
alpar@532
   541
        typename Graph::Arc e=*this;
alpar@532
   542
        ++(*this);
alpar@532
   543
        return e;
alpar@532
   544
      }
alpar@532
   545
    };
alpar@532
   546
tapolcai@531
   547
  };
tapolcai@531
   548
tapolcai@531
   549
}
tapolcai@531
   550
tapolcai@531
   551
#endif