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