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