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