src/work/jacint/max_flow_no_stack.h
author jacint
Tue, 20 Jul 2004 14:31:24 +0000
changeset 715 665689d86225
child 719 cb9efd4cc9db
permissions -rw-r--r--
trying if without stl stack we are faster
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
    ///
jacint@714
   159
    MaxFlowNoStack(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
jacint@714
   160
	    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
    /// 
jacint@714
   416
    void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
jacint@714
   417
jacint@714
   418
    /// Resets the edge map of the flows to _flow.
jacint@714
   419
jacint@714
   420
    /// Resets the edge map of the flows to _flow.
jacint@714
   421
    /// 
jacint@714
   422
    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
jacint@714
   423
jacint@714
   424
jacint@714
   425
  private:
jacint@714
   426
jacint@714
   427
    int push(Node w, NNMap& next, VecFirst& first) {
jacint@714
   428
jacint@714
   429
      int lev=level[w];
jacint@714
   430
      Num exc=excess[w];
jacint@714
   431
      int newlevel=n;       //bound on the next level of w
jacint@714
   432
jacint@714
   433
      OutEdgeIt e;
jacint@714
   434
      for(g->first(e,w); g->valid(e); g->next(e)) {
jacint@714
   435
jacint@714
   436
	if ( (*flow)[e] >= (*capacity)[e] ) continue;
jacint@714
   437
	Node v=g->head(e);
jacint@714
   438
jacint@714
   439
	if( lev > level[v] ) { //Push is allowed now
jacint@714
   440
jacint@714
   441
	  if ( excess[v]<=0 && v!=t && v!=s ) {
jacint@714
   442
	    next.set(v,first[level[v]]);
jacint@714
   443
	    first[level[v]]=v;
jacint@714
   444
	    //	    int lev_v=level[v];
jacint@714
   445
	    //active[lev_v].push(v);
jacint@714
   446
	  }
jacint@714
   447
jacint@714
   448
	  Num cap=(*capacity)[e];
jacint@714
   449
	  Num flo=(*flow)[e];
jacint@714
   450
	  Num remcap=cap-flo;
jacint@714
   451
jacint@714
   452
	  if ( remcap >= exc ) { //A nonsaturating push.
jacint@714
   453
jacint@714
   454
	    flow->set(e, flo+exc);
jacint@714
   455
	    excess.set(v, excess[v]+exc);
jacint@714
   456
	    exc=0;
jacint@714
   457
	    break;
jacint@714
   458
jacint@714
   459
	  } else { //A saturating push.
jacint@714
   460
	    flow->set(e, cap);
jacint@714
   461
	    excess.set(v, excess[v]+remcap);
jacint@714
   462
	    exc-=remcap;
jacint@714
   463
	  }
jacint@714
   464
	} else if ( newlevel > level[v] ) newlevel = level[v];
jacint@714
   465
      } //for out edges wv
jacint@714
   466
jacint@714
   467
      if ( exc > 0 ) {
jacint@714
   468
	InEdgeIt e;
jacint@714
   469
	for(g->first(e,w); g->valid(e); g->next(e)) {
jacint@714
   470
jacint@714
   471
	  if( (*flow)[e] <= 0 ) continue;
jacint@714
   472
	  Node v=g->tail(e);
jacint@714
   473
jacint@714
   474
	  if( lev > level[v] ) { //Push is allowed now
jacint@714
   475
jacint@714
   476
	    if ( excess[v]<=0 && v!=t && v!=s ) {
jacint@714
   477
	      next.set(v,first[level[v]]);
jacint@714
   478
	      first[level[v]]=v;
jacint@714
   479
	      //int lev_v=level[v];
jacint@714
   480
	      //active[lev_v].push(v);
jacint@714
   481
	    }
jacint@714
   482
jacint@714
   483
	    Num flo=(*flow)[e];
jacint@714
   484
jacint@714
   485
	    if ( flo >= exc ) { //A nonsaturating push.
jacint@714
   486
jacint@714
   487
	      flow->set(e, flo-exc);
jacint@714
   488
	      excess.set(v, excess[v]+exc);
jacint@714
   489
	      exc=0;
jacint@714
   490
	      break;
jacint@714
   491
	    } else {  //A saturating push.
jacint@714
   492
jacint@714
   493
	      excess.set(v, excess[v]+flo);
jacint@714
   494
	      exc-=flo;
jacint@714
   495
	      flow->set(e,0);
jacint@714
   496
	    }
jacint@714
   497
	  } else if ( newlevel > level[v] ) newlevel = level[v];
jacint@714
   498
	} //for in edges vw
jacint@714
   499
jacint@714
   500
      } // if w still has excess after the out edge for cycle
jacint@714
   501
jacint@714
   502
      excess.set(w, exc);
jacint@714
   503
jacint@714
   504
      return newlevel;
jacint@714
   505
    }
jacint@714
   506
jacint@714
   507
jacint@714
   508
    void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
jacint@714
   509
			VecNode& level_list, NNMap& left, NNMap& right)
jacint@714
   510
    {
jacint@714
   511
      std::queue<Node> bfs_queue;
jacint@714
   512
jacint@714
   513
      switch (fe) {
jacint@714
   514
      case NO_FLOW:   //flow is already set to const zero in this case
jacint@714
   515
      case ZERO_FLOW:
jacint@714
   516
	{
jacint@714
   517
	  //Reverse_bfs from t, to find the starting level.
jacint@714
   518
	  level.set(t,0);
jacint@714
   519
	  bfs_queue.push(t);
jacint@714
   520
jacint@714
   521
	  while (!bfs_queue.empty()) {
jacint@714
   522
jacint@714
   523
	    Node v=bfs_queue.front();
jacint@714
   524
	    bfs_queue.pop();
jacint@714
   525
	    int l=level[v]+1;
jacint@714
   526
jacint@714
   527
	    InEdgeIt e;
jacint@714
   528
	    for(g->first(e,v); g->valid(e); g->next(e)) {
jacint@714
   529
	      Node w=g->tail(e);
jacint@714
   530
	      if ( level[w] == n && w != s ) {
jacint@714
   531
		bfs_queue.push(w);
jacint@714
   532
		Node z=level_list[l];
jacint@714
   533
		if ( g->valid(z) ) left.set(z,w);
jacint@714
   534
		right.set(w,z);
jacint@714
   535
		level_list[l]=w;
jacint@714
   536
		level.set(w, l);
jacint@714
   537
	      }
jacint@714
   538
	    }
jacint@714
   539
	  }
jacint@714
   540
jacint@714
   541
	  //the starting flow
jacint@714
   542
	  OutEdgeIt e;
jacint@714
   543
	  for(g->first(e,s); g->valid(e); g->next(e))
jacint@714
   544
	    {
jacint@714
   545
	      Num c=(*capacity)[e];
jacint@714
   546
	      if ( c <= 0 ) continue;
jacint@714
   547
	      Node w=g->head(e);
jacint@714
   548
	      if ( level[w] < n ) {
jacint@714
   549
		if ( excess[w] <= 0 && w!=t ) 
jacint@714
   550
		  {
jacint@714
   551
		    next.set(w,first[level[w]]);
jacint@714
   552
		    first[level[w]]=w;
jacint@714
   553
		    //active[level[w]].push(w);
jacint@714
   554
		  }
jacint@714
   555
		flow->set(e, c);
jacint@714
   556
		excess.set(w, excess[w]+c);
jacint@714
   557
	      }
jacint@714
   558
	    }
jacint@714
   559
	  break;
jacint@714
   560
	}
jacint@714
   561
jacint@714
   562
      case GEN_FLOW:
jacint@714
   563
      case PRE_FLOW:
jacint@714
   564
	{
jacint@714
   565
	  //Reverse_bfs from t in the residual graph,
jacint@714
   566
	  //to find the starting level.
jacint@714
   567
	  level.set(t,0);
jacint@714
   568
	  bfs_queue.push(t);
jacint@714
   569
jacint@714
   570
	  while (!bfs_queue.empty()) {
jacint@714
   571
jacint@714
   572
	    Node v=bfs_queue.front();
jacint@714
   573
	    bfs_queue.pop();
jacint@714
   574
	    int l=level[v]+1;
jacint@714
   575
jacint@714
   576
	    InEdgeIt e;
jacint@714
   577
	    for(g->first(e,v); g->valid(e); g->next(e)) {
jacint@714
   578
	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
jacint@714
   579
	      Node w=g->tail(e);
jacint@714
   580
	      if ( level[w] == n && w != s ) {
jacint@714
   581
		bfs_queue.push(w);
jacint@714
   582
		Node z=level_list[l];
jacint@714
   583
		if ( g->valid(z) ) left.set(z,w);
jacint@714
   584
		right.set(w,z);
jacint@714
   585
		level_list[l]=w;
jacint@714
   586
		level.set(w, l);
jacint@714
   587
	      }
jacint@714
   588
	    }
jacint@714
   589
jacint@714
   590
	    OutEdgeIt f;
jacint@714
   591
	    for(g->first(f,v); g->valid(f); g->next(f)) {
jacint@714
   592
	      if ( 0 >= (*flow)[f] ) continue;
jacint@714
   593
	      Node w=g->head(f);
jacint@714
   594
	      if ( level[w] == n && w != s ) {
jacint@714
   595
		bfs_queue.push(w);
jacint@714
   596
		Node z=level_list[l];
jacint@714
   597
		if ( g->valid(z) ) left.set(z,w);
jacint@714
   598
		right.set(w,z);
jacint@714
   599
		level_list[l]=w;
jacint@714
   600
		level.set(w, l);
jacint@714
   601
	      }
jacint@714
   602
	    }
jacint@714
   603
	  }
jacint@714
   604
jacint@714
   605
jacint@714
   606
	  //the starting flow
jacint@714
   607
	  OutEdgeIt e;
jacint@714
   608
	  for(g->first(e,s); g->valid(e); g->next(e))
jacint@714
   609
	    {
jacint@714
   610
	      Num rem=(*capacity)[e]-(*flow)[e];
jacint@714
   611
	      if ( rem <= 0 ) continue;
jacint@714
   612
	      Node w=g->head(e);
jacint@714
   613
	      if ( level[w] < n ) {
jacint@714
   614
		if ( excess[w] <= 0 && w!=t )
jacint@714
   615
		  {
jacint@714
   616
		    next.set(w,first[level[w]]);
jacint@714
   617
		    first[level[w]]=w;
jacint@714
   618
		    //active[level[w]].push(w);
jacint@714
   619
		  }   
jacint@714
   620
		flow->set(e, (*capacity)[e]);
jacint@714
   621
		excess.set(w, excess[w]+rem);
jacint@714
   622
	      }
jacint@714
   623
	    }
jacint@714
   624
jacint@714
   625
	  InEdgeIt f;
jacint@714
   626
	  for(g->first(f,s); g->valid(f); g->next(f))
jacint@714
   627
	    {
jacint@714
   628
	      if ( (*flow)[f] <= 0 ) continue;
jacint@714
   629
	      Node w=g->tail(f);
jacint@714
   630
	      if ( level[w] < n ) {
jacint@714
   631
		if ( excess[w] <= 0 && w!=t )
jacint@714
   632
		  {
jacint@714
   633
		    next.set(w,first[level[w]]);
jacint@714
   634
		    first[level[w]]=w;
jacint@714
   635
		    //active[level[w]].push(w);
jacint@714
   636
		  }   
jacint@714
   637
		excess.set(w, excess[w]+(*flow)[f]);
jacint@714
   638
		flow->set(f, 0);
jacint@714
   639
	      }
jacint@714
   640
	    }
jacint@714
   641
	  break;
jacint@714
   642
	} //case PRE_FLOW
jacint@714
   643
      }
jacint@714
   644
    } //preflowPreproc
jacint@714
   645
jacint@714
   646
jacint@714
   647
jacint@714
   648
    void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
jacint@714
   649
		 VecNode& level_list, NNMap& left,
jacint@714
   650
		 NNMap& right, int& b, int& k, bool what_heur )
jacint@714
   651
    {
jacint@714
   652
jacint@714
   653
      Num lev=level[w];
jacint@714
   654
jacint@714
   655
      Node right_n=right[w];
jacint@714
   656
      Node left_n=left[w];
jacint@714
   657
jacint@714
   658
      //unlacing starts
jacint@714
   659
      if ( g->valid(right_n) ) {
jacint@714
   660
	if ( g->valid(left_n) ) {
jacint@714
   661
	  right.set(left_n, right_n);
jacint@714
   662
	  left.set(right_n, left_n);
jacint@714
   663
	} else {
jacint@714
   664
	  level_list[lev]=right_n;
jacint@714
   665
	  left.set(right_n, INVALID);
jacint@714
   666
	}
jacint@714
   667
      } else {
jacint@714
   668
	if ( g->valid(left_n) ) {
jacint@714
   669
	  right.set(left_n, INVALID);
jacint@714
   670
	} else {
jacint@714
   671
	  level_list[lev]=INVALID;
jacint@714
   672
	}
jacint@714
   673
      }
jacint@714
   674
      //unlacing ends
jacint@714
   675
jacint@714
   676
      if ( !g->valid(level_list[lev]) ) {
jacint@714
   677
jacint@714
   678
	//gapping starts
jacint@714
   679
	for (int i=lev; i!=k ; ) {
jacint@714
   680
	  Node v=level_list[++i];
jacint@714
   681
	  while ( g->valid(v) ) {
jacint@714
   682
	    level.set(v,n);
jacint@714
   683
	    v=right[v];
jacint@714
   684
	  }
jacint@714
   685
	  level_list[i]=INVALID;
jacint@714
   686
	  if ( !what_heur ) first[i]=INVALID;
jacint@714
   687
	  /*{
jacint@714
   688
	    while ( !active[i].empty() ) {
jacint@714
   689
	    active[i].pop();    //FIXME: ezt szebben kene
jacint@714
   690
	    }
jacint@714
   691
	    }*/
jacint@714
   692
	}
jacint@714
   693
jacint@714
   694
	level.set(w,n);
jacint@714
   695
	b=lev-1;
jacint@714
   696
	k=b;
jacint@714
   697
	//gapping ends
jacint@714
   698
jacint@714
   699
      } else {
jacint@714
   700
jacint@714
   701
	if ( newlevel == n ) level.set(w,n);
jacint@714
   702
	else {
jacint@714
   703
	  level.set(w,++newlevel);
jacint@714
   704
	  next.set(w,first[newlevel]);
jacint@714
   705
	  first[newlevel]=w;
jacint@714
   706
	  //	  active[newlevel].push(w);
jacint@714
   707
	  if ( what_heur ) b=newlevel;
jacint@714
   708
	  if ( k < newlevel ) ++k;      //now k=newlevel
jacint@714
   709
	  Node z=level_list[newlevel];
jacint@714
   710
	  if ( g->valid(z) ) left.set(z,w);
jacint@714
   711
	  right.set(w,z);
jacint@714
   712
	  left.set(w,INVALID);
jacint@714
   713
	  level_list[newlevel]=w;
jacint@714
   714
	}
jacint@714
   715
      }
jacint@714
   716
jacint@714
   717
    } //relabel
jacint@714
   718
jacint@714
   719
jacint@714
   720
    template<typename MapGraphWrapper>
jacint@714
   721
    class DistanceMap {
jacint@714
   722
    protected:
jacint@714
   723
      const MapGraphWrapper* g;
jacint@714
   724
      typename MapGraphWrapper::template NodeMap<int> dist;
jacint@714
   725
    public:
jacint@714
   726
      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
jacint@714
   727
      void set(const typename MapGraphWrapper::Node& n, int a) {
jacint@714
   728
	dist.set(n, a);
jacint@714
   729
      }
jacint@714
   730
      int operator[](const typename MapGraphWrapper::Node& n) const { 
jacint@714
   731
	return dist[n]; 
jacint@714
   732
      }
jacint@714
   733
      //       int get(const typename MapGraphWrapper::Node& n) const {
jacint@714
   734
      // 	return dist[n]; }
jacint@714
   735
      //       bool get(const typename MapGraphWrapper::Edge& e) const {
jacint@714
   736
      // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
jacint@714
   737
      bool operator[](const typename MapGraphWrapper::Edge& e) const {
jacint@714
   738
	return (dist[g->tail(e)]<dist[g->head(e)]);
jacint@714
   739
      }
jacint@714
   740
    };
jacint@714
   741
jacint@714
   742
  };
jacint@714
   743
jacint@714
   744
jacint@714
   745
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
   746
  void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
jacint@714
   747
  {
jacint@714
   748
jacint@714
   749
    int heur0=(int)(H0*n);  //time while running 'bound decrease'
jacint@714
   750
    int heur1=(int)(H1*n);  //time while running 'highest label'
jacint@714
   751
    int heur=heur1;         //starting time interval (#of relabels)
jacint@714
   752
    int numrelabel=0;
jacint@714
   753
jacint@714
   754
    bool what_heur=1;
jacint@714
   755
    //It is 0 in case 'bound decrease' and 1 in case 'highest label'
jacint@714
   756
jacint@714
   757
    bool end=false;
jacint@714
   758
    //Needed for 'bound decrease', true means no active nodes are above bound
jacint@714
   759
    //b.
jacint@714
   760
jacint@714
   761
    int k=n-2;  //bound on the highest level under n containing a node
jacint@714
   762
    int b=k;    //bound on the highest level under n of an active node
jacint@714
   763
jacint@714
   764
    VecFirst first(n, INVALID);
jacint@714
   765
    NNMap next(*g, INVALID); //maybe INVALID is not needed
jacint@714
   766
    //    VecStack active(n);
jacint@714
   767
jacint@714
   768
    NNMap left(*g, INVALID);
jacint@714
   769
    NNMap right(*g, INVALID);
jacint@714
   770
    VecNode level_list(n,INVALID);
jacint@714
   771
    //List of the nodes in level i<n, set to n.
jacint@714
   772
jacint@714
   773
    NodeIt v;
jacint@714
   774
    for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
jacint@714
   775
    //setting each node to level n
jacint@714
   776
jacint@714
   777
    if ( fe == NO_FLOW ) {
jacint@714
   778
      EdgeIt e;
jacint@714
   779
      for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
jacint@714
   780
    }
jacint@714
   781
jacint@714
   782
    switch (fe) { //computing the excess
jacint@714
   783
    case PRE_FLOW:
jacint@714
   784
      {
jacint@714
   785
	NodeIt v;
jacint@714
   786
	for(g->first(v); g->valid(v); g->next(v)) {
jacint@714
   787
	  Num exc=0;
jacint@714
   788
jacint@714
   789
	  InEdgeIt e;
jacint@714
   790
	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
jacint@714
   791
	  OutEdgeIt f;
jacint@714
   792
	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
jacint@714
   793
jacint@714
   794
	  excess.set(v,exc);
jacint@714
   795
jacint@714
   796
	  //putting the active nodes into the stack
jacint@714
   797
	  int lev=level[v];
jacint@714
   798
	  if ( exc > 0 && lev < n && v != t ) 
jacint@714
   799
	    {
jacint@714
   800
	      next.set(v,first[lev]);
jacint@714
   801
	      first[lev]=v;
jacint@714
   802
	    }
jacint@714
   803
	  //	  active[lev].push(v);
jacint@714
   804
	}
jacint@714
   805
	break;
jacint@714
   806
      }
jacint@714
   807
    case GEN_FLOW:
jacint@714
   808
      {
jacint@714
   809
	NodeIt v;
jacint@714
   810
	for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
jacint@714
   811
jacint@714
   812
	Num exc=0;
jacint@714
   813
	InEdgeIt e;
jacint@714
   814
	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
jacint@714
   815
	OutEdgeIt f;
jacint@714
   816
	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
jacint@714
   817
	excess.set(t,exc);
jacint@714
   818
	break;
jacint@714
   819
      }
jacint@714
   820
    case ZERO_FLOW:
jacint@714
   821
    case NO_FLOW:
jacint@714
   822
      {
jacint@714
   823
	NodeIt v;
jacint@714
   824
        for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
jacint@714
   825
	break;
jacint@714
   826
      }
jacint@714
   827
    }
jacint@714
   828
jacint@714
   829
    preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
jacint@714
   830
    //End of preprocessing
jacint@714
   831
jacint@714
   832
jacint@714
   833
    //Push/relabel on the highest level active nodes.
jacint@714
   834
    while ( true ) {
jacint@714
   835
      if ( b == 0 ) {
jacint@714
   836
	if ( !what_heur && !end && k > 0 ) {
jacint@714
   837
	  b=k;
jacint@714
   838
	  end=true;
jacint@714
   839
	} else break;
jacint@714
   840
      }
jacint@714
   841
jacint@714
   842
      if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
jacint@714
   843
      else {
jacint@714
   844
	end=false;
jacint@714
   845
	Node w=first[b];
jacint@714
   846
	first[b]=next[w];
jacint@714
   847
	/*	Node w=active[b].top();
jacint@714
   848
		active[b].pop();*/
jacint@714
   849
	int newlevel=push(w,/*active*/next, first);
jacint@714
   850
	if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
jacint@714
   851
				     left, right, b, k, what_heur);
jacint@714
   852
jacint@714
   853
	++numrelabel;
jacint@714
   854
	if ( numrelabel >= heur ) {
jacint@714
   855
	  numrelabel=0;
jacint@714
   856
	  if ( what_heur ) {
jacint@714
   857
	    what_heur=0;
jacint@714
   858
	    heur=heur0;
jacint@714
   859
	    end=false;
jacint@714
   860
	  } else {
jacint@714
   861
	    what_heur=1;
jacint@714
   862
	    heur=heur1;
jacint@714
   863
	    b=k;
jacint@714
   864
	  }
jacint@714
   865
	}
jacint@714
   866
      }
jacint@714
   867
    }
jacint@714
   868
jacint@714
   869
    status=AFTER_PRE_FLOW_PHASE_1;
jacint@714
   870
  }
jacint@714
   871
jacint@714
   872
jacint@714
   873
jacint@714
   874
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
   875
  void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase2()
jacint@714
   876
  {
jacint@714
   877
jacint@714
   878
    int k=n-2;  //bound on the highest level under n containing a node
jacint@714
   879
    int b=k;    //bound on the highest level under n of an active node
jacint@714
   880
jacint@714
   881
    
jacint@714
   882
    VecFirst first(n, INVALID);
jacint@714
   883
    NNMap next(*g, INVALID); //maybe INVALID is not needed
jacint@714
   884
    //    VecStack active(n);
jacint@714
   885
    level.set(s,0);
jacint@714
   886
    std::queue<Node> bfs_queue;
jacint@714
   887
    bfs_queue.push(s);
jacint@714
   888
jacint@714
   889
    while (!bfs_queue.empty()) {
jacint@714
   890
jacint@714
   891
      Node v=bfs_queue.front();
jacint@714
   892
      bfs_queue.pop();
jacint@714
   893
      int l=level[v]+1;
jacint@714
   894
jacint@714
   895
      InEdgeIt e;
jacint@714
   896
      for(g->first(e,v); g->valid(e); g->next(e)) {
jacint@714
   897
	if ( (*capacity)[e] <= (*flow)[e] ) continue;
jacint@714
   898
	Node u=g->tail(e);
jacint@714
   899
	if ( level[u] >= n ) {
jacint@714
   900
	  bfs_queue.push(u);
jacint@714
   901
	  level.set(u, l);
jacint@714
   902
	  if ( excess[u] > 0 ) {
jacint@714
   903
	    next.set(u,first[l]);
jacint@714
   904
	    first[l]=u;
jacint@714
   905
	    //active[l].push(u);
jacint@714
   906
	  }
jacint@714
   907
	}
jacint@714
   908
      }
jacint@714
   909
jacint@714
   910
      OutEdgeIt f;
jacint@714
   911
      for(g->first(f,v); g->valid(f); g->next(f)) {
jacint@714
   912
	if ( 0 >= (*flow)[f] ) continue;
jacint@714
   913
	Node u=g->head(f);
jacint@714
   914
	if ( level[u] >= n ) {
jacint@714
   915
	  bfs_queue.push(u);
jacint@714
   916
	  level.set(u, l);
jacint@714
   917
	  if ( excess[u] > 0 ) {
jacint@714
   918
	    next.set(u,first[l]);
jacint@714
   919
	    first[l]=u;
jacint@714
   920
	    //active[l].push(u);
jacint@714
   921
	  }
jacint@714
   922
	}
jacint@714
   923
      }
jacint@714
   924
    }
jacint@714
   925
    b=n-2;
jacint@714
   926
jacint@714
   927
    while ( true ) {
jacint@714
   928
jacint@714
   929
      if ( b == 0 ) break;
jacint@714
   930
jacint@714
   931
      if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
jacint@714
   932
      else {
jacint@714
   933
jacint@714
   934
	Node w=first[b];
jacint@714
   935
	first[b]=next[w];
jacint@714
   936
	/*	Node w=active[b].top();
jacint@714
   937
		active[b].pop();*/
jacint@714
   938
	int newlevel=push(w,next, first/*active*/);
jacint@714
   939
jacint@714
   940
	//relabel
jacint@714
   941
	if ( excess[w] > 0 ) {
jacint@714
   942
	  level.set(w,++newlevel);
jacint@714
   943
	  next.set(w,first[newlevel]);
jacint@714
   944
	  first[newlevel]=w;
jacint@714
   945
	  //active[newlevel].push(w);
jacint@714
   946
	  b=newlevel;
jacint@714
   947
	}
jacint@714
   948
      }  // if stack[b] is nonempty
jacint@714
   949
    } // while(true)
jacint@714
   950
jacint@714
   951
    status=AFTER_PRE_FLOW_PHASE_2;
jacint@714
   952
  }
jacint@714
   953
jacint@714
   954
jacint@714
   955
jacint@714
   956
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
   957
  bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
jacint@714
   958
  {
jacint@714
   959
    ResGW res_graph(*g, *capacity, *flow);
jacint@714
   960
    bool _augment=false;
jacint@714
   961
jacint@714
   962
    //ReachedMap level(res_graph);
jacint@714
   963
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
jacint@714
   964
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
jacint@714
   965
    bfs.pushAndSetReached(s);
jacint@714
   966
jacint@714
   967
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
jacint@714
   968
    pred.set(s, INVALID);
jacint@714
   969
jacint@714
   970
    typename ResGW::template NodeMap<Num> free(res_graph);
jacint@714
   971
jacint@714
   972
    //searching for augmenting path
jacint@714
   973
    while ( !bfs.finished() ) {
jacint@714
   974
      ResGWOutEdgeIt e=bfs;
jacint@714
   975
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
jacint@714
   976
	Node v=res_graph.tail(e);
jacint@714
   977
	Node w=res_graph.head(e);
jacint@714
   978
	pred.set(w, e);
jacint@714
   979
	if (res_graph.valid(pred[v])) {
jacint@714
   980
	  free.set(w, std::min(free[v], res_graph.resCap(e)));
jacint@714
   981
	} else {
jacint@714
   982
	  free.set(w, res_graph.resCap(e));
jacint@714
   983
	}
jacint@714
   984
	if (res_graph.head(e)==t) { _augment=true; break; }
jacint@714
   985
      }
jacint@714
   986
jacint@714
   987
      ++bfs;
jacint@714
   988
    } //end of searching augmenting path
jacint@714
   989
jacint@714
   990
    if (_augment) {
jacint@714
   991
      Node n=t;
jacint@714
   992
      Num augment_value=free[t];
jacint@714
   993
      while (res_graph.valid(pred[n])) {
jacint@714
   994
	ResGWEdge e=pred[n];
jacint@714
   995
	res_graph.augment(e, augment_value);
jacint@714
   996
	n=res_graph.tail(e);
jacint@714
   997
      }
jacint@714
   998
    }
jacint@714
   999
jacint@714
  1000
    status=AFTER_AUGMENTING;
jacint@714
  1001
    return _augment;
jacint@714
  1002
  }
jacint@714
  1003
jacint@714
  1004
jacint@714
  1005
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
  1006
  bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
jacint@714
  1007
  {
jacint@714
  1008
    ResGW res_graph(*g, *capacity, *flow);
jacint@714
  1009
    bool _augment=false;
jacint@714
  1010
jacint@714
  1011
    if (status!=AFTER_FAST_AUGMENTING) {
jacint@714
  1012
      FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); 
jacint@714
  1013
      number_of_augmentations=1;
jacint@714
  1014
    } else {
jacint@714
  1015
      ++number_of_augmentations;
jacint@714
  1016
    }
jacint@714
  1017
    TrickyReachedMap<ReachedMap> 
jacint@714
  1018
      tricky_reached_map(level, number_of_augmentations);
jacint@714
  1019
    //ReachedMap level(res_graph);
jacint@714
  1020
//    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
jacint@714
  1021
    BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > 
jacint@714
  1022
      bfs(res_graph, tricky_reached_map);
jacint@714
  1023
    bfs.pushAndSetReached(s);
jacint@714
  1024
jacint@714
  1025
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
jacint@714
  1026
    pred.set(s, INVALID);
jacint@714
  1027
jacint@714
  1028
    typename ResGW::template NodeMap<Num> free(res_graph);
jacint@714
  1029
jacint@714
  1030
    //searching for augmenting path
jacint@714
  1031
    while ( !bfs.finished() ) {
jacint@714
  1032
      ResGWOutEdgeIt e=bfs;
jacint@714
  1033
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
jacint@714
  1034
	Node v=res_graph.tail(e);
jacint@714
  1035
	Node w=res_graph.head(e);
jacint@714
  1036
	pred.set(w, e);
jacint@714
  1037
	if (res_graph.valid(pred[v])) {
jacint@714
  1038
	  free.set(w, std::min(free[v], res_graph.resCap(e)));
jacint@714
  1039
	} else {
jacint@714
  1040
	  free.set(w, res_graph.resCap(e));
jacint@714
  1041
	}
jacint@714
  1042
	if (res_graph.head(e)==t) { _augment=true; break; }
jacint@714
  1043
      }
jacint@714
  1044
jacint@714
  1045
      ++bfs;
jacint@714
  1046
    } //end of searching augmenting path
jacint@714
  1047
jacint@714
  1048
    if (_augment) {
jacint@714
  1049
      Node n=t;
jacint@714
  1050
      Num augment_value=free[t];
jacint@714
  1051
      while (res_graph.valid(pred[n])) {
jacint@714
  1052
	ResGWEdge e=pred[n];
jacint@714
  1053
	res_graph.augment(e, augment_value);
jacint@714
  1054
	n=res_graph.tail(e);
jacint@714
  1055
      }
jacint@714
  1056
    }
jacint@714
  1057
jacint@714
  1058
    status=AFTER_FAST_AUGMENTING;
jacint@714
  1059
    return _augment;
jacint@714
  1060
  }
jacint@714
  1061
jacint@714
  1062
jacint@714
  1063
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
  1064
  template<typename MutableGraph>
jacint@714
  1065
  bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
jacint@714
  1066
  {
jacint@714
  1067
    typedef MutableGraph MG;
jacint@714
  1068
    bool _augment=false;
jacint@714
  1069
jacint@714
  1070
    ResGW res_graph(*g, *capacity, *flow);
jacint@714
  1071
jacint@714
  1072
    //bfs for distances on the residual graph
jacint@714
  1073
    //ReachedMap level(res_graph);
jacint@714
  1074
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
jacint@714
  1075
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
jacint@714
  1076
    bfs.pushAndSetReached(s);
jacint@714
  1077
    typename ResGW::template NodeMap<int>
jacint@714
  1078
      dist(res_graph); //filled up with 0's
jacint@714
  1079
jacint@714
  1080
    //F will contain the physical copy of the residual graph
jacint@714
  1081
    //with the set of edges which are on shortest paths
jacint@714
  1082
    MG F;
jacint@714
  1083
    typename ResGW::template NodeMap<typename MG::Node>
jacint@714
  1084
      res_graph_to_F(res_graph);
jacint@714
  1085
    {
jacint@714
  1086
      typename ResGW::NodeIt n;
jacint@714
  1087
      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
jacint@714
  1088
	res_graph_to_F.set(n, F.addNode());
jacint@714
  1089
      }
jacint@714
  1090
    }
jacint@714
  1091
jacint@714
  1092
    typename MG::Node sF=res_graph_to_F[s];
jacint@714
  1093
    typename MG::Node tF=res_graph_to_F[t];
jacint@714
  1094
    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
jacint@714
  1095
    typename MG::template EdgeMap<Num> residual_capacity(F);
jacint@714
  1096
jacint@714
  1097
    while ( !bfs.finished() ) {
jacint@714
  1098
      ResGWOutEdgeIt e=bfs;
jacint@714
  1099
      if (res_graph.valid(e)) {
jacint@714
  1100
	if (bfs.isBNodeNewlyReached()) {
jacint@714
  1101
	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
jacint@714
  1102
	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
jacint@714
  1103
					res_graph_to_F[res_graph.head(e)]);
jacint@714
  1104
	  original_edge.update();
jacint@714
  1105
	  original_edge.set(f, e);
jacint@714
  1106
	  residual_capacity.update();
jacint@714
  1107
	  residual_capacity.set(f, res_graph.resCap(e));
jacint@714
  1108
	} else {
jacint@714
  1109
	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
jacint@714
  1110
	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
jacint@714
  1111
					  res_graph_to_F[res_graph.head(e)]);
jacint@714
  1112
	    original_edge.update();
jacint@714
  1113
	    original_edge.set(f, e);
jacint@714
  1114
	    residual_capacity.update();
jacint@714
  1115
	    residual_capacity.set(f, res_graph.resCap(e));
jacint@714
  1116
	  }
jacint@714
  1117
	}
jacint@714
  1118
      }
jacint@714
  1119
      ++bfs;
jacint@714
  1120
    } //computing distances from s in the residual graph
jacint@714
  1121
jacint@714
  1122
    bool __augment=true;
jacint@714
  1123
jacint@714
  1124
    while (__augment) {
jacint@714
  1125
      __augment=false;
jacint@714
  1126
      //computing blocking flow with dfs
jacint@714
  1127
      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
jacint@714
  1128
      typename MG::template NodeMap<typename MG::Edge> pred(F);
jacint@714
  1129
      pred.set(sF, INVALID);
jacint@714
  1130
      //invalid iterators for sources
jacint@714
  1131
jacint@714
  1132
      typename MG::template NodeMap<Num> free(F);
jacint@714
  1133
jacint@714
  1134
      dfs.pushAndSetReached(sF);
jacint@714
  1135
      while (!dfs.finished()) {
jacint@714
  1136
	++dfs;
jacint@714
  1137
	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
jacint@714
  1138
	  if (dfs.isBNodeNewlyReached()) {
jacint@714
  1139
	    typename MG::Node v=F.aNode(dfs);
jacint@714
  1140
	    typename MG::Node w=F.bNode(dfs);
jacint@714
  1141
	    pred.set(w, dfs);
jacint@714
  1142
	    if (F.valid(pred[v])) {
jacint@714
  1143
	      free.set(w, std::min(free[v], residual_capacity[dfs]));
jacint@714
  1144
	    } else {
jacint@714
  1145
	      free.set(w, residual_capacity[dfs]);
jacint@714
  1146
	    }
jacint@714
  1147
	    if (w==tF) {
jacint@714
  1148
	      __augment=true;
jacint@714
  1149
	      _augment=true;
jacint@714
  1150
	      break;
jacint@714
  1151
	    }
jacint@714
  1152
jacint@714
  1153
	  } else {
jacint@714
  1154
	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
jacint@714
  1155
	  }
jacint@714
  1156
	}
jacint@714
  1157
      }
jacint@714
  1158
jacint@714
  1159
      if (__augment) {
jacint@714
  1160
	typename MG::Node n=tF;
jacint@714
  1161
	Num augment_value=free[tF];
jacint@714
  1162
	while (F.valid(pred[n])) {
jacint@714
  1163
	  typename MG::Edge e=pred[n];
jacint@714
  1164
	  res_graph.augment(original_edge[e], augment_value);
jacint@714
  1165
	  n=F.tail(e);
jacint@714
  1166
	  if (residual_capacity[e]==augment_value)
jacint@714
  1167
	    F.erase(e);
jacint@714
  1168
	  else
jacint@714
  1169
	    residual_capacity.set(e, residual_capacity[e]-augment_value);
jacint@714
  1170
	}
jacint@714
  1171
      }
jacint@714
  1172
jacint@714
  1173
    }
jacint@714
  1174
jacint@714
  1175
    status=AFTER_AUGMENTING;
jacint@714
  1176
    return _augment;
jacint@714
  1177
  }
jacint@714
  1178
jacint@714
  1179
jacint@714
  1180
jacint@714
  1181
jacint@714
  1182
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
jacint@714
  1183
  bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
jacint@714
  1184
  {
jacint@714
  1185
    bool _augment=false;
jacint@714
  1186
jacint@714
  1187
    ResGW res_graph(*g, *capacity, *flow);
jacint@714
  1188
jacint@714
  1189
    //ReachedMap level(res_graph);
jacint@714
  1190
    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
jacint@714
  1191
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
jacint@714
  1192
jacint@714
  1193
    bfs.pushAndSetReached(s);
jacint@714
  1194
    DistanceMap<ResGW> dist(res_graph);
jacint@714
  1195
    while ( !bfs.finished() ) {
jacint@714
  1196
      ResGWOutEdgeIt e=bfs;
jacint@714
  1197
      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
jacint@714
  1198
	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
jacint@714
  1199
      }
jacint@714
  1200
      ++bfs;
jacint@714
  1201
    } //computing distances from s in the residual graph
jacint@714
  1202
jacint@714
  1203
      //Subgraph containing the edges on some shortest paths
jacint@714
  1204
    ConstMap<typename ResGW::Node, bool> true_map(true);
jacint@714
  1205
    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
jacint@714
  1206
      DistanceMap<ResGW> > FilterResGW;
jacint@714
  1207
    FilterResGW filter_res_graph(res_graph, true_map, dist);
jacint@714
  1208
jacint@714
  1209
    //Subgraph, which is able to delete edges which are already
jacint@714
  1210
    //met by the dfs
jacint@714
  1211
    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
jacint@714
  1212
      first_out_edges(filter_res_graph);
jacint@714
  1213
    typename FilterResGW::NodeIt v;
jacint@714
  1214
    for(filter_res_graph.first(v); filter_res_graph.valid(v);
jacint@714
  1215
	filter_res_graph.next(v))
jacint@714
  1216
      {
jacint@714
  1217
 	typename FilterResGW::OutEdgeIt e;
jacint@714
  1218
 	filter_res_graph.first(e, v);
jacint@714
  1219
 	first_out_edges.set(v, e);
jacint@714
  1220
      }
jacint@714
  1221
    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
jacint@714
  1222
      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
jacint@714
  1223
    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
jacint@714
  1224
jacint@714
  1225
    bool __augment=true;
jacint@714
  1226
jacint@714
  1227
    while (__augment) {
jacint@714
  1228
jacint@714
  1229
      __augment=false;
jacint@714
  1230
      //computing blocking flow with dfs
jacint@714
  1231
      DfsIterator< ErasingResGW,
jacint@714
  1232
	typename ErasingResGW::template NodeMap<bool> >
jacint@714
  1233
	dfs(erasing_res_graph);
jacint@714
  1234
      typename ErasingResGW::
jacint@714
  1235
	template NodeMap<typename ErasingResGW::OutEdgeIt>
jacint@714
  1236
	pred(erasing_res_graph);
jacint@714
  1237
      pred.set(s, INVALID);
jacint@714
  1238
      //invalid iterators for sources
jacint@714
  1239
jacint@714
  1240
      typename ErasingResGW::template NodeMap<Num>
jacint@714
  1241
	free1(erasing_res_graph);
jacint@714
  1242
jacint@714
  1243
      dfs.pushAndSetReached
jacint@714
  1244
	///\bug hugo 0.2
jacint@714
  1245
	(typename ErasingResGW::Node
jacint@714
  1246
	 (typename FilterResGW::Node
jacint@714
  1247
	  (typename ResGW::Node(s)
jacint@714
  1248
	   )
jacint@714
  1249
	  )
jacint@714
  1250
	 );
jacint@714
  1251
      while (!dfs.finished()) {
jacint@714
  1252
	++dfs;
jacint@714
  1253
	if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
jacint@714
  1254
 	  {
jacint@714
  1255
  	    if (dfs.isBNodeNewlyReached()) {
jacint@714
  1256
jacint@714
  1257
 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
jacint@714
  1258
 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
jacint@714
  1259
jacint@714
  1260
 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
jacint@714
  1261
 	      if (erasing_res_graph.valid(pred[v])) {
jacint@714
  1262
 		free1.set
jacint@714
  1263
		  (w, std::min(free1[v], res_graph.resCap
jacint@714
  1264
			       (typename ErasingResGW::OutEdgeIt(dfs))));
jacint@714
  1265
 	      } else {
jacint@714
  1266
 		free1.set
jacint@714
  1267
		  (w, res_graph.resCap
jacint@714
  1268
		   (typename ErasingResGW::OutEdgeIt(dfs)));
jacint@714
  1269
 	      }
jacint@714
  1270
jacint@714
  1271
 	      if (w==t) {
jacint@714
  1272
 		__augment=true;
jacint@714
  1273
 		_augment=true;
jacint@714
  1274
 		break;
jacint@714
  1275
 	      }
jacint@714
  1276
 	    } else {
jacint@714
  1277
 	      erasing_res_graph.erase(dfs);
jacint@714
  1278
	    }
jacint@714
  1279
	  }
jacint@714
  1280
      }
jacint@714
  1281
jacint@714
  1282
      if (__augment) {
jacint@714
  1283
	typename ErasingResGW::Node
jacint@714
  1284
	  n=typename FilterResGW::Node(typename ResGW::Node(t));
jacint@714
  1285
	// 	  typename ResGW::NodeMap<Num> a(res_graph);
jacint@714
  1286
	// 	  typename ResGW::Node b;
jacint@714
  1287
	// 	  Num j=a[b];
jacint@714
  1288
	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
jacint@714
  1289
	// 	  typename FilterResGW::Node b1;
jacint@714
  1290
	// 	  Num j1=a1[b1];
jacint@714
  1291
	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
jacint@714
  1292
	// 	  typename ErasingResGW::Node b2;
jacint@714
  1293
	// 	  Num j2=a2[b2];
jacint@714
  1294
	Num augment_value=free1[n];
jacint@714
  1295
	while (erasing_res_graph.valid(pred[n])) {
jacint@714
  1296
	  typename ErasingResGW::OutEdgeIt e=pred[n];
jacint@714
  1297
	  res_graph.augment(e, augment_value);
jacint@714
  1298
	  n=erasing_res_graph.tail(e);
jacint@714
  1299
	  if (res_graph.resCap(e)==0)
jacint@714
  1300
	    erasing_res_graph.erase(e);
jacint@714
  1301
	}
jacint@714
  1302
      }
jacint@714
  1303
jacint@714
  1304
    } //while (__augment)
jacint@714
  1305
jacint@714
  1306
    status=AFTER_AUGMENTING;
jacint@714
  1307
    return _augment;
jacint@714
  1308
  }
jacint@714
  1309
jacint@714
  1310
jacint@714
  1311
} //namespace hugo
jacint@714
  1312
jacint@714
  1313
#endif //HUGO_MAX_FLOW_H
jacint@714
  1314
jacint@714
  1315
jacint@714
  1316
jacint@714
  1317