src/hugo/max_flow.h
author alpar
Thu, 22 Jul 2004 20:07:49 +0000
changeset 734 329832ac02b7
child 735 2859c45c31dd
permissions -rw-r--r--
A remark added.
alpar@726
     1
// -*- C++ -*-
alpar@726
     2
#ifndef HUGO_MAX_FLOW_NO_STACK_H
alpar@726
     3
#define HUGO_MAX_FLOW_NO_STACK_H
alpar@726
     4
alpar@726
     5
#include <vector>
alpar@726
     6
#include <queue>
alpar@726
     7
//#include <stack>
alpar@726
     8
alpar@726
     9
#include <hugo/graph_wrapper.h>
alpar@726
    10
#include <hugo/invalid.h>
alpar@726
    11
#include <hugo/maps.h>
alpar@726
    12
alpar@726
    13
/// \file
alpar@726
    14
/// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test.
alpar@726
    15
/// \ingroup galgs
alpar@726
    16
alpar@726
    17
namespace hugo {
alpar@726
    18
alpar@726
    19
  /// \addtogroup galgs
alpar@726
    20
  /// @{                                                                                                                                        
alpar@726
    21
  ///Maximum flow algorithms class.
alpar@726
    22
alpar@726
    23
  ///This class provides various algorithms for finding a flow of
alpar@726
    24
  ///maximum value in a directed graph. The \e source node, the \e
alpar@726
    25
  ///target node, the \e capacity of the edges and the \e starting \e
alpar@726
    26
  ///flow value of the edges should be passed to the algorithm through the
alpar@726
    27
  ///constructor. It is possible to change these quantities using the
alpar@726
    28
  ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
alpar@726
    29
  ///\ref resetFlow. Before any subsequent runs of any algorithm of
alpar@726
    30
  ///the class \ref resetFlow should be called. 
alpar@726
    31
alpar@726
    32
  ///After running an algorithm of the class, the actual flow value 
alpar@726
    33
  ///can be obtained by calling \ref flowValue(). The minimum
alpar@726
    34
  ///value cut can be written into a \c node map of \c bools by
alpar@726
    35
  ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
alpar@726
    36
  ///the inclusionwise minimum and maximum of the minimum value
alpar@726
    37
  ///cuts, resp.)                                                                                                                               
alpar@726
    38
  ///\param Graph The directed graph type the algorithm runs on.
alpar@726
    39
  ///\param Num The number type of the capacities and the flow values.
alpar@726
    40
  ///\param CapMap The capacity map type.
alpar@726
    41
  ///\param FlowMap The flow map type.                                                                                                           
alpar@726
    42
  ///\author Marton Makai, Jacint Szabo 
alpar@726
    43
  template <typename Graph, typename Num,
alpar@726
    44
	    typename CapMap=typename Graph::template EdgeMap<Num>,
alpar@726
    45
            typename FlowMap=typename Graph::template EdgeMap<Num> >
alpar@726
    46
  class MaxFlow {
alpar@726
    47
  protected:
alpar@726
    48
    typedef typename Graph::Node Node;
alpar@726
    49
    typedef typename Graph::NodeIt NodeIt;
alpar@726
    50
    typedef typename Graph::EdgeIt EdgeIt;
alpar@726
    51
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@726
    52
    typedef typename Graph::InEdgeIt InEdgeIt;
alpar@726
    53
alpar@726
    54
    //    typedef typename std::vector<std::stack<Node> > VecStack;
alpar@726
    55
    typedef typename std::vector<Node> VecFirst;
alpar@726
    56
    typedef typename Graph::template NodeMap<Node> NNMap;
alpar@726
    57
    typedef typename std::vector<Node> VecNode;
alpar@726
    58
alpar@726
    59
    const Graph* g;
alpar@726
    60
    Node s;
alpar@726
    61
    Node t;
alpar@726
    62
    const CapMap* capacity;
alpar@726
    63
    FlowMap* flow;
alpar@726
    64
    int n;      //the number of nodes of G
alpar@726
    65
    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
alpar@726
    66
    //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
alpar@726
    67
    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
alpar@726
    68
    typedef typename ResGW::Edge ResGWEdge;
alpar@726
    69
    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
alpar@726
    70
    typedef typename Graph::template NodeMap<int> ReachedMap;
alpar@726
    71
alpar@726
    72
alpar@726
    73
    //level works as a bool map in augmenting path algorithms and is
alpar@726
    74
    //used by bfs for storing reached information.  In preflow, it
alpar@726
    75
    //shows the levels of nodes.     
alpar@726
    76
    ReachedMap level;
alpar@726
    77
alpar@726
    78
    //excess is needed only in preflow
alpar@726
    79
    typename Graph::template NodeMap<Num> excess;
alpar@726
    80
alpar@726
    81
    // constants used for heuristics
alpar@726
    82
    static const int H0=20;
alpar@726
    83
    static const int H1=1;
alpar@726
    84
alpar@726
    85
  public:
alpar@726
    86
alpar@726
    87
    ///Indicates the property of the starting flow.
alpar@726
    88
alpar@726
    89
    ///Indicates the property of the starting flow. The meanings are as follows:
alpar@726
    90
    ///- \c ZERO_FLOW: constant zero flow
alpar@726
    91
    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
alpar@726
    92
    ///the sum of the out-flows in every node except the \e source and
alpar@726
    93
    ///the \e target.
alpar@726
    94
    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
alpar@726
    95
    ///least the sum of the out-flows in every node except the \e source.
alpar@726
    96
    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
alpar@726
    97
    ///set to the constant zero flow in the beginning of the algorithm in this case.
alpar@726
    98
    enum FlowEnum{
alpar@726
    99
      ZERO_FLOW,
alpar@726
   100
      GEN_FLOW,
alpar@726
   101
      PRE_FLOW,
alpar@726
   102
      NO_FLOW
alpar@726
   103
    };
alpar@726
   104
alpar@726
   105
    enum StatusEnum {
alpar@726
   106
      AFTER_NOTHING,
alpar@726
   107
      AFTER_AUGMENTING,
alpar@726
   108
      AFTER_FAST_AUGMENTING, 
alpar@726
   109
      AFTER_PRE_FLOW_PHASE_1,      
alpar@726
   110
      AFTER_PRE_FLOW_PHASE_2
alpar@726
   111
    };
alpar@726
   112
alpar@726
   113
    /// Don not needle this flag only if necessary.
alpar@726
   114
    StatusEnum status;
alpar@726
   115
alpar@726
   116
//     int number_of_augmentations;
alpar@726
   117
alpar@726
   118
alpar@726
   119
//     template<typename IntMap>
alpar@726
   120
//     class TrickyReachedMap {
alpar@726
   121
//     protected:
alpar@726
   122
//       IntMap* map;
alpar@726
   123
//       int* number_of_augmentations;
alpar@726
   124
//     public:
alpar@726
   125
//       TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
alpar@726
   126
// 	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
alpar@726
   127
//       void set(const Node& n, bool b) {
alpar@726
   128
// 	if (b)
alpar@726
   129
// 	  map->set(n, *number_of_augmentations);
alpar@726
   130
// 	else 
alpar@726
   131
// 	  map->set(n, *number_of_augmentations-1);
alpar@726
   132
//       }
alpar@726
   133
//       bool operator[](const Node& n) const { 
alpar@726
   134
// 	return (*map)[n]==*number_of_augmentations; 
alpar@726
   135
//       }
alpar@726
   136
//     };
alpar@726
   137
    
alpar@726
   138
    ///Constructor
alpar@726
   139
alpar@726
   140
    ///\todo Document, please.
alpar@726
   141
    ///
alpar@726
   142
    MaxFlow(const Graph& _G, Node _s, Node _t,
alpar@726
   143
		   const CapMap& _capacity, FlowMap& _flow) :
alpar@726
   144
      g(&_G), s(_s), t(_t), capacity(&_capacity),
alpar@726
   145
      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
alpar@726
   146
      status(AFTER_NOTHING) { }
alpar@726
   147
alpar@726
   148
    ///Runs a maximum flow algorithm.
alpar@726
   149
alpar@726
   150
    ///Runs a preflow algorithm, which is the fastest maximum flow
alpar@726
   151
    ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
alpar@726
   152
    ///\pre The starting flow must be
alpar@726
   153
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
alpar@726
   154
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
alpar@726
   155
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
alpar@726
   156
    /// - any map if \c fe is NO_FLOW.
alpar@726
   157
    void run(FlowEnum fe=ZERO_FLOW) {
alpar@726
   158
      preflow(fe);
alpar@726
   159
    }
alpar@726
   160
alpar@726
   161
                                                                              
alpar@726
   162
    ///Runs a preflow algorithm.  
alpar@726
   163
alpar@726
   164
    ///Runs a preflow algorithm. The preflow algorithms provide the
alpar@726
   165
    ///fastest way to compute a maximum flow in a directed graph.
alpar@726
   166
    ///\pre The starting flow must be
alpar@726
   167
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
alpar@726
   168
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
alpar@726
   169
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
alpar@726
   170
    /// - any map if \c fe is NO_FLOW.
alpar@726
   171
    ///
alpar@726
   172
    ///\todo NO_FLOW should be the default flow.
alpar@726
   173
    void preflow(FlowEnum fe) {
alpar@726
   174
      preflowPhase1(fe);
alpar@726
   175
      preflowPhase2();
alpar@726
   176
    }
alpar@726
   177
    // Heuristics:
alpar@726
   178
    //   2 phase
alpar@726
   179
    //   gap
alpar@726
   180
    //   list 'level_list' on the nodes on level i implemented by hand
alpar@726
   181
    //   stack 'active' on the active nodes on level i                                                                                    
alpar@726
   182
    //   runs heuristic 'highest label' for H1*n relabels
alpar@726
   183
    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
alpar@726
   184
    //   Parameters H0 and H1 are initialized to 20 and 1.
alpar@726
   185
alpar@726
   186
    ///Runs the first phase of the preflow algorithm.
alpar@726
   187
alpar@726
   188
    ///The preflow algorithm consists of two phases, this method runs the
alpar@726
   189
    ///first phase. After the first phase the maximum flow value and a
alpar@726
   190
    ///minimum value cut can already be computed, though a maximum flow
alpar@726
   191
    ///is net yet obtained. So after calling this method \ref flowValue
alpar@726
   192
    ///and \ref actMinCut gives proper results.
alpar@726
   193
    ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
alpar@726
   194
    ///give minimum value cuts unless calling \ref preflowPhase2.
alpar@726
   195
    ///\pre The starting flow must be
alpar@726
   196
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
alpar@726
   197
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
alpar@726
   198
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
alpar@726
   199
    /// - any map if \c fe is NO_FLOW.
alpar@726
   200
    void preflowPhase1(FlowEnum fe)
alpar@726
   201
    {
alpar@726
   202
alpar@726
   203
      int heur0=(int)(H0*n);  //time while running 'bound decrease'
alpar@726
   204
      int heur1=(int)(H1*n);  //time while running 'highest label'
alpar@726
   205
      int heur=heur1;         //starting time interval (#of relabels)
alpar@726
   206
      int numrelabel=0;
alpar@726
   207
alpar@726
   208
      bool what_heur=1;
alpar@726
   209
      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
alpar@726
   210
alpar@726
   211
      bool end=false;
alpar@726
   212
      //Needed for 'bound decrease', true means no active nodes are above bound
alpar@726
   213
      //b.
alpar@726
   214
alpar@726
   215
      int k=n-2;  //bound on the highest level under n containing a node
alpar@726
   216
      int b=k;    //bound on the highest level under n of an active node
alpar@726
   217
alpar@726
   218
      VecFirst first(n, INVALID);
alpar@726
   219
      NNMap next(*g, INVALID); //maybe INVALID is not needed
alpar@726
   220
      //    VecStack active(n);
alpar@726
   221
alpar@726
   222
      NNMap left(*g, INVALID);
alpar@726
   223
      NNMap right(*g, INVALID);
alpar@726
   224
      VecNode level_list(n,INVALID);
alpar@726
   225
      //List of the nodes in level i<n, set to n.
alpar@726
   226
alpar@726
   227
      NodeIt v;
alpar@726
   228
      for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
alpar@726
   229
      //setting each node to level n
alpar@726
   230
alpar@726
   231
      if ( fe == NO_FLOW ) {
alpar@726
   232
	EdgeIt e;
alpar@726
   233
	for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
alpar@726
   234
      }
alpar@726
   235
alpar@726
   236
      switch (fe) { //computing the excess
alpar@726
   237
      case PRE_FLOW:
alpar@726
   238
	{
alpar@726
   239
	  NodeIt v;
alpar@726
   240
	  for(g->first(v); g->valid(v); g->next(v)) {
alpar@726
   241
	    Num exc=0;
alpar@726
   242
alpar@726
   243
	    InEdgeIt e;
alpar@726
   244
	    for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
alpar@726
   245
	    OutEdgeIt f;
alpar@726
   246
	    for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
alpar@726
   247
alpar@726
   248
	    excess.set(v,exc);
alpar@726
   249
alpar@726
   250
	    //putting the active nodes into the stack
alpar@726
   251
	    int lev=level[v];
alpar@726
   252
	    if ( exc > 0 && lev < n && v != t ) 
alpar@726
   253
	      {
alpar@726
   254
		next.set(v,first[lev]);
alpar@726
   255
		first[lev]=v;
alpar@726
   256
	      }
alpar@726
   257
	    //	  active[lev].push(v);
alpar@726
   258
	  }
alpar@726
   259
	  break;
alpar@726
   260
	}
alpar@726
   261
      case GEN_FLOW:
alpar@726
   262
	{
alpar@726
   263
	  NodeIt v;
alpar@726
   264
	  for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
alpar@726
   265
alpar@726
   266
	  Num exc=0;
alpar@726
   267
	  InEdgeIt e;
alpar@726
   268
	  for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
alpar@726
   269
	  OutEdgeIt f;
alpar@726
   270
	  for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
alpar@726
   271
	  excess.set(t,exc);
alpar@726
   272
	  break;
alpar@726
   273
	}
alpar@726
   274
      case ZERO_FLOW:
alpar@726
   275
      case NO_FLOW:
alpar@726
   276
	{
alpar@726
   277
	  NodeIt v;
alpar@726
   278
	  for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
alpar@726
   279
	  break;
alpar@726
   280
	}
alpar@726
   281
      }
alpar@726
   282
alpar@726
   283
      preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
alpar@726
   284
      //End of preprocessing
alpar@726
   285
alpar@726
   286
alpar@726
   287
      //Push/relabel on the highest level active nodes.
alpar@726
   288
      while ( true ) {
alpar@726
   289
	if ( b == 0 ) {
alpar@726
   290
	  if ( !what_heur && !end && k > 0 ) {
alpar@726
   291
	    b=k;
alpar@726
   292
	    end=true;
alpar@726
   293
	  } else break;
alpar@726
   294
	}
alpar@726
   295
alpar@726
   296
	if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
alpar@726
   297
	else {
alpar@726
   298
	  end=false;
alpar@726
   299
	  Node w=first[b];
alpar@726
   300
	  first[b]=next[w];
alpar@726
   301
	  /*	Node w=active[b].top();
alpar@726
   302
		active[b].pop();*/
alpar@726
   303
	  int newlevel=push(w,/*active*/next, first);
alpar@726
   304
	  if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
alpar@726
   305
				       left, right, b, k, what_heur);
alpar@726
   306
alpar@726
   307
	  ++numrelabel;
alpar@726
   308
	  if ( numrelabel >= heur ) {
alpar@726
   309
	    numrelabel=0;
alpar@726
   310
	    if ( what_heur ) {
alpar@726
   311
	      what_heur=0;
alpar@726
   312
	      heur=heur0;
alpar@726
   313
	      end=false;
alpar@726
   314
	    } else {
alpar@726
   315
	      what_heur=1;
alpar@726
   316
	      heur=heur1;
alpar@726
   317
	      b=k;
alpar@726
   318
	    }
alpar@726
   319
	  }
alpar@726
   320
	}
alpar@726
   321
      }
alpar@726
   322
alpar@726
   323
      status=AFTER_PRE_FLOW_PHASE_1;
alpar@726
   324
    }
alpar@726
   325
alpar@726
   326
alpar@726
   327
    ///Runs the second phase of the preflow algorithm.
alpar@726
   328
alpar@726
   329
    ///The preflow algorithm consists of two phases, this method runs
alpar@726
   330
    ///the second phase. After calling \ref preflowPhase1 and then
alpar@726
   331
    ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
alpar@726
   332
    ///\ref minMinCut and \ref maxMinCut give proper results.
alpar@726
   333
    ///\pre \ref preflowPhase1 must be called before.
alpar@726
   334
    void preflowPhase2()
alpar@726
   335
    {
alpar@726
   336
alpar@726
   337
      int k=n-2;  //bound on the highest level under n containing a node
alpar@726
   338
      int b=k;    //bound on the highest level under n of an active node
alpar@726
   339
alpar@726
   340
    
alpar@726
   341
      VecFirst first(n, INVALID);
alpar@726
   342
      NNMap next(*g, INVALID); //maybe INVALID is not needed
alpar@726
   343
      //    VecStack active(n);
alpar@726
   344
      level.set(s,0);
alpar@726
   345
      std::queue<Node> bfs_queue;
alpar@726
   346
      bfs_queue.push(s);
alpar@726
   347
alpar@726
   348
      while (!bfs_queue.empty()) {
alpar@726
   349
alpar@726
   350
	Node v=bfs_queue.front();
alpar@726
   351
	bfs_queue.pop();
alpar@726
   352
	int l=level[v]+1;
alpar@726
   353
alpar@726
   354
	InEdgeIt e;
alpar@726
   355
	for(g->first(e,v); g->valid(e); g->next(e)) {
alpar@726
   356
	  if ( (*capacity)[e] <= (*flow)[e] ) continue;
alpar@726
   357
	  Node u=g->tail(e);
alpar@726
   358
	  if ( level[u] >= n ) {
alpar@726
   359
	    bfs_queue.push(u);
alpar@726
   360
	    level.set(u, l);
alpar@726
   361
	    if ( excess[u] > 0 ) {
alpar@726
   362
	      next.set(u,first[l]);
alpar@726
   363
	      first[l]=u;
alpar@726
   364
	      //active[l].push(u);
alpar@726
   365
	    }
alpar@726
   366
	  }
alpar@726
   367
	}
alpar@726
   368
alpar@726
   369
	OutEdgeIt f;
alpar@726
   370
	for(g->first(f,v); g->valid(f); g->next(f)) {
alpar@726
   371
	  if ( 0 >= (*flow)[f] ) continue;
alpar@726
   372
	  Node u=g->head(f);
alpar@726
   373
	  if ( level[u] >= n ) {
alpar@726
   374
	    bfs_queue.push(u);
alpar@726
   375
	    level.set(u, l);
alpar@726
   376
	    if ( excess[u] > 0 ) {
alpar@726
   377
	      next.set(u,first[l]);
alpar@726
   378
	      first[l]=u;
alpar@726
   379
	      //active[l].push(u);
alpar@726
   380
	    }
alpar@726
   381
	  }
alpar@726
   382
	}
alpar@726
   383
      }
alpar@726
   384
      b=n-2;
alpar@726
   385
alpar@726
   386
      while ( true ) {
alpar@726
   387
alpar@726
   388
	if ( b == 0 ) break;
alpar@726
   389
alpar@726
   390
	if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
alpar@726
   391
	else {
alpar@726
   392
alpar@726
   393
	  Node w=first[b];
alpar@726
   394
	  first[b]=next[w];
alpar@726
   395
	  /*	Node w=active[b].top();
alpar@726
   396
		active[b].pop();*/
alpar@726
   397
	  int newlevel=push(w,next, first/*active*/);
alpar@726
   398
alpar@726
   399
	  //relabel
alpar@726
   400
	  if ( excess[w] > 0 ) {
alpar@726
   401
	    level.set(w,++newlevel);
alpar@726
   402
	    next.set(w,first[newlevel]);
alpar@726
   403
	    first[newlevel]=w;
alpar@726
   404
	    //active[newlevel].push(w);
alpar@726
   405
	    b=newlevel;
alpar@726
   406
	  }
alpar@726
   407
	}  // if stack[b] is nonempty
alpar@726
   408
      } // while(true)
alpar@726
   409
alpar@726
   410
      status=AFTER_PRE_FLOW_PHASE_2;
alpar@726
   411
    }
alpar@726
   412
alpar@726
   413
alpar@726
   414
    /// Returns the maximum value of a flow.
alpar@726
   415
alpar@726
   416
    /// Returns the maximum value of a flow, by counting the 
alpar@726
   417
    /// over-flow of the target node \ref t.
alpar@726
   418
    /// It can be called already after running \ref preflowPhase1.
alpar@726
   419
    Num flowValue() const {
alpar@726
   420
      Num a=0;
alpar@726
   421
      for(InEdgeIt e(*g,t);g->valid(e);G.next(e)) a+=(*flow)[e];
alpar@726
   422
      for(OutEdgeIt e(*g,t);g->valid(e);G.next(e)) a-=(*flow)[e];
alpar@726
   423
alpar@726
   424
      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
alpar@726
   425
    }
alpar@726
   426
alpar@726
   427
    ///Returns a minimum value cut after calling \ref preflowPhase1.
alpar@726
   428
alpar@726
   429
    ///After the first phase of the preflow algorithm the maximum flow
alpar@726
   430
    ///value and a minimum value cut can already be computed. This
alpar@726
   431
    ///method can be called after running \ref preflowPhase1 for
alpar@726
   432
    ///obtaining a minimum value cut.
alpar@726
   433
    /// \warning Gives proper result only right after calling \ref
alpar@726
   434
    /// preflowPhase1.
alpar@726
   435
    /// \todo We have to make some status variable which shows the
alpar@726
   436
    /// actual state
alpar@726
   437
    /// of the class. This enables us to determine which methods are valid
alpar@726
   438
    /// for MinCut computation
alpar@726
   439
    template<typename _CutMap>
alpar@726
   440
    void actMinCut(_CutMap& M) const {
alpar@726
   441
      NodeIt v;
alpar@726
   442
      switch (status) {
alpar@726
   443
      case AFTER_PRE_FLOW_PHASE_1:
alpar@726
   444
	for(g->first(v); g->valid(v); g->next(v)) {
alpar@726
   445
	  if (level[v] < n) {
alpar@726
   446
	    M.set(v, false);
alpar@726
   447
	  } else {
alpar@726
   448
	    M.set(v, true);
alpar@726
   449
	  }
alpar@726
   450
	}
alpar@726
   451
	break;
alpar@726
   452
      case AFTER_PRE_FLOW_PHASE_2:
alpar@726
   453
      case AFTER_NOTHING:
alpar@726
   454
	minMinCut(M);
alpar@726
   455
	break;
alpar@726
   456
      case AFTER_AUGMENTING:
alpar@726
   457
	for(g->first(v); g->valid(v); g->next(v)) {
alpar@726
   458
	  if (level[v]) {
alpar@726
   459
	    M.set(v, true);
alpar@726
   460
	  } else {
alpar@726
   461
	    M.set(v, false);
alpar@726
   462
	  }
alpar@726
   463
	}
alpar@726
   464
	break;
alpar@726
   465
      case AFTER_FAST_AUGMENTING:
alpar@726
   466
	for(g->first(v); g->valid(v); g->next(v)) {
alpar@726
   467
	  if (level[v]==number_of_augmentations) {
alpar@726
   468
	    M.set(v, true);
alpar@726
   469
	  } else {
alpar@726
   470
	    M.set(v, false);
alpar@726
   471
	  }
alpar@726
   472
	}
alpar@726
   473
	break;
alpar@726
   474
      }
alpar@726
   475
    }
alpar@726
   476
alpar@726
   477
    ///Returns the inclusionwise minimum of the minimum value cuts.
alpar@726
   478
alpar@726
   479
    ///Sets \c M to the characteristic vector of the minimum value cut
alpar@726
   480
    ///which is inclusionwise minimum. It is computed by processing
alpar@726
   481
    ///a bfs from the source node \c s in the residual graph.
alpar@726
   482
    ///\pre M should be a node map of bools initialized to false.
alpar@726
   483
    ///\pre \c flow must be a maximum flow.
alpar@726
   484
    template<typename _CutMap>
alpar@726
   485
    void minMinCut(_CutMap& M) const {
alpar@726
   486
      std::queue<Node> queue;
alpar@726
   487
alpar@726
   488
      M.set(s,true);
alpar@726
   489
      queue.push(s);
alpar@726
   490
alpar@726
   491
      while (!queue.empty()) {
alpar@726
   492
        Node w=queue.front();
alpar@726
   493
	queue.pop();
alpar@726
   494
alpar@726
   495
	OutEdgeIt e;
alpar@726
   496
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
alpar@726
   497
	  Node v=g->head(e);
alpar@726
   498
	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
alpar@726
   499
	    queue.push(v);
alpar@726
   500
	    M.set(v, true);
alpar@726
   501
	  }
alpar@726
   502
	}
alpar@726
   503
alpar@726
   504
	InEdgeIt f;
alpar@726
   505
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
alpar@726
   506
	  Node v=g->tail(f);
alpar@726
   507
	  if (!M[v] && (*flow)[f] > 0 ) {
alpar@726
   508
	    queue.push(v);
alpar@726
   509
	    M.set(v, true);
alpar@726
   510
	  }
alpar@726
   511
	}
alpar@726
   512
      }
alpar@726
   513
    }
alpar@726
   514
alpar@726
   515
    ///Returns the inclusionwise maximum of the minimum value cuts.
alpar@726
   516
alpar@726
   517
    ///Sets \c M to the characteristic vector of the minimum value cut
alpar@726
   518
    ///which is inclusionwise maximum. It is computed by processing a
alpar@726
   519
    ///backward bfs from the target node \c t in the residual graph.
alpar@726
   520
    ///\pre M should be a node map of bools initialized to false.
alpar@726
   521
    ///\pre \c flow must be a maximum flow. 
alpar@726
   522
    template<typename _CutMap>
alpar@726
   523
    void maxMinCut(_CutMap& M) const {
alpar@726
   524
alpar@726
   525
      NodeIt v;
alpar@726
   526
      for(g->first(v) ; g->valid(v); g->next(v)) {
alpar@726
   527
	M.set(v, true);
alpar@726
   528
      }
alpar@726
   529
alpar@726
   530
      std::queue<Node> queue;
alpar@726
   531
alpar@726
   532
      M.set(t,false);
alpar@726
   533
      queue.push(t);
alpar@726
   534
alpar@726
   535
      while (!queue.empty()) {
alpar@726
   536
        Node w=queue.front();
alpar@726
   537
	queue.pop();
alpar@726
   538
alpar@726
   539
	InEdgeIt e;
alpar@726
   540
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
alpar@726
   541
	  Node v=g->tail(e);
alpar@726
   542
	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
alpar@726
   543
	    queue.push(v);
alpar@726
   544
	    M.set(v, false);
alpar@726
   545
	  }
alpar@726
   546
	}
alpar@726
   547
alpar@726
   548
	OutEdgeIt f;
alpar@726
   549
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
alpar@726
   550
	  Node v=g->head(f);
alpar@726
   551
	  if (M[v] && (*flow)[f] > 0 ) {
alpar@726
   552
	    queue.push(v);
alpar@726
   553
	    M.set(v, false);
alpar@726
   554
	  }
alpar@726
   555
	}
alpar@726
   556
      }
alpar@726
   557
    }
alpar@726
   558
alpar@726
   559
    ///Returns a minimum value cut.
alpar@726
   560
alpar@726
   561
    ///Sets \c M to the characteristic vector of a minimum value cut.
alpar@726
   562
    ///\pre M should be a node map of bools initialized to false.
alpar@726
   563
    ///\pre \c flow must be a maximum flow.    
alpar@726
   564
    template<typename CutMap>
alpar@726
   565
    void minCut(CutMap& M) const { minMinCut(M); }
alpar@726
   566
alpar@726
   567
    ///Resets the source node to \c _s.
alpar@726
   568
alpar@726
   569
    ///Resets the source node to \c _s.
alpar@726
   570
    /// 
alpar@726
   571
    void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
alpar@726
   572
alpar@726
   573
    ///Resets the target node to \c _t.
alpar@726
   574
alpar@726
   575
    ///Resets the target node to \c _t.
alpar@726
   576
    ///
alpar@726
   577
    void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
alpar@726
   578
alpar@726
   579
    /// Resets the edge map of the capacities to _cap.
alpar@726
   580
alpar@726
   581
    /// Resets the edge map of the capacities to _cap.
alpar@726
   582
    /// 
alpar@726
   583
    void resetCap(const CapMap& _cap)
alpar@726
   584
    { capacity=&_cap; status=AFTER_NOTHING; }
alpar@726
   585
alpar@726
   586
    /// Resets the edge map of the flows to _flow.
alpar@726
   587
alpar@726
   588
    /// Resets the edge map of the flows to _flow.
alpar@726
   589
    /// 
alpar@726
   590
    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
alpar@726
   591
alpar@726
   592
alpar@726
   593
  private:
alpar@726
   594
alpar@726
   595
    int push(Node w, NNMap& next, VecFirst& first) {
alpar@726
   596
alpar@726
   597
      int lev=level[w];
alpar@726
   598
      Num exc=excess[w];
alpar@726
   599
      int newlevel=n;       //bound on the next level of w
alpar@726
   600
alpar@726
   601
      OutEdgeIt e;
alpar@726
   602
      for(g->first(e,w); g->valid(e); g->next(e)) {
alpar@726
   603
alpar@726
   604
	if ( (*flow)[e] >= (*capacity)[e] ) continue;
alpar@726
   605
	Node v=g->head(e);
alpar@726
   606
alpar@726
   607
	if( lev > level[v] ) { //Push is allowed now
alpar@726
   608
alpar@726
   609
	  if ( excess[v]<=0 && v!=t && v!=s ) {
alpar@726
   610
	    next.set(v,first[level[v]]);
alpar@726
   611
	    first[level[v]]=v;
alpar@726
   612
	    //	    int lev_v=level[v];
alpar@726
   613
	    //active[lev_v].push(v);
alpar@726
   614
	  }
alpar@726
   615
alpar@726
   616
	  Num cap=(*capacity)[e];
alpar@726
   617
	  Num flo=(*flow)[e];
alpar@726
   618
	  Num remcap=cap-flo;
alpar@726
   619
alpar@726
   620
	  if ( remcap >= exc ) { //A nonsaturating push.
alpar@726
   621
alpar@726
   622
	    flow->set(e, flo+exc);
alpar@726
   623
	    excess.set(v, excess[v]+exc);
alpar@726
   624
	    exc=0;
alpar@726
   625
	    break;
alpar@726
   626
alpar@726
   627
	  } else { //A saturating push.
alpar@726
   628
	    flow->set(e, cap);
alpar@726
   629
	    excess.set(v, excess[v]+remcap);
alpar@726
   630
	    exc-=remcap;
alpar@726
   631
	  }
alpar@726
   632
	} else if ( newlevel > level[v] ) newlevel = level[v];
alpar@726
   633
      } //for out edges wv
alpar@726
   634
alpar@726
   635
      if ( exc > 0 ) {
alpar@726
   636
	InEdgeIt e;
alpar@726
   637
	for(g->first(e,w); g->valid(e); g->next(e)) {
alpar@726
   638
alpar@726
   639
	  if( (*flow)[e] <= 0 ) continue;
alpar@726
   640
	  Node v=g->tail(e);
alpar@726
   641
alpar@726
   642
	  if( lev > level[v] ) { //Push is allowed now
alpar@726
   643
alpar@726
   644
	    if ( excess[v]<=0 && v!=t && v!=s ) {
alpar@726
   645
	      next.set(v,first[level[v]]);
alpar@726
   646
	      first[level[v]]=v;
alpar@726
   647
	      //int lev_v=level[v];
alpar@726
   648
	      //active[lev_v].push(v);
alpar@726
   649
	    }
alpar@726
   650
alpar@726
   651
	    Num flo=(*flow)[e];
alpar@726
   652
alpar@726
   653
	    if ( flo >= exc ) { //A nonsaturating push.
alpar@726
   654
alpar@726
   655
	      flow->set(e, flo-exc);
alpar@726
   656
	      excess.set(v, excess[v]+exc);
alpar@726
   657
	      exc=0;
alpar@726
   658
	      break;
alpar@726
   659
	    } else {  //A saturating push.
alpar@726
   660
alpar@726
   661
	      excess.set(v, excess[v]+flo);
alpar@726
   662
	      exc-=flo;
alpar@726
   663
	      flow->set(e,0);
alpar@726
   664
	    }
alpar@726
   665
	  } else if ( newlevel > level[v] ) newlevel = level[v];
alpar@726
   666
	} //for in edges vw
alpar@726
   667
alpar@726
   668
      } // if w still has excess after the out edge for cycle
alpar@726
   669
alpar@726
   670
      excess.set(w, exc);
alpar@726
   671
alpar@726
   672
      return newlevel;
alpar@726
   673
    }
alpar@726
   674
alpar@726
   675
alpar@726
   676
    void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
alpar@726
   677
			VecNode& level_list, NNMap& left, NNMap& right)
alpar@726
   678
    {
alpar@726
   679
      std::queue<Node> bfs_queue;
alpar@726
   680
alpar@726
   681
      switch (fe) {
alpar@726
   682
      case NO_FLOW:   //flow is already set to const zero in this case
alpar@726
   683
      case ZERO_FLOW:
alpar@726
   684
	{
alpar@726
   685
	  //Reverse_bfs from t, to find the starting level.
alpar@726
   686
	  level.set(t,0);
alpar@726
   687
	  bfs_queue.push(t);
alpar@726
   688
alpar@726
   689
	  while (!bfs_queue.empty()) {
alpar@726
   690
alpar@726
   691
	    Node v=bfs_queue.front();
alpar@726
   692
	    bfs_queue.pop();
alpar@726
   693
	    int l=level[v]+1;
alpar@726
   694
alpar@726
   695
	    InEdgeIt e;
alpar@726
   696
	    for(g->first(e,v); g->valid(e); g->next(e)) {
alpar@726
   697
	      Node w=g->tail(e);
alpar@726
   698
	      if ( level[w] == n && w != s ) {
alpar@726
   699
		bfs_queue.push(w);
alpar@726
   700
		Node z=level_list[l];
alpar@726
   701
		if ( g->valid(z) ) left.set(z,w);
alpar@726
   702
		right.set(w,z);
alpar@726
   703
		level_list[l]=w;
alpar@726
   704
		level.set(w, l);
alpar@726
   705
	      }
alpar@726
   706
	    }
alpar@726
   707
	  }
alpar@726
   708
alpar@726
   709
	  //the starting flow
alpar@726
   710
	  OutEdgeIt e;
alpar@726
   711
	  for(g->first(e,s); g->valid(e); g->next(e))
alpar@726
   712
	    {
alpar@726
   713
	      Num c=(*capacity)[e];
alpar@726
   714
	      if ( c <= 0 ) continue;
alpar@726
   715
	      Node w=g->head(e);
alpar@726
   716
	      if ( level[w] < n ) {
alpar@726
   717
		if ( excess[w] <= 0 && w!=t ) 
alpar@726
   718
		  {
alpar@726
   719
		    next.set(w,first[level[w]]);
alpar@726
   720
		    first[level[w]]=w;
alpar@726
   721
		    //active[level[w]].push(w);
alpar@726
   722
		  }
alpar@726
   723
		flow->set(e, c);
alpar@726
   724
		excess.set(w, excess[w]+c);
alpar@726
   725
	      }
alpar@726
   726
	    }
alpar@726
   727
	  break;
alpar@726
   728
	}
alpar@726
   729
alpar@726
   730
      case GEN_FLOW:
alpar@726
   731
      case PRE_FLOW:
alpar@726
   732
	{
alpar@726
   733
	  //Reverse_bfs from t in the residual graph,
alpar@726
   734
	  //to find the starting level.
alpar@726
   735
	  level.set(t,0);
alpar@726
   736
	  bfs_queue.push(t);
alpar@726
   737
alpar@726
   738
	  while (!bfs_queue.empty()) {
alpar@726
   739
alpar@726
   740
	    Node v=bfs_queue.front();
alpar@726
   741
	    bfs_queue.pop();
alpar@726
   742
	    int l=level[v]+1;
alpar@726
   743
alpar@726
   744
	    InEdgeIt e;
alpar@726
   745
	    for(g->first(e,v); g->valid(e); g->next(e)) {
alpar@726
   746
	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
alpar@726
   747
	      Node w=g->tail(e);
alpar@726
   748
	      if ( level[w] == n && w != s ) {
alpar@726
   749
		bfs_queue.push(w);
alpar@726
   750
		Node z=level_list[l];
alpar@726
   751
		if ( g->valid(z) ) left.set(z,w);
alpar@726
   752
		right.set(w,z);
alpar@726
   753
		level_list[l]=w;
alpar@726
   754
		level.set(w, l);
alpar@726
   755
	      }
alpar@726
   756
	    }
alpar@726
   757
alpar@726
   758
	    OutEdgeIt f;
alpar@726
   759
	    for(g->first(f,v); g->valid(f); g->next(f)) {
alpar@726
   760
	      if ( 0 >= (*flow)[f] ) continue;
alpar@726
   761
	      Node w=g->head(f);
alpar@726
   762
	      if ( level[w] == n && w != s ) {
alpar@726
   763
		bfs_queue.push(w);
alpar@726
   764
		Node z=level_list[l];
alpar@726
   765
		if ( g->valid(z) ) left.set(z,w);
alpar@726
   766
		right.set(w,z);
alpar@726
   767
		level_list[l]=w;
alpar@726
   768
		level.set(w, l);
alpar@726
   769
	      }
alpar@726
   770
	    }
alpar@726
   771
	  }
alpar@726
   772
alpar@726
   773
alpar@726
   774
	  //the starting flow
alpar@726
   775
	  OutEdgeIt e;
alpar@726
   776
	  for(g->first(e,s); g->valid(e); g->next(e))
alpar@726
   777
	    {
alpar@726
   778
	      Num rem=(*capacity)[e]-(*flow)[e];
alpar@726
   779
	      if ( rem <= 0 ) continue;
alpar@726
   780
	      Node w=g->head(e);
alpar@726
   781
	      if ( level[w] < n ) {
alpar@726
   782
		if ( excess[w] <= 0 && w!=t )
alpar@726
   783
		  {
alpar@726
   784
		    next.set(w,first[level[w]]);
alpar@726
   785
		    first[level[w]]=w;
alpar@726
   786
		    //active[level[w]].push(w);
alpar@726
   787
		  }   
alpar@726
   788
		flow->set(e, (*capacity)[e]);
alpar@726
   789
		excess.set(w, excess[w]+rem);
alpar@726
   790
	      }
alpar@726
   791
	    }
alpar@726
   792
alpar@726
   793
	  InEdgeIt f;
alpar@726
   794
	  for(g->first(f,s); g->valid(f); g->next(f))
alpar@726
   795
	    {
alpar@726
   796
	      if ( (*flow)[f] <= 0 ) continue;
alpar@726
   797
	      Node w=g->tail(f);
alpar@726
   798
	      if ( level[w] < n ) {
alpar@726
   799
		if ( excess[w] <= 0 && w!=t )
alpar@726
   800
		  {
alpar@726
   801
		    next.set(w,first[level[w]]);
alpar@726
   802
		    first[level[w]]=w;
alpar@726
   803
		    //active[level[w]].push(w);
alpar@726
   804
		  }   
alpar@726
   805
		excess.set(w, excess[w]+(*flow)[f]);
alpar@726
   806
		flow->set(f, 0);
alpar@726
   807
	      }
alpar@726
   808
	    }
alpar@726
   809
	  break;
alpar@726
   810
	} //case PRE_FLOW
alpar@726
   811
      }
alpar@726
   812
    } //preflowPreproc
alpar@726
   813
alpar@726
   814
alpar@726
   815
alpar@726
   816
    void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
alpar@726
   817
		 VecNode& level_list, NNMap& left,
alpar@726
   818
		 NNMap& right, int& b, int& k, bool what_heur )
alpar@726
   819
    {
alpar@726
   820
alpar@726
   821
      Num lev=level[w];
alpar@726
   822
alpar@726
   823
      Node right_n=right[w];
alpar@726
   824
      Node left_n=left[w];
alpar@726
   825
alpar@726
   826
      //unlacing starts
alpar@726
   827
      if ( g->valid(right_n) ) {
alpar@726
   828
	if ( g->valid(left_n) ) {
alpar@726
   829
	  right.set(left_n, right_n);
alpar@726
   830
	  left.set(right_n, left_n);
alpar@726
   831
	} else {
alpar@726
   832
	  level_list[lev]=right_n;
alpar@726
   833
	  left.set(right_n, INVALID);
alpar@726
   834
	}
alpar@726
   835
      } else {
alpar@726
   836
	if ( g->valid(left_n) ) {
alpar@726
   837
	  right.set(left_n, INVALID);
alpar@726
   838
	} else {
alpar@726
   839
	  level_list[lev]=INVALID;
alpar@726
   840
	}
alpar@726
   841
      }
alpar@726
   842
      //unlacing ends
alpar@726
   843
alpar@726
   844
      if ( !g->valid(level_list[lev]) ) {
alpar@726
   845
alpar@726
   846
	//gapping starts
alpar@726
   847
	for (int i=lev; i!=k ; ) {
alpar@726
   848
	  Node v=level_list[++i];
alpar@726
   849
	  while ( g->valid(v) ) {
alpar@726
   850
	    level.set(v,n);
alpar@726
   851
	    v=right[v];
alpar@726
   852
	  }
alpar@726
   853
	  level_list[i]=INVALID;
alpar@726
   854
	  if ( !what_heur ) first[i]=INVALID;
alpar@726
   855
	  /*{
alpar@726
   856
	    while ( !active[i].empty() ) {
alpar@726
   857
	    active[i].pop();    //FIXME: ezt szebben kene
alpar@726
   858
	    }
alpar@726
   859
	    }*/
alpar@726
   860
	}
alpar@726
   861
alpar@726
   862
	level.set(w,n);
alpar@726
   863
	b=lev-1;
alpar@726
   864
	k=b;
alpar@726
   865
	//gapping ends
alpar@726
   866
alpar@726
   867
      } else {
alpar@726
   868
alpar@726
   869
	if ( newlevel == n ) level.set(w,n);
alpar@726
   870
	else {
alpar@726
   871
	  level.set(w,++newlevel);
alpar@726
   872
	  next.set(w,first[newlevel]);
alpar@726
   873
	  first[newlevel]=w;
alpar@726
   874
	  //	  active[newlevel].push(w);
alpar@726
   875
	  if ( what_heur ) b=newlevel;
alpar@726
   876
	  if ( k < newlevel ) ++k;      //now k=newlevel
alpar@726
   877
	  Node z=level_list[newlevel];
alpar@726
   878
	  if ( g->valid(z) ) left.set(z,w);
alpar@726
   879
	  right.set(w,z);
alpar@726
   880
	  left.set(w,INVALID);
alpar@726
   881
	  level_list[newlevel]=w;
alpar@726
   882
	}
alpar@726
   883
      }
alpar@726
   884
    } //relabel
alpar@726
   885
  };  //class MaxFlow
alpar@726
   886
} //namespace hugo
alpar@726
   887
alpar@726
   888
#endif //HUGO_MAX_FLOW_H
alpar@726
   889
alpar@726
   890
alpar@726
   891
alpar@726
   892