src/work/jacint/max_flow.h
author marci
Fri, 14 May 2004 18:08:29 +0000
changeset 641 bfd6c14e2975
parent 632 3f3e184252d2
child 647 19dd325da0e8
permissions -rw-r--r--
some documentation in stGraphWrapper<Gr> and BipartiteGraphWrapper<Gr>
marci@478
     1
// -*- C++ -*-
marci@480
     2
#ifndef HUGO_MAX_FLOW_H
marci@480
     3
#define HUGO_MAX_FLOW_H
marci@478
     4
marci@478
     5
#include <vector>
marci@478
     6
#include <queue>
marci@478
     7
#include <stack>
marci@478
     8
marci@557
     9
#include <hugo/graph_wrapper.h>
marci@602
    10
#include <bfs_dfs.h>
marci@555
    11
#include <hugo/invalid.h>
marci@555
    12
#include <hugo/maps.h>
marci@640
    13
#include <hugo/for_each_macros.h>
marci@478
    14
marci@488
    15
/// \file
jacint@631
    16
/// \brief Maximum flow algorithms.
marci@615
    17
/// \ingroup galgs
marci@478
    18
marci@478
    19
namespace hugo {
marci@478
    20
jacint@631
    21
  /// \addtogroup galgs
jacint@631
    22
  /// @{                                                                                                                                        
jacint@631
    23
  ///Maximum flow algorithms class.
marci@488
    24
jacint@631
    25
  ///This class provides various algorithms for finding a flow of
jacint@631
    26
  ///maximum value in a directed graph. The \e source node, the \e
jacint@631
    27
  ///target node, the \e capacity of the edges and the \e starting \e
jacint@631
    28
  ///flow value of the edges can be passed to the algorithm through the
jacint@631
    29
  ///constructor. It is possible to change these quantities using the
jacint@631
    30
  ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
jacint@631
    31
  ///\ref resetFlow. Before any subsequent runs of any algorithm of
jacint@631
    32
  ///the class \ref resetFlow should be called, otherwise it will
jacint@631
    33
  ///start from a maximum flow.                                                                                                                 
jacint@631
    34
  ///After running an algorithm of the class, the maximum value of a
jacint@631
    35
  ///value can be obtained by calling \ref flowValue(). The minimum
jacint@631
    36
  ///value cut can be written into a \c node map of \c bools by
jacint@631
    37
  ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
jacint@631
    38
  ///the inclusionwise minimum and maximum of the minimum value
jacint@631
    39
  ///cuts, resp.)                                                                                                                               
marci@632
    40
  ///\param Graph The directed graph type the algorithm runs on.
jacint@631
    41
  ///\param Num The number type of the capacities and the flow values.
jacint@631
    42
  ///\param CapMap The type of the capacity map.
jacint@631
    43
  ///\param FlowMap The type of the flow map.                                                                                                           
jacint@631
    44
  ///\author Marton Makai, Jacint Szabo 
marci@615
    45
  template <typename Graph, typename Num,
marci@615
    46
	    typename CapMap=typename Graph::template EdgeMap<Num>,
marci@478
    47
            typename FlowMap=typename Graph::template EdgeMap<Num> >
marci@478
    48
  class MaxFlow {
marci@615
    49
  protected:
marci@478
    50
    typedef typename Graph::Node Node;
marci@478
    51
    typedef typename Graph::NodeIt NodeIt;
jacint@631
    52
    typedef typename Graph::EdgeIt EdgeIt;
marci@478
    53
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@478
    54
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@478
    55
marci@478
    56
    typedef typename std::vector<std::stack<Node> > VecStack;
marci@478
    57
    typedef typename Graph::template NodeMap<Node> NNMap;
marci@478
    58
    typedef typename std::vector<Node> VecNode;
marci@478
    59
marci@478
    60
    const Graph* g;
marci@478
    61
    Node s;
marci@478
    62
    Node t;
marci@615
    63
    const CapMap* capacity;
marci@478
    64
    FlowMap* flow;
marci@478
    65
    int n;      //the number of nodes of G
marci@478
    66
    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
marci@478
    67
    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
marci@478
    68
    typedef typename ResGW::Edge ResGWEdge;
marci@478
    69
    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
marci@478
    70
    typedef typename Graph::template NodeMap<int> ReachedMap;
jacint@631
    71
jacint@631
    72
jacint@631
    73
    //level works as a bool map in augmenting path algorithms and is
jacint@631
    74
    //used by bfs for storing reached information.  In preflow, it
jacint@631
    75
    //shows the levels of nodes.     
marci@478
    76
    ReachedMap level;
jacint@631
    77
jacint@631
    78
    //excess is needed only in preflow
marci@615
    79
    typename Graph::template NodeMap<Num> excess;
jacint@631
    80
jacint@631
    81
    //fixme    
jacint@631
    82
//   protected:
marci@602
    83
    //     MaxFlow() { }
marci@615
    84
    //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
marci@615
    85
    // 	     FlowMap& _flow)
marci@602
    86
    //       {
marci@615
    87
    // 	g=&_G;
marci@615
    88
    // 	s=_s;
marci@615
    89
    // 	t=_t;
marci@602
    90
    // 	capacity=&_capacity;
marci@602
    91
    // 	flow=&_flow;
marci@602
    92
    // 	n=_G.nodeNum;
marci@615
    93
    // 	level.set (_G); //kellene vmi ilyesmi fv
marci@602
    94
    // 	excess(_G,0); //itt is
marci@602
    95
    //       }
marci@478
    96
marci@615
    97
    // constants used for heuristics
marci@615
    98
    static const int H0=20;
marci@615
    99
    static const int H1=1;
marci@615
   100
marci@478
   101
  public:
marci@615
   102
jacint@631
   103
    ///Indicates the property of the starting flow.
jacint@631
   104
jacint@631
   105
    ///Indicates the property of the starting flow. The meanings are as follows:
jacint@631
   106
    ///- \c ZERO_FLOW: constant zero flow
jacint@631
   107
    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
jacint@631
   108
    ///the sum of the out-flows in every node except the \e source and
jacint@631
   109
    ///the \e target.
jacint@631
   110
    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
jacint@631
   111
    ///least the sum of the out-flows in every node except the \e source.
jacint@631
   112
    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
jacint@631
   113
    ///set to the constant zero flow in the beginning of the algorithm in this case.
marci@478
   114
    enum flowEnum{
marci@615
   115
      ZERO_FLOW,
marci@615
   116
      GEN_FLOW,
marci@615
   117
      PRE_FLOW,
marci@615
   118
      NO_FLOW
marci@478
   119
    };
marci@478
   120
marci@615
   121
    MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
marci@478
   122
	    FlowMap& _flow) :
marci@615
   123
      g(&_G), s(_s), t(_t), capacity(&_capacity),
marci@478
   124
      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
marci@478
   125
jacint@631
   126
    ///Runs a maximum flow algorithm.
jacint@631
   127
jacint@631
   128
    ///Runs a preflow algorithm, which is the fastest maximum flow
jacint@631
   129
    ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
jacint@631
   130
    ///\pre The starting flow must be
jacint@631
   131
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
jacint@631
   132
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
jacint@631
   133
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
jacint@631
   134
    /// - any map if \c fe is NO_FLOW.
marci@615
   135
    void run(flowEnum fe=ZERO_FLOW) {
marci@615
   136
      preflow(fe);
marci@478
   137
    }
marci@615
   138
jacint@631
   139
                                                                                             
jacint@631
   140
    ///Runs a preflow algorithm.  
jacint@631
   141
jacint@631
   142
    ///Runs a preflow algorithm. The preflow algorithms provide the
jacint@631
   143
    ///fastest way to compute a maximum flow in a directed graph.
jacint@631
   144
    ///\pre The starting flow must be
jacint@631
   145
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
jacint@631
   146
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
jacint@631
   147
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
jacint@631
   148
    /// - any map if \c fe is NO_FLOW.
marci@488
   149
    void preflow(flowEnum fe) {
jacint@631
   150
      preflowPhase1(fe);
jacint@631
   151
      preflowPhase2();
marci@478
   152
    }
jacint@631
   153
    // Heuristics:
jacint@631
   154
    //   2 phase
jacint@631
   155
    //   gap
jacint@631
   156
    //   list 'level_list' on the nodes on level i implemented by hand
jacint@631
   157
    //   stack 'active' on the active nodes on level i                                                                                    
jacint@631
   158
    //   runs heuristic 'highest label' for H1*n relabels
jacint@631
   159
    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
jacint@631
   160
    //   Parameters H0 and H1 are initialized to 20 and 1.
marci@478
   161
jacint@631
   162
    ///Runs the first phase of the preflow algorithm.
marci@478
   163
jacint@631
   164
    ///The preflow algorithm consists of two phases, this method runs the
jacint@631
   165
    ///first phase. After the first phase the maximum flow value and a
jacint@631
   166
    ///minimum value cut can already be computed, though a maximum flow
jacint@631
   167
    ///is net yet obtained. So after calling this method \ref flowValue
jacint@631
   168
    ///and \ref actMinCut gives proper results.
jacint@631
   169
    ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
jacint@631
   170
    ///give minimum value cuts unless calling \ref preflowPhase2.
jacint@631
   171
    ///\pre The starting flow must be
jacint@631
   172
    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
jacint@631
   173
    /// - an arbitary flow if \c fe is \c GEN_FLOW,
jacint@631
   174
    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
jacint@631
   175
    /// - any map if \c fe is NO_FLOW.
jacint@631
   176
    void preflowPhase1( flowEnum fe );
jacint@631
   177
jacint@631
   178
    ///Runs the second phase of the preflow algorithm.
jacint@631
   179
jacint@631
   180
    ///The preflow algorithm consists of two phases, this method runs
jacint@631
   181
    ///the second phase. After calling \ref preflowPhase1 and then
jacint@631
   182
    ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
jacint@631
   183
    ///\ref minMinCut and \ref maxMinCut give proper results.
jacint@631
   184
    ///\pre \ref preflowPhase1 must be called before.
jacint@631
   185
    void preflowPhase2();
marci@478
   186
marci@615
   187
    /// Starting from a flow, this method searches for an augmenting path
marci@615
   188
    /// according to the Edmonds-Karp algorithm
marci@615
   189
    /// and augments the flow on if any.
marci@487
   190
    /// The return value shows if the augmentation was succesful.
marci@478
   191
    bool augmentOnShortestPath();
marci@478
   192
marci@615
   193
    /// Starting from a flow, this method searches for an augmenting blocking
marci@615
   194
    /// flow according to Dinits' algorithm and augments the flow on if any.
marci@615
   195
    /// The blocking flow is computed in a physically constructed
marci@485
   196
    /// residual graph of type \c Mutablegraph.
marci@487
   197
    /// The return value show sif the augmentation was succesful.
marci@478
   198
    template<typename MutableGraph> bool augmentOnBlockingFlow();
marci@478
   199
marci@615
   200
    /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
marci@485
   201
    /// residual graph is not constructed physically.
marci@487
   202
    /// The return value shows if the augmentation was succesful.
marci@478
   203
    bool augmentOnBlockingFlow2();
marci@478
   204
jacint@631
   205
    /// Returns the maximum value of a flow.
jacint@631
   206
jacint@631
   207
    /// Returns the maximum value of a flow, by counting the 
jacint@631
   208
    /// over-flow of the target node \ref t.
jacint@631
   209
    /// It can be called already after running \ref preflowPhase1.
marci@615
   210
    Num flowValue() {
marci@478
   211
      Num a=0;
marci@615
   212
      FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
marci@615
   213
      FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
marci@478
   214
      return a;
jacint@631
   215
      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
marci@478
   216
    }
marci@478
   217
jacint@631
   218
    ///Returns a minimum value cut after calling \ref preflowPhase1.
jacint@631
   219
jacint@631
   220
    ///After the first phase of the preflow algorithm the maximum flow
jacint@631
   221
    ///value and a minimum value cut can already be computed. This
jacint@631
   222
    ///method can be called after running \ref preflowPhase1 for
jacint@631
   223
    ///obtaining a minimum value cut.
jacint@631
   224
    /// \warning Gives proper result only right after calling \ref
jacint@631
   225
    /// preflowPhase1.
marci@615
   226
    /// \todo We have to make some status variable which shows the
marci@615
   227
    /// actual state
marci@615
   228
    /// of the class. This enables us to determine which methods are valid
marci@485
   229
    /// for MinCut computation
marci@478
   230
    template<typename _CutMap>
marci@478
   231
    void actMinCut(_CutMap& M) {
marci@478
   232
      NodeIt v;
marci@485
   233
      for(g->first(v); g->valid(v); g->next(v)) {
marci@485
   234
	if ( level[v] < n ) {
marci@485
   235
	  M.set(v,false);
marci@485
   236
	} else {
marci@485
   237
	  M.set(v,true);
marci@485
   238
	}
marci@478
   239
      }
marci@478
   240
    }
marci@478
   241
jacint@631
   242
    ///Returns the inclusionwise minimum of the minimum value cuts.
jacint@631
   243
jacint@631
   244
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@631
   245
    ///which is inclusionwise minimum. It is computed by processing
jacint@631
   246
    ///a bfs from the source node \c s in the residual graph.
jacint@631
   247
    ///\pre M should be a node map of bools initialized to false.
jacint@631
   248
    ///\pre \c flow must be a maximum flow.
marci@478
   249
    template<typename _CutMap>
marci@478
   250
    void minMinCut(_CutMap& M) {
marci@615
   251
marci@478
   252
      std::queue<Node> queue;
marci@615
   253
marci@615
   254
      M.set(s,true);
marci@478
   255
      queue.push(s);
marci@478
   256
marci@478
   257
      while (!queue.empty()) {
marci@478
   258
        Node w=queue.front();
marci@478
   259
	queue.pop();
marci@478
   260
marci@478
   261
	OutEdgeIt e;
marci@478
   262
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
marci@478
   263
	  Node v=g->head(e);
marci@478
   264
	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
marci@478
   265
	    queue.push(v);
marci@478
   266
	    M.set(v, true);
marci@478
   267
	  }
marci@615
   268
	}
marci@478
   269
marci@478
   270
	InEdgeIt f;
marci@478
   271
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
marci@478
   272
	  Node v=g->tail(f);
marci@478
   273
	  if (!M[v] && (*flow)[f] > 0 ) {
marci@478
   274
	    queue.push(v);
marci@478
   275
	    M.set(v, true);
marci@478
   276
	  }
marci@615
   277
	}
marci@478
   278
      }
marci@478
   279
    }
marci@478
   280
jacint@631
   281
    ///Returns the inclusionwise maximum of the minimum value cuts.
marci@478
   282
jacint@631
   283
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@631
   284
    ///which is inclusionwise maximum. It is computed by processing a
jacint@631
   285
    ///backward bfs from the target node \c t in the residual graph.
jacint@631
   286
    ///\pre M should be a node map of bools initialized to false.
jacint@631
   287
    ///\pre \c flow must be a maximum flow. 
marci@478
   288
    template<typename _CutMap>
marci@478
   289
    void maxMinCut(_CutMap& M) {
marci@478
   290
marci@478
   291
      NodeIt v;
marci@478
   292
      for(g->first(v) ; g->valid(v); g->next(v)) {
marci@478
   293
	M.set(v, true);
marci@478
   294
      }
marci@478
   295
marci@478
   296
      std::queue<Node> queue;
marci@615
   297
marci@615
   298
      M.set(t,false);
marci@478
   299
      queue.push(t);
marci@478
   300
marci@478
   301
      while (!queue.empty()) {
marci@478
   302
        Node w=queue.front();
marci@478
   303
	queue.pop();
marci@478
   304
marci@478
   305
	InEdgeIt e;
marci@478
   306
	for(g->first(e,w) ; g->valid(e); g->next(e)) {
marci@478
   307
	  Node v=g->tail(e);
marci@478
   308
	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
marci@478
   309
	    queue.push(v);
marci@478
   310
	    M.set(v, false);
marci@478
   311
	  }
marci@478
   312
	}
marci@615
   313
marci@478
   314
	OutEdgeIt f;
marci@478
   315
	for(g->first(f,w) ; g->valid(f); g->next(f)) {
marci@478
   316
	  Node v=g->head(f);
marci@478
   317
	  if (M[v] && (*flow)[f] > 0 ) {
marci@478
   318
	    queue.push(v);
marci@478
   319
	    M.set(v, false);
marci@478
   320
	  }
marci@478
   321
	}
marci@478
   322
      }
marci@478
   323
    }
marci@478
   324
jacint@631
   325
    ///Returns a minimum value cut.
marci@478
   326
jacint@631
   327
    ///Sets \c M to the characteristic vector of a minimum value cut.
jacint@631
   328
    ///\pre M should be a node map of bools initialized to false.
jacint@631
   329
    ///\pre \c flow must be a maximum flow.    
marci@478
   330
    template<typename CutMap>
marci@485
   331
    void minCut(CutMap& M) { minMinCut(M); }
marci@478
   332
jacint@631
   333
    ///Resets the source node to \c _s.
jacint@631
   334
jacint@631
   335
    ///Resets the source node to \c _s.
jacint@631
   336
    /// 
marci@487
   337
    void resetSource(Node _s) { s=_s; }
jacint@631
   338
jacint@631
   339
    ///Resets the target node to \c _t.
jacint@631
   340
jacint@631
   341
    ///Resets the target node to \c _t.
marci@487
   342
    ///
marci@487
   343
    void resetTarget(Node _t) { t=_t; }
marci@615
   344
jacint@631
   345
    /// Resets the edge map of the capacities to _cap.
jacint@631
   346
jacint@631
   347
    /// Resets the edge map of the capacities to _cap.
jacint@631
   348
    /// 
marci@485
   349
    void resetCap(const CapMap& _cap) { capacity=&_cap; }
marci@615
   350
jacint@631
   351
    /// Resets the edge map of the flows to _flow.
jacint@631
   352
jacint@631
   353
    /// Resets the edge map of the flows to _flow.
jacint@631
   354
    /// 
marci@485
   355
    void resetFlow(FlowMap& _flow) { flow=&_flow; }
marci@478
   356
marci@478
   357
marci@478
   358
  private:
marci@478
   359
marci@478
   360
    int push(Node w, VecStack& active) {
marci@615
   361
marci@478
   362
      int lev=level[w];
marci@478
   363
      Num exc=excess[w];
marci@478
   364
      int newlevel=n;       //bound on the next level of w
marci@615
   365
marci@478
   366
      OutEdgeIt e;
marci@478
   367
      for(g->first(e,w); g->valid(e); g->next(e)) {
marci@615
   368
marci@615
   369
	if ( (*flow)[e] >= (*capacity)[e] ) continue;
marci@615
   370
	Node v=g->head(e);
marci@615
   371
marci@478
   372
	if( lev > level[v] ) { //Push is allowed now
marci@615
   373
marci@478
   374
	  if ( excess[v]<=0 && v!=t && v!=s ) {
marci@478
   375
	    int lev_v=level[v];
marci@478
   376
	    active[lev_v].push(v);
marci@478
   377
	  }
marci@615
   378
marci@478
   379
	  Num cap=(*capacity)[e];
marci@478
   380
	  Num flo=(*flow)[e];
marci@478
   381
	  Num remcap=cap-flo;
marci@615
   382
marci@478
   383
	  if ( remcap >= exc ) { //A nonsaturating push.
marci@615
   384
marci@478
   385
	    flow->set(e, flo+exc);
marci@478
   386
	    excess.set(v, excess[v]+exc);
marci@478
   387
	    exc=0;
marci@615
   388
	    break;
marci@615
   389
marci@478
   390
	  } else { //A saturating push.
marci@478
   391
	    flow->set(e, cap);
marci@478
   392
	    excess.set(v, excess[v]+remcap);
marci@478
   393
	    exc-=remcap;
marci@478
   394
	  }
marci@478
   395
	} else if ( newlevel > level[v] ) newlevel = level[v];
marci@615
   396
      } //for out edges wv
marci@615
   397
marci@615
   398
      if ( exc > 0 ) {
marci@478
   399
	InEdgeIt e;
marci@478
   400
	for(g->first(e,w); g->valid(e); g->next(e)) {
marci@615
   401
marci@615
   402
	  if( (*flow)[e] <= 0 ) continue;
marci@615
   403
	  Node v=g->tail(e);
marci@615
   404
marci@478
   405
	  if( lev > level[v] ) { //Push is allowed now
marci@615
   406
marci@478
   407
	    if ( excess[v]<=0 && v!=t && v!=s ) {
marci@478
   408
	      int lev_v=level[v];
marci@478
   409
	      active[lev_v].push(v);
marci@478
   410
	    }
marci@615
   411
marci@478
   412
	    Num flo=(*flow)[e];
marci@615
   413
marci@478
   414
	    if ( flo >= exc ) { //A nonsaturating push.
marci@615
   415
marci@478
   416
	      flow->set(e, flo-exc);
marci@478
   417
	      excess.set(v, excess[v]+exc);
marci@478
   418
	      exc=0;
marci@615
   419
	      break;
marci@478
   420
	    } else {  //A saturating push.
marci@615
   421
marci@478
   422
	      excess.set(v, excess[v]+flo);
marci@478
   423
	      exc-=flo;
marci@478
   424
	      flow->set(e,0);
marci@615
   425
	    }
marci@478
   426
	  } else if ( newlevel > level[v] ) newlevel = level[v];
marci@478
   427
	} //for in edges vw
marci@615
   428
marci@478
   429
      } // if w still has excess after the out edge for cycle
marci@615
   430
marci@478
   431
      excess.set(w, exc);
marci@615
   432
marci@478
   433
      return newlevel;
marci@485
   434
    }
marci@478
   435
marci@478
   436
marci@615
   437
    void preflowPreproc(flowEnum fe, VecStack& active,
marci@615
   438
			VecNode& level_list, NNMap& left, NNMap& right)
marci@602
   439
    {
marci@615
   440
      std::queue<Node> bfs_queue;
marci@478
   441
marci@615
   442
      switch (fe) {
jacint@631
   443
      case NO_FLOW:   //flow is already set to const zero in this case
marci@615
   444
      case ZERO_FLOW:
marci@602
   445
	{
marci@602
   446
	  //Reverse_bfs from t, to find the starting level.
marci@602
   447
	  level.set(t,0);
marci@602
   448
	  bfs_queue.push(t);
marci@615
   449
marci@602
   450
	  while (!bfs_queue.empty()) {
marci@615
   451
marci@615
   452
	    Node v=bfs_queue.front();
marci@602
   453
	    bfs_queue.pop();
marci@602
   454
	    int l=level[v]+1;
marci@615
   455
marci@602
   456
	    InEdgeIt e;
marci@602
   457
	    for(g->first(e,v); g->valid(e); g->next(e)) {
marci@602
   458
	      Node w=g->tail(e);
marci@602
   459
	      if ( level[w] == n && w != s ) {
marci@602
   460
		bfs_queue.push(w);
marci@602
   461
		Node first=level_list[l];
marci@602
   462
		if ( g->valid(first) ) left.set(first,w);
marci@602
   463
		right.set(w,first);
marci@602
   464
		level_list[l]=w;
marci@602
   465
		level.set(w, l);
marci@602
   466
	      }
marci@602
   467
	    }
marci@602
   468
	  }
marci@615
   469
marci@602
   470
	  //the starting flow
marci@602
   471
	  OutEdgeIt e;
marci@615
   472
	  for(g->first(e,s); g->valid(e); g->next(e))
marci@602
   473
	    {
marci@602
   474
	      Num c=(*capacity)[e];
marci@602
   475
	      if ( c <= 0 ) continue;
marci@602
   476
	      Node w=g->head(e);
marci@615
   477
	      if ( level[w] < n ) {
marci@602
   478
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@615
   479
		flow->set(e, c);
marci@602
   480
		excess.set(w, excess[w]+c);
marci@602
   481
	      }
marci@602
   482
	    }
marci@602
   483
	  break;
marci@602
   484
	}
marci@615
   485
marci@602
   486
      case GEN_FLOW:
marci@615
   487
      case PRE_FLOW:
marci@602
   488
	{
marci@615
   489
	  //Reverse_bfs from t in the residual graph,
marci@602
   490
	  //to find the starting level.
marci@602
   491
	  level.set(t,0);
marci@602
   492
	  bfs_queue.push(t);
marci@615
   493
marci@602
   494
	  while (!bfs_queue.empty()) {
marci@615
   495
marci@615
   496
	    Node v=bfs_queue.front();
marci@602
   497
	    bfs_queue.pop();
marci@602
   498
	    int l=level[v]+1;
marci@615
   499
marci@602
   500
	    InEdgeIt e;
marci@602
   501
	    for(g->first(e,v); g->valid(e); g->next(e)) {
marci@602
   502
	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@602
   503
	      Node w=g->tail(e);
marci@602
   504
	      if ( level[w] == n && w != s ) {
marci@602
   505
		bfs_queue.push(w);
marci@602
   506
		Node first=level_list[l];
marci@602
   507
		if ( g->valid(first) ) left.set(first,w);
marci@602
   508
		right.set(w,first);
marci@602
   509
		level_list[l]=w;
marci@602
   510
		level.set(w, l);
marci@602
   511
	      }
marci@602
   512
	    }
marci@615
   513
marci@602
   514
	    OutEdgeIt f;
marci@602
   515
	    for(g->first(f,v); g->valid(f); g->next(f)) {
marci@602
   516
	      if ( 0 >= (*flow)[f] ) continue;
marci@602
   517
	      Node w=g->head(f);
marci@602
   518
	      if ( level[w] == n && w != s ) {
marci@602
   519
		bfs_queue.push(w);
marci@602
   520
		Node first=level_list[l];
marci@602
   521
		if ( g->valid(first) ) left.set(first,w);
marci@602
   522
		right.set(w,first);
marci@602
   523
		level_list[l]=w;
marci@602
   524
		level.set(w, l);
marci@602
   525
	      }
marci@602
   526
	    }
marci@602
   527
	  }
marci@615
   528
marci@615
   529
marci@602
   530
	  //the starting flow
marci@602
   531
	  OutEdgeIt e;
marci@615
   532
	  for(g->first(e,s); g->valid(e); g->next(e))
marci@602
   533
	    {
marci@602
   534
	      Num rem=(*capacity)[e]-(*flow)[e];
marci@602
   535
	      if ( rem <= 0 ) continue;
marci@602
   536
	      Node w=g->head(e);
marci@615
   537
	      if ( level[w] < n ) {
marci@602
   538
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@615
   539
		flow->set(e, (*capacity)[e]);
marci@602
   540
		excess.set(w, excess[w]+rem);
marci@602
   541
	      }
marci@602
   542
	    }
marci@615
   543
marci@602
   544
	  InEdgeIt f;
marci@615
   545
	  for(g->first(f,s); g->valid(f); g->next(f))
marci@602
   546
	    {
marci@602
   547
	      if ( (*flow)[f] <= 0 ) continue;
marci@602
   548
	      Node w=g->tail(f);
marci@615
   549
	      if ( level[w] < n ) {
marci@602
   550
		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
marci@602
   551
		excess.set(w, excess[w]+(*flow)[f]);
marci@615
   552
		flow->set(f, 0);
marci@602
   553
	      }
marci@615
   554
	    }
marci@602
   555
	  break;
marci@615
   556
	} //case PRE_FLOW
marci@602
   557
      }
marci@602
   558
    } //preflowPreproc
marci@478
   559
marci@478
   560
marci@478
   561
marci@615
   562
    void relabel(Node w, int newlevel, VecStack& active,
marci@615
   563
		 VecNode& level_list, NNMap& left,
marci@615
   564
		 NNMap& right, int& b, int& k, bool what_heur )
marci@478
   565
    {
marci@478
   566
marci@615
   567
      Num lev=level[w];
marci@615
   568
marci@478
   569
      Node right_n=right[w];
marci@478
   570
      Node left_n=left[w];
marci@615
   571
marci@478
   572
      //unlacing starts
marci@478
   573
      if ( g->valid(right_n) ) {
marci@478
   574
	if ( g->valid(left_n) ) {
marci@478
   575
	  right.set(left_n, right_n);
marci@478
   576
	  left.set(right_n, left_n);
marci@478
   577
	} else {
marci@615
   578
	  level_list[lev]=right_n;
marci@478
   579
	  left.set(right_n, INVALID);
marci@615
   580
	}
marci@478
   581
      } else {
marci@478
   582
	if ( g->valid(left_n) ) {
marci@478
   583
	  right.set(left_n, INVALID);
marci@615
   584
	} else {
marci@615
   585
	  level_list[lev]=INVALID;
marci@615
   586
	}
marci@615
   587
      }
marci@478
   588
      //unlacing ends
marci@615
   589
marci@478
   590
      if ( !g->valid(level_list[lev]) ) {
marci@615
   591
marci@478
   592
	//gapping starts
marci@478
   593
	for (int i=lev; i!=k ; ) {
marci@478
   594
	  Node v=level_list[++i];
marci@478
   595
	  while ( g->valid(v) ) {
marci@478
   596
	    level.set(v,n);
marci@478
   597
	    v=right[v];
marci@478
   598
	  }
marci@478
   599
	  level_list[i]=INVALID;
marci@478
   600
	  if ( !what_heur ) {
marci@478
   601
	    while ( !active[i].empty() ) {
marci@478
   602
	      active[i].pop();    //FIXME: ezt szebben kene
marci@478
   603
	    }
marci@615
   604
	  }
marci@478
   605
	}
marci@615
   606
marci@478
   607
	level.set(w,n);
marci@478
   608
	b=lev-1;
marci@478
   609
	k=b;
marci@478
   610
	//gapping ends
marci@615
   611
marci@478
   612
      } else {
marci@615
   613
marci@615
   614
	if ( newlevel == n ) level.set(w,n);
marci@478
   615
	else {
marci@478
   616
	  level.set(w,++newlevel);
marci@478
   617
	  active[newlevel].push(w);
marci@478
   618
	  if ( what_heur ) b=newlevel;
marci@478
   619
	  if ( k < newlevel ) ++k;      //now k=newlevel
marci@478
   620
	  Node first=level_list[newlevel];
marci@478
   621
	  if ( g->valid(first) ) left.set(first,w);
marci@478
   622
	  right.set(w,first);
marci@478
   623
	  left.set(w,INVALID);
marci@478
   624
	  level_list[newlevel]=w;
marci@478
   625
	}
marci@478
   626
      }
marci@615
   627
marci@478
   628
    } //relabel
marci@478
   629
marci@478
   630
marci@615
   631
    template<typename MapGraphWrapper>
marci@478
   632
    class DistanceMap {
marci@478
   633
    protected:
marci@478
   634
      const MapGraphWrapper* g;
marci@615
   635
      typename MapGraphWrapper::template NodeMap<int> dist;
marci@478
   636
    public:
marci@478
   637
      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
marci@615
   638
      void set(const typename MapGraphWrapper::Node& n, int a) {
marci@615
   639
	dist.set(n, a);
marci@478
   640
      }
marci@615
   641
      int operator[](const typename MapGraphWrapper::Node& n)
marci@485
   642
      { return dist[n]; }
marci@615
   643
      //       int get(const typename MapGraphWrapper::Node& n) const {
marci@485
   644
      // 	return dist[n]; }
marci@615
   645
      //       bool get(const typename MapGraphWrapper::Edge& e) const {
marci@485
   646
      // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
marci@615
   647
      bool operator[](const typename MapGraphWrapper::Edge& e) const {
marci@615
   648
	return (dist[g->tail(e)]<dist[g->head(e)]);
marci@478
   649
      }
marci@478
   650
    };
marci@615
   651
marci@478
   652
  };
marci@478
   653
marci@478
   654
marci@478
   655
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@631
   656
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1( flowEnum fe )
marci@478
   657
  {
marci@615
   658
marci@615
   659
    int heur0=(int)(H0*n);  //time while running 'bound decrease'
marci@485
   660
    int heur1=(int)(H1*n);  //time while running 'highest label'
marci@485
   661
    int heur=heur1;         //starting time interval (#of relabels)
marci@485
   662
    int numrelabel=0;
marci@615
   663
marci@615
   664
    bool what_heur=1;
marci@485
   665
    //It is 0 in case 'bound decrease' and 1 in case 'highest label'
marci@478
   666
marci@615
   667
    bool end=false;
marci@615
   668
    //Needed for 'bound decrease', true means no active nodes are above bound
marci@615
   669
    //b.
marci@478
   670
marci@485
   671
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   672
    int b=k;    //bound on the highest level under n of an active node
marci@615
   673
marci@485
   674
    VecStack active(n);
marci@615
   675
marci@485
   676
    NNMap left(*g, INVALID);
marci@485
   677
    NNMap right(*g, INVALID);
marci@485
   678
    VecNode level_list(n,INVALID);
marci@485
   679
    //List of the nodes in level i<n, set to n.
marci@478
   680
marci@485
   681
    NodeIt v;
marci@485
   682
    for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
marci@485
   683
    //setting each node to level n
marci@615
   684
jacint@631
   685
    if ( fe == NO_FLOW ) {
jacint@631
   686
      EdgeIt e;
jacint@631
   687
      for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
jacint@631
   688
    }
jacint@631
   689
jacint@631
   690
    switch (fe) { //computing the excess
marci@615
   691
    case PRE_FLOW:
marci@485
   692
      {
marci@485
   693
	NodeIt v;
marci@485
   694
	for(g->first(v); g->valid(v); g->next(v)) {
marci@478
   695
	  Num exc=0;
marci@615
   696
marci@478
   697
	  InEdgeIt e;
marci@485
   698
	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@478
   699
	  OutEdgeIt f;
marci@485
   700
	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@615
   701
marci@615
   702
	  excess.set(v,exc);
marci@615
   703
marci@485
   704
	  //putting the active nodes into the stack
marci@485
   705
	  int lev=level[v];
marci@485
   706
	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
marci@478
   707
	}
marci@478
   708
	break;
marci@478
   709
      }
marci@485
   710
    case GEN_FLOW:
marci@485
   711
      {
jacint@631
   712
	NodeIt v;
jacint@631
   713
	for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
jacint@631
   714
marci@485
   715
	Num exc=0;
marci@485
   716
	InEdgeIt e;
marci@485
   717
	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
marci@485
   718
	OutEdgeIt f;
marci@485
   719
	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
marci@615
   720
	excess.set(t,exc);
marci@485
   721
	break;
marci@485
   722
      }
jacint@631
   723
    case ZERO_FLOW:
jacint@631
   724
    case NO_FLOW:
jacint@631
   725
      {
jacint@631
   726
	NodeIt v;
jacint@631
   727
        for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
jacint@631
   728
	break;
jacint@631
   729
      }
marci@485
   730
    }
marci@615
   731
marci@615
   732
    preflowPreproc(fe, active, level_list, left, right);
marci@615
   733
    //End of preprocessing
marci@615
   734
marci@615
   735
marci@485
   736
    //Push/relabel on the highest level active nodes.
marci@485
   737
    while ( true ) {
marci@485
   738
      if ( b == 0 ) {
marci@485
   739
	if ( !what_heur && !end && k > 0 ) {
marci@485
   740
	  b=k;
marci@485
   741
	  end=true;
marci@485
   742
	} else break;
marci@485
   743
      }
marci@615
   744
marci@615
   745
      if ( active[b].empty() ) --b;
marci@485
   746
      else {
marci@615
   747
	end=false;
marci@485
   748
	Node w=active[b].top();
marci@485
   749
	active[b].pop();
marci@485
   750
	int newlevel=push(w,active);
marci@615
   751
	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
marci@485
   752
				     left, right, b, k, what_heur);
marci@615
   753
marci@615
   754
	++numrelabel;
marci@485
   755
	if ( numrelabel >= heur ) {
marci@485
   756
	  numrelabel=0;
marci@485
   757
	  if ( what_heur ) {
marci@485
   758
	    what_heur=0;
marci@485
   759
	    heur=heur0;
marci@485
   760
	    end=false;
marci@485
   761
	  } else {
marci@485
   762
	    what_heur=1;
marci@485
   763
	    heur=heur1;
marci@615
   764
	    b=k;
marci@485
   765
	  }
marci@478
   766
	}
marci@615
   767
      }
marci@615
   768
    }
marci@485
   769
  }
marci@478
   770
marci@478
   771
marci@478
   772
marci@478
   773
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@631
   774
  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
marci@478
   775
  {
marci@615
   776
marci@485
   777
    int k=n-2;  //bound on the highest level under n containing a node
marci@485
   778
    int b=k;    //bound on the highest level under n of an active node
marci@615
   779
marci@485
   780
    VecStack active(n);
marci@485
   781
    level.set(s,0);
marci@485
   782
    std::queue<Node> bfs_queue;
marci@485
   783
    bfs_queue.push(s);
marci@615
   784
marci@485
   785
    while (!bfs_queue.empty()) {
marci@615
   786
marci@615
   787
      Node v=bfs_queue.front();
marci@485
   788
      bfs_queue.pop();
marci@485
   789
      int l=level[v]+1;
marci@615
   790
marci@485
   791
      InEdgeIt e;
marci@485
   792
      for(g->first(e,v); g->valid(e); g->next(e)) {
marci@485
   793
	if ( (*capacity)[e] <= (*flow)[e] ) continue;
marci@485
   794
	Node u=g->tail(e);
marci@615
   795
	if ( level[u] >= n ) {
marci@485
   796
	  bfs_queue.push(u);
marci@485
   797
	  level.set(u, l);
marci@485
   798
	  if ( excess[u] > 0 ) active[l].push(u);
marci@478
   799
	}
marci@478
   800
      }
marci@615
   801
marci@485
   802
      OutEdgeIt f;
marci@485
   803
      for(g->first(f,v); g->valid(f); g->next(f)) {
marci@485
   804
	if ( 0 >= (*flow)[f] ) continue;
marci@485
   805
	Node u=g->head(f);
marci@615
   806
	if ( level[u] >= n ) {
marci@485
   807
	  bfs_queue.push(u);
marci@485
   808
	  level.set(u, l);
marci@485
   809
	  if ( excess[u] > 0 ) active[l].push(u);
marci@485
   810
	}
marci@485
   811
      }
marci@485
   812
    }
marci@485
   813
    b=n-2;
marci@478
   814
marci@485
   815
    while ( true ) {
marci@615
   816
marci@485
   817
      if ( b == 0 ) break;
marci@478
   818
marci@615
   819
      if ( active[b].empty() ) --b;
marci@485
   820
      else {
marci@485
   821
	Node w=active[b].top();
marci@485
   822
	active[b].pop();
marci@615
   823
	int newlevel=push(w,active);
marci@478
   824
marci@485
   825
	//relabel
marci@485
   826
	if ( excess[w] > 0 ) {
marci@485
   827
	  level.set(w,++newlevel);
marci@485
   828
	  active[newlevel].push(w);
marci@485
   829
	  b=newlevel;
marci@485
   830
	}
marci@485
   831
      }  // if stack[b] is nonempty
marci@485
   832
    } // while(true)
marci@485
   833
  }
marci@478
   834
marci@478
   835
marci@478
   836
marci@478
   837
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@615
   838
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
marci@478
   839
  {
marci@485
   840
    ResGW res_graph(*g, *capacity, *flow);
marci@485
   841
    bool _augment=false;
marci@615
   842
marci@485
   843
    //ReachedMap level(res_graph);
marci@485
   844
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   845
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   846
    bfs.pushAndSetReached(s);
marci@615
   847
marci@615
   848
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
marci@485
   849
    pred.set(s, INVALID);
marci@615
   850
marci@485
   851
    typename ResGW::template NodeMap<Num> free(res_graph);
marci@615
   852
marci@485
   853
    //searching for augmenting path
marci@615
   854
    while ( !bfs.finished() ) {
marci@485
   855
      ResGWOutEdgeIt e=bfs;
marci@485
   856
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
   857
	Node v=res_graph.tail(e);
marci@485
   858
	Node w=res_graph.head(e);
marci@485
   859
	pred.set(w, e);
marci@485
   860
	if (res_graph.valid(pred[v])) {
marci@485
   861
	  free.set(w, std::min(free[v], res_graph.resCap(e)));
marci@485
   862
	} else {
marci@615
   863
	  free.set(w, res_graph.resCap(e));
marci@478
   864
	}
marci@485
   865
	if (res_graph.head(e)==t) { _augment=true; break; }
marci@485
   866
      }
marci@615
   867
marci@485
   868
      ++bfs;
marci@485
   869
    } //end of searching augmenting path
marci@478
   870
marci@485
   871
    if (_augment) {
marci@485
   872
      Node n=t;
marci@485
   873
      Num augment_value=free[t];
marci@615
   874
      while (res_graph.valid(pred[n])) {
marci@485
   875
	ResGWEdge e=pred[n];
marci@615
   876
	res_graph.augment(e, augment_value);
marci@485
   877
	n=res_graph.tail(e);
marci@478
   878
      }
marci@485
   879
    }
marci@478
   880
marci@485
   881
    return _augment;
marci@485
   882
  }
marci@478
   883
marci@478
   884
marci@478
   885
marci@478
   886
marci@478
   887
marci@478
   888
marci@478
   889
marci@478
   890
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@615
   891
  template<typename MutableGraph>
marci@615
   892
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
marci@615
   893
  {
marci@485
   894
    typedef MutableGraph MG;
marci@485
   895
    bool _augment=false;
marci@478
   896
marci@485
   897
    ResGW res_graph(*g, *capacity, *flow);
marci@478
   898
marci@485
   899
    //bfs for distances on the residual graph
marci@485
   900
    //ReachedMap level(res_graph);
marci@485
   901
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
   902
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@485
   903
    bfs.pushAndSetReached(s);
marci@615
   904
    typename ResGW::template NodeMap<int>
marci@485
   905
      dist(res_graph); //filled up with 0's
marci@478
   906
marci@485
   907
    //F will contain the physical copy of the residual graph
marci@485
   908
    //with the set of edges which are on shortest paths
marci@485
   909
    MG F;
marci@615
   910
    typename ResGW::template NodeMap<typename MG::Node>
marci@485
   911
      res_graph_to_F(res_graph);
marci@485
   912
    {
marci@485
   913
      typename ResGW::NodeIt n;
marci@485
   914
      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
marci@485
   915
	res_graph_to_F.set(n, F.addNode());
marci@478
   916
      }
marci@485
   917
    }
marci@478
   918
marci@485
   919
    typename MG::Node sF=res_graph_to_F[s];
marci@485
   920
    typename MG::Node tF=res_graph_to_F[t];
marci@485
   921
    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
marci@485
   922
    typename MG::template EdgeMap<Num> residual_capacity(F);
marci@478
   923
marci@615
   924
    while ( !bfs.finished() ) {
marci@485
   925
      ResGWOutEdgeIt e=bfs;
marci@485
   926
      if (res_graph.valid(e)) {
marci@485
   927
	if (bfs.isBNodeNewlyReached()) {
marci@485
   928
	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@615
   929
	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
marci@615
   930
					res_graph_to_F[res_graph.head(e)]);
marci@485
   931
	  original_edge.update();
marci@485
   932
	  original_edge.set(f, e);
marci@485
   933
	  residual_capacity.update();
marci@485
   934
	  residual_capacity.set(f, res_graph.resCap(e));
marci@485
   935
	} else {
marci@485
   936
	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
marci@615
   937
	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
marci@615
   938
					  res_graph_to_F[res_graph.head(e)]);
marci@478
   939
	    original_edge.update();
marci@478
   940
	    original_edge.set(f, e);
marci@478
   941
	    residual_capacity.update();
marci@478
   942
	    residual_capacity.set(f, res_graph.resCap(e));
marci@478
   943
	  }
marci@478
   944
	}
marci@485
   945
      }
marci@485
   946
      ++bfs;
marci@485
   947
    } //computing distances from s in the residual graph
marci@478
   948
marci@485
   949
    bool __augment=true;
marci@478
   950
marci@485
   951
    while (__augment) {
marci@485
   952
      __augment=false;
marci@485
   953
      //computing blocking flow with dfs
marci@485
   954
      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
marci@485
   955
      typename MG::template NodeMap<typename MG::Edge> pred(F);
marci@485
   956
      pred.set(sF, INVALID);
marci@485
   957
      //invalid iterators for sources
marci@478
   958
marci@485
   959
      typename MG::template NodeMap<Num> free(F);
marci@478
   960
marci@615
   961
      dfs.pushAndSetReached(sF);
marci@485
   962
      while (!dfs.finished()) {
marci@485
   963
	++dfs;
marci@485
   964
	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
marci@485
   965
	  if (dfs.isBNodeNewlyReached()) {
marci@485
   966
	    typename MG::Node v=F.aNode(dfs);
marci@485
   967
	    typename MG::Node w=F.bNode(dfs);
marci@485
   968
	    pred.set(w, dfs);
marci@485
   969
	    if (F.valid(pred[v])) {
marci@485
   970
	      free.set(w, std::min(free[v], residual_capacity[dfs]));
marci@485
   971
	    } else {
marci@615
   972
	      free.set(w, residual_capacity[dfs]);
marci@485
   973
	    }
marci@615
   974
	    if (w==tF) {
marci@615
   975
	      __augment=true;
marci@485
   976
	      _augment=true;
marci@615
   977
	      break;
marci@485
   978
	    }
marci@615
   979
marci@485
   980
	  } else {
marci@485
   981
	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
marci@485
   982
	  }
marci@615
   983
	}
marci@485
   984
      }
marci@485
   985
marci@485
   986
      if (__augment) {
marci@485
   987
	typename MG::Node n=tF;
marci@485
   988
	Num augment_value=free[tF];
marci@615
   989
	while (F.valid(pred[n])) {
marci@485
   990
	  typename MG::Edge e=pred[n];
marci@615
   991
	  res_graph.augment(original_edge[e], augment_value);
marci@485
   992
	  n=F.tail(e);
marci@615
   993
	  if (residual_capacity[e]==augment_value)
marci@615
   994
	    F.erase(e);
marci@615
   995
	  else
marci@485
   996
	    residual_capacity.set(e, residual_capacity[e]-augment_value);
marci@478
   997
	}
marci@485
   998
      }
marci@615
   999
marci@485
  1000
    }
marci@615
  1001
marci@485
  1002
    return _augment;
marci@485
  1003
  }
marci@478
  1004
marci@478
  1005
marci@478
  1006
marci@478
  1007
marci@478
  1008
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@615
  1009
  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
marci@478
  1010
  {
marci@485
  1011
    bool _augment=false;
marci@478
  1012
marci@485
  1013
    ResGW res_graph(*g, *capacity, *flow);
marci@615
  1014
marci@485
  1015
    //ReachedMap level(res_graph);
marci@485
  1016
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@485
  1017
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@478
  1018
marci@485
  1019
    bfs.pushAndSetReached(s);
marci@485
  1020
    DistanceMap<ResGW> dist(res_graph);
marci@615
  1021
    while ( !bfs.finished() ) {
marci@485
  1022
      ResGWOutEdgeIt e=bfs;
marci@485
  1023
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@485
  1024
	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@485
  1025
      }
marci@485
  1026
      ++bfs;
marci@485
  1027
    } //computing distances from s in the residual graph
marci@478
  1028
marci@478
  1029
      //Subgraph containing the edges on some shortest paths
marci@485
  1030
    ConstMap<typename ResGW::Node, bool> true_map(true);
marci@615
  1031
    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
marci@485
  1032
      DistanceMap<ResGW> > FilterResGW;
marci@485
  1033
    FilterResGW filter_res_graph(res_graph, true_map, dist);
marci@478
  1034
marci@615
  1035
    //Subgraph, which is able to delete edges which are already
marci@485
  1036
    //met by the dfs
marci@615
  1037
    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
marci@485
  1038
      first_out_edges(filter_res_graph);
marci@485
  1039
    typename FilterResGW::NodeIt v;
marci@615
  1040
    for(filter_res_graph.first(v); filter_res_graph.valid(v);
marci@615
  1041
	filter_res_graph.next(v))
marci@478
  1042
      {
marci@478
  1043
 	typename FilterResGW::OutEdgeIt e;
marci@478
  1044
 	filter_res_graph.first(e, v);
marci@478
  1045
 	first_out_edges.set(v, e);
marci@478
  1046
      }
marci@485
  1047
    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
marci@485
  1048
      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
marci@485
  1049
    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
marci@478
  1050
marci@485
  1051
    bool __augment=true;
marci@478
  1052
marci@485
  1053
    while (__augment) {
marci@478
  1054
marci@485
  1055
      __augment=false;
marci@485
  1056
      //computing blocking flow with dfs
marci@615
  1057
      DfsIterator< ErasingResGW,
marci@615
  1058
	typename ErasingResGW::template NodeMap<bool> >
marci@485
  1059
	dfs(erasing_res_graph);
marci@485
  1060
      typename ErasingResGW::
marci@615
  1061
	template NodeMap<typename ErasingResGW::OutEdgeIt>
marci@615
  1062
	pred(erasing_res_graph);
marci@485
  1063
      pred.set(s, INVALID);
marci@485
  1064
      //invalid iterators for sources
marci@478
  1065
marci@615
  1066
      typename ErasingResGW::template NodeMap<Num>
marci@485
  1067
	free1(erasing_res_graph);
marci@478
  1068
marci@615
  1069
      dfs.pushAndSetReached
marci@615
  1070
	///\bug hugo 0.2
marci@615
  1071
	(typename ErasingResGW::Node
marci@615
  1072
	 (typename FilterResGW::Node
marci@615
  1073
	  (typename ResGW::Node(s)
marci@615
  1074
	   )
marci@615
  1075
	  )
marci@615
  1076
	 );
marci@485
  1077
      while (!dfs.finished()) {
marci@485
  1078
	++dfs;
marci@615
  1079
	if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
marci@615
  1080
 	  {
marci@478
  1081
  	    if (dfs.isBNodeNewlyReached()) {
marci@615
  1082
marci@478
  1083
 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
marci@478
  1084
 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
marci@478
  1085
marci@478
  1086
 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
marci@478
  1087
 	      if (erasing_res_graph.valid(pred[v])) {
marci@615
  1088
 		free1.set
marci@615
  1089
		  (w, std::min(free1[v], res_graph.resCap
marci@615
  1090
			       (typename ErasingResGW::OutEdgeIt(dfs))));
marci@478
  1091
 	      } else {
marci@615
  1092
 		free1.set
marci@615
  1093
		  (w, res_graph.resCap
marci@615
  1094
		   (typename ErasingResGW::OutEdgeIt(dfs)));
marci@478
  1095
 	      }
marci@615
  1096
marci@615
  1097
 	      if (w==t) {
marci@615
  1098
 		__augment=true;
marci@478
  1099
 		_augment=true;
marci@615
  1100
 		break;
marci@478
  1101
 	      }
marci@478
  1102
 	    } else {
marci@478
  1103
 	      erasing_res_graph.erase(dfs);
marci@478
  1104
	    }
marci@478
  1105
	  }
marci@615
  1106
      }
marci@478
  1107
marci@485
  1108
      if (__augment) {
marci@615
  1109
	typename ErasingResGW::Node
marci@615
  1110
	  n=typename FilterResGW::Node(typename ResGW::Node(t));
marci@485
  1111
	// 	  typename ResGW::NodeMap<Num> a(res_graph);
marci@485
  1112
	// 	  typename ResGW::Node b;
marci@485
  1113
	// 	  Num j=a[b];
marci@485
  1114
	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
marci@485
  1115
	// 	  typename FilterResGW::Node b1;
marci@485
  1116
	// 	  Num j1=a1[b1];
marci@485
  1117
	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
marci@485
  1118
	// 	  typename ErasingResGW::Node b2;
marci@485
  1119
	// 	  Num j2=a2[b2];
marci@485
  1120
	Num augment_value=free1[n];
marci@615
  1121
	while (erasing_res_graph.valid(pred[n])) {
marci@485
  1122
	  typename ErasingResGW::OutEdgeIt e=pred[n];
marci@485
  1123
	  res_graph.augment(e, augment_value);
marci@485
  1124
	  n=erasing_res_graph.tail(e);
marci@485
  1125
	  if (res_graph.resCap(e)==0)
marci@485
  1126
	    erasing_res_graph.erase(e);
marci@478
  1127
	}
marci@478
  1128
      }
marci@615
  1129
marci@615
  1130
    } //while (__augment)
marci@615
  1131
marci@485
  1132
    return _augment;
marci@485
  1133
  }
marci@478
  1134
marci@478
  1135
marci@478
  1136
} //namespace hugo
marci@478
  1137
marci@480
  1138
#endif //HUGO_MAX_FLOW_H
marci@478
  1139
marci@478
  1140
marci@478
  1141
marci@478
  1142