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