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