lemon/preflow.h
author deba
Fri, 28 Sep 2007 12:14:18 +0000
changeset 2477 b5e1f017ff41
parent 2391 14a343be7a5a
child 2514 57143c09dc20
permissions -rw-r--r--
Bug fix in most significant digit calculation
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_PREFLOW_H
alpar@921
    20
#define LEMON_PREFLOW_H
jacint@836
    21
jacint@836
    22
#include <vector>
jacint@836
    23
#include <queue>
jacint@836
    24
jacint@1762
    25
#include <lemon/error.h>
deba@1993
    26
#include <lemon/bits/invalid.h>
alpar@1835
    27
#include <lemon/tolerance.h>
alpar@921
    28
#include <lemon/maps.h>
klao@977
    29
#include <lemon/graph_utils.h>
jacint@836
    30
jacint@836
    31
/// \file
deba@2376
    32
/// \ingroup max_flow
deba@1742
    33
/// \brief Implementation of the preflow algorithm.
jacint@836
    34
alpar@921
    35
namespace lemon {
jacint@836
    36
deba@2376
    37
  ///\ingroup max_flow
deba@1792
    38
  ///\brief %Preflow algorithms class.
deba@1792
    39
  ///
jacint@836
    40
  ///This class provides an implementation of the \e preflow \e
jacint@836
    41
  ///algorithm producing a flow of maximum value in a directed
jacint@2024
    42
  ///graph. The preflow algorithms are the fastest known max flow algorithms. 
jacint@2024
    43
  ///The \e source node, the \e target node, the \e
jacint@836
    44
  ///capacity of the edges and the \e starting \e flow value of the
jacint@836
    45
  ///edges should be passed to the algorithm through the
jacint@836
    46
  ///constructor. It is possible to change these quantities using the
zsuzska@1285
    47
  ///functions \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285
    48
  ///flowMap.
jacint@836
    49
  ///
alpar@921
    50
  ///After running \ref lemon::Preflow::phase1() "phase1()"
alpar@921
    51
  ///or \ref lemon::Preflow::run() "run()", the maximal flow
jacint@836
    52
  ///value can be obtained by calling \ref flowValue(). The minimum
alpar@851
    53
  ///value cut can be written into a <tt>bool</tt> node map by
alpar@851
    54
  ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
jacint@836
    55
  ///the inclusionwise minimum and maximum of the minimum value cuts,
jacint@836
    56
  ///resp.)
jacint@836
    57
  ///
jacint@836
    58
  ///\param Graph The directed graph type the algorithm runs on.
jacint@836
    59
  ///\param Num The number type of the capacities and the flow values.
alpar@1222
    60
  ///\param CapacityMap The capacity map type.
jacint@836
    61
  ///\param FlowMap The flow map type.
alpar@2387
    62
  ///\param Tol The tolerance type. 
jacint@836
    63
  ///
jacint@836
    64
  ///\author Jacint Szabo 
alpar@1227
    65
  ///\todo Second template parameter is superfluous
jacint@836
    66
  template <typename Graph, typename Num,
alpar@1222
    67
	    typename CapacityMap=typename Graph::template EdgeMap<Num>,
alpar@1835
    68
            typename FlowMap=typename Graph::template EdgeMap<Num>,
alpar@2387
    69
	    typename Tol=Tolerance<Num> >
jacint@836
    70
  class Preflow {
jacint@836
    71
  protected:
jacint@836
    72
    typedef typename Graph::Node Node;
jacint@836
    73
    typedef typename Graph::NodeIt NodeIt;
jacint@836
    74
    typedef typename Graph::EdgeIt EdgeIt;
jacint@836
    75
    typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@836
    76
    typedef typename Graph::InEdgeIt InEdgeIt;
jacint@836
    77
jacint@836
    78
    typedef typename Graph::template NodeMap<Node> NNMap;
jacint@836
    79
    typedef typename std::vector<Node> VecNode;
jacint@836
    80
alpar@1222
    81
    const Graph* _g;
alpar@1222
    82
    Node _source;
alpar@1222
    83
    Node _target;
alpar@1222
    84
    const CapacityMap* _capacity;
alpar@1222
    85
    FlowMap* _flow;
alpar@1835
    86
alpar@2387
    87
    Tol _surely;
alpar@1835
    88
    
alpar@1222
    89
    int _node_num;      //the number of nodes of G
jacint@836
    90
    
jacint@836
    91
    typename Graph::template NodeMap<int> level;  
jacint@836
    92
    typename Graph::template NodeMap<Num> excess;
jacint@836
    93
jacint@836
    94
    // constants used for heuristics
jacint@836
    95
    static const int H0=20;
jacint@836
    96
    static const int H1=1;
jacint@836
    97
jacint@1762
    98
  public:
jacint@1762
    99
jacint@1762
   100
    ///\ref Exception for the case when s=t.
jacint@1762
   101
jacint@1762
   102
    ///\ref Exception for the case when the source equals the target.
jacint@1762
   103
    class InvalidArgument : public lemon::LogicError {
jacint@836
   104
    public:
alpar@2151
   105
      virtual const char* what() const throw() {
jacint@1762
   106
	return "lemon::Preflow::InvalidArgument";
jacint@1762
   107
      }
jacint@1762
   108
    };
jacint@1762
   109
    
jacint@1762
   110
    
jacint@836
   111
    ///Indicates the property of the starting flow map.
jacint@1762
   112
    
alpar@1222
   113
    ///Indicates the property of the starting flow map.
jacint@836
   114
    ///
jacint@836
   115
    enum FlowEnum{
alpar@1898
   116
      ///indicates an unspecified edge map. \c flow will be 
alpar@1898
   117
      ///set to the constant zero flow in the beginning of
alpar@1898
   118
      ///the algorithm in this case.
jacint@836
   119
      NO_FLOW,
alpar@1898
   120
      ///constant zero flow
jacint@836
   121
      ZERO_FLOW,
alpar@1898
   122
      ///any flow, i.e. the sum of the in-flows equals to
alpar@1898
   123
      ///the sum of the out-flows in every node except the \c source and
alpar@1898
   124
      ///the \c target.
jacint@836
   125
      GEN_FLOW,
alpar@1898
   126
      ///any preflow, i.e. the sum of the in-flows is at 
alpar@1898
   127
      ///least the sum of the out-flows in every node except the \c source.
jacint@836
   128
      PRE_FLOW
jacint@836
   129
    };
jacint@836
   130
jacint@836
   131
    ///Indicates the state of the preflow algorithm.
jacint@836
   132
alpar@1222
   133
    ///Indicates the state of the preflow algorithm.
jacint@836
   134
    ///
jacint@836
   135
    enum StatusEnum {
alpar@1898
   136
      ///before running the algorithm or
alpar@1898
   137
      ///at an unspecified state.
jacint@836
   138
      AFTER_NOTHING,
alpar@1898
   139
      ///right after running \ref phase1()
jacint@836
   140
      AFTER_PREFLOW_PHASE_1,      
alpar@1898
   141
      ///after running \ref phase2()
jacint@836
   142
      AFTER_PREFLOW_PHASE_2
jacint@836
   143
    };
jacint@836
   144
    
jacint@1762
   145
  protected: 
jacint@1762
   146
    FlowEnum flow_prop;
jacint@836
   147
    StatusEnum status; // Do not needle this flag only if necessary.
jacint@836
   148
    
jacint@836
   149
  public: 
jacint@836
   150
    ///The constructor of the class.
jacint@836
   151
jacint@836
   152
    ///The constructor of the class. 
zsuzska@1285
   153
    ///\param _gr The directed graph the algorithm runs on. 
jacint@836
   154
    ///\param _s The source node.
jacint@836
   155
    ///\param _t The target node.
alpar@1222
   156
    ///\param _cap The capacity of the edges. 
alpar@1222
   157
    ///\param _f The flow of the edges. 
alpar@2387
   158
    ///\param _sr Tol class.
jacint@836
   159
    ///Except the graph, all of these parameters can be reset by
zsuzska@1285
   160
    ///calling \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285
   161
    ///flowMap, resp.
deba@2033
   162
    Preflow(const Graph& _gr, Node _s, Node _t, 
deba@2033
   163
            const CapacityMap& _cap, FlowMap& _f,
alpar@2387
   164
            const Tol &_sr=Tol()) :
alpar@1222
   165
	_g(&_gr), _source(_s), _target(_t), _capacity(&_cap),
deba@2019
   166
	_flow(&_f), _surely(_sr),
alpar@1835
   167
	_node_num(countNodes(_gr)), level(_gr), excess(_gr,0), 
jacint@1762
   168
	flow_prop(NO_FLOW), status(AFTER_NOTHING) { 
jacint@1762
   169
	if ( _source==_target )
jacint@1762
   170
	  throw InvalidArgument();
deba@2033
   171
    }
jacint@1762
   172
    
alpar@1898
   173
    ///Give a reference to the tolerance handler class
jacint@836
   174
alpar@1898
   175
    ///Give a reference to the tolerance handler class
alpar@1898
   176
    ///\sa Tolerance
alpar@2387
   177
    Tol &tolerance() { return _surely; }
alpar@1898
   178
jacint@836
   179
    ///Runs the preflow algorithm.  
jacint@836
   180
alpar@851
   181
    ///Runs the preflow algorithm.
alpar@851
   182
    ///
jacint@836
   183
    void run() {
jacint@836
   184
      phase1(flow_prop);
jacint@836
   185
      phase2();
jacint@836
   186
    }
jacint@836
   187
    
jacint@836
   188
    ///Runs the preflow algorithm.  
jacint@836
   189
    
jacint@836
   190
    ///Runs the preflow algorithm. 
jacint@836
   191
    ///\pre The starting flow map must be
jacint@836
   192
    /// - a constant zero flow if \c fp is \c ZERO_FLOW,
jacint@836
   193
    /// - an arbitrary flow if \c fp is \c GEN_FLOW,
jacint@836
   194
    /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
jacint@836
   195
    /// - any map if \c fp is NO_FLOW.
jacint@836
   196
    ///If the starting flow map is a flow or a preflow then 
jacint@836
   197
    ///the algorithm terminates faster.
jacint@836
   198
    void run(FlowEnum fp) {
jacint@836
   199
      flow_prop=fp;
jacint@836
   200
      run();
jacint@836
   201
    }
jacint@836
   202
      
jacint@836
   203
    ///Runs the first phase of the preflow algorithm.
jacint@836
   204
jacint@920
   205
    ///The preflow algorithm consists of two phases, this method runs
jacint@920
   206
    ///the first phase. After the first phase the maximum flow value
zsuzska@1285
   207
    ///and a minimum value cut can already be computed, although a
jacint@920
   208
    ///maximum flow is not yet obtained. So after calling this method
jacint@920
   209
    ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920
   210
    ///minCut returns a minimum cut.     
jacint@920
   211
    ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
jacint@920
   212
    ///value cuts unless calling \ref phase2.  
kpeter@2473
   213
    ///\warning A real flow map (i.e. not \ref lemon::NullMap "NullMap")
kpeter@2473
   214
    ///is needed for this phase.
jacint@920
   215
    ///\pre The starting flow must be 
jacint@920
   216
    ///- a constant zero flow if \c fp is \c ZERO_FLOW, 
jacint@920
   217
    ///- an arbitary flow if \c fp is \c GEN_FLOW, 
jacint@920
   218
    ///- an arbitary preflow if \c fp is \c PRE_FLOW, 
jacint@920
   219
    ///- any map if \c fp is NO_FLOW.
jacint@836
   220
    void phase1(FlowEnum fp)
jacint@836
   221
    {
jacint@836
   222
      flow_prop=fp;
jacint@836
   223
      phase1();
jacint@836
   224
    }
jacint@836
   225
jacint@836
   226
    
jacint@836
   227
    ///Runs the first phase of the preflow algorithm.
jacint@836
   228
jacint@920
   229
    ///The preflow algorithm consists of two phases, this method runs
jacint@920
   230
    ///the first phase. After the first phase the maximum flow value
zsuzska@1285
   231
    ///and a minimum value cut can already be computed, although a
jacint@920
   232
    ///maximum flow is not yet obtained. So after calling this method
jacint@920
   233
    ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920
   234
    ///minCut returns a minimum cut.
deba@1786
   235
    ///\warning \ref minMinCut() and \ref maxMinCut() do not
alpar@911
   236
    ///give minimum value cuts unless calling \ref phase2().
kpeter@2473
   237
    ///\warning A real flow map (i.e. not \ref lemon::NullMap "NullMap")
kpeter@2473
   238
    ///is needed for this phase.
jacint@836
   239
    void phase1()
jacint@836
   240
    {
deba@2386
   241
      int heur0=int(H0*_node_num);  //time while running 'bound decrease'
deba@2386
   242
      int heur1=int(H1*_node_num);  //time while running 'highest label'
jacint@836
   243
      int heur=heur1;         //starting time interval (#of relabels)
jacint@836
   244
      int numrelabel=0;
jacint@836
   245
jacint@836
   246
      bool what_heur=1;
jacint@836
   247
      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
jacint@836
   248
jacint@836
   249
      bool end=false;
jacint@836
   250
      //Needed for 'bound decrease', true means no active 
jacint@836
   251
      //nodes are above bound b.
jacint@836
   252
alpar@1222
   253
      int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@2024
   254
      int b=k;    //bound on the highest level under n containing an active node
jacint@836
   255
alpar@1222
   256
      VecNode first(_node_num, INVALID);
alpar@1222
   257
      NNMap next(*_g, INVALID);
jacint@836
   258
alpar@1222
   259
      NNMap left(*_g, INVALID);
alpar@1222
   260
      NNMap right(*_g, INVALID);
alpar@1222
   261
      VecNode level_list(_node_num,INVALID);
jacint@836
   262
      //List of the nodes in level i<n, set to n.
jacint@836
   263
jacint@836
   264
      preflowPreproc(first, next, level_list, left, right);
jacint@836
   265
jacint@836
   266
      //Push/relabel on the highest level active nodes.
jacint@836
   267
      while ( true ) {
jacint@836
   268
	if ( b == 0 ) {
jacint@836
   269
	  if ( !what_heur && !end && k > 0 ) {
jacint@836
   270
	    b=k;
jacint@836
   271
	    end=true;
jacint@836
   272
	  } else break;
jacint@836
   273
	}
jacint@836
   274
jacint@836
   275
	if ( first[b]==INVALID ) --b;
jacint@836
   276
	else {
jacint@836
   277
	  end=false;
jacint@836
   278
	  Node w=first[b];
jacint@836
   279
	  first[b]=next[w];
jacint@836
   280
	  int newlevel=push(w, next, first);
deba@2330
   281
	  if ( excess[w] != 0 ) {
deba@2330
   282
            relabel(w, newlevel, first, next, level_list, 
deba@2330
   283
                    left, right, b, k, what_heur);
deba@2330
   284
          }
jacint@836
   285
jacint@836
   286
	  ++numrelabel;
jacint@836
   287
	  if ( numrelabel >= heur ) {
jacint@836
   288
	    numrelabel=0;
jacint@836
   289
	    if ( what_heur ) {
jacint@836
   290
	      what_heur=0;
jacint@836
   291
	      heur=heur0;
jacint@836
   292
	      end=false;
jacint@836
   293
	    } else {
jacint@836
   294
	      what_heur=1;
jacint@836
   295
	      heur=heur1;
jacint@836
   296
	      b=k;
jacint@836
   297
	    }
jacint@836
   298
	  }
jacint@836
   299
	}
jacint@836
   300
      }
jacint@836
   301
      flow_prop=PRE_FLOW;
jacint@836
   302
      status=AFTER_PREFLOW_PHASE_1;
jacint@836
   303
    }
jacint@836
   304
    // Heuristics:
jacint@836
   305
    //   2 phase
jacint@836
   306
    //   gap
jacint@836
   307
    //   list 'level_list' on the nodes on level i implemented by hand
jacint@836
   308
    //   stack 'active' on the active nodes on level i      
jacint@836
   309
    //   runs heuristic 'highest label' for H1*n relabels
alpar@1222
   310
    //   runs heuristic 'bound decrease' for H0*n relabels,
alpar@1222
   311
    //        starts with 'highest label'
jacint@836
   312
    //   Parameters H0 and H1 are initialized to 20 and 1.
jacint@836
   313
jacint@836
   314
jacint@836
   315
    ///Runs the second phase of the preflow algorithm.
jacint@836
   316
jacint@836
   317
    ///The preflow algorithm consists of two phases, this method runs
alpar@1631
   318
    ///the second phase. After calling \ref phase1() and then
alpar@1631
   319
    ///\ref phase2(),
alpar@1631
   320
    /// \ref flowMap() return a maximum flow, \ref flowValue
jacint@920
   321
    ///returns the value of a maximum flow, \ref minCut returns a
jacint@920
   322
    ///minimum cut, while the methods \ref minMinCut and \ref
jacint@920
   323
    ///maxMinCut return the inclusionwise minimum and maximum cuts of
jacint@920
   324
    ///minimum value, resp.  \pre \ref phase1 must be called before.
deba@2330
   325
    ///
deba@2330
   326
    /// \todo The inexact computation can cause positive excess on a set of 
deba@2330
   327
    /// unpushable nodes. We may have to watch the empty level in this case 
deba@2330
   328
    /// due to avoid the terrible long running time.
jacint@836
   329
    void phase2()
jacint@836
   330
    {
jacint@836
   331
alpar@1222
   332
      int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@836
   333
      int b=k;    //bound on the highest level under n of an active node
jacint@836
   334
jacint@836
   335
    
alpar@1222
   336
      VecNode first(_node_num, INVALID);
alpar@1222
   337
      NNMap next(*_g, INVALID); 
alpar@1222
   338
      level.set(_source,0);
jacint@836
   339
      std::queue<Node> bfs_queue;
alpar@1222
   340
      bfs_queue.push(_source);
jacint@836
   341
jacint@836
   342
      while ( !bfs_queue.empty() ) {
jacint@836
   343
jacint@836
   344
	Node v=bfs_queue.front();
jacint@836
   345
	bfs_queue.pop();
jacint@836
   346
	int l=level[v]+1;
jacint@836
   347
alpar@1222
   348
	for(InEdgeIt e(*_g,v); e!=INVALID; ++e) {
deba@2330
   349
	  if ( !_surely.positive((*_capacity)[e] - (*_flow)[e])) continue;
alpar@1222
   350
	  Node u=_g->source(e);
alpar@1222
   351
	  if ( level[u] >= _node_num ) {
jacint@836
   352
	    bfs_queue.push(u);
jacint@836
   353
	    level.set(u, l);
deba@2330
   354
	    if ( excess[u] != 0 ) {
jacint@836
   355
	      next.set(u,first[l]);
jacint@836
   356
	      first[l]=u;
jacint@836
   357
	    }
jacint@836
   358
	  }
jacint@836
   359
	}
jacint@836
   360
alpar@1222
   361
	for(OutEdgeIt e(*_g,v); e!=INVALID; ++e) {
jacint@2024
   362
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   363
	  Node u=_g->target(e);
alpar@1222
   364
	  if ( level[u] >= _node_num ) {
jacint@836
   365
	    bfs_queue.push(u);
jacint@836
   366
	    level.set(u, l);
deba@2330
   367
	    if ( excess[u] != 0 ) {
jacint@836
   368
	      next.set(u,first[l]);
jacint@836
   369
	      first[l]=u;
jacint@836
   370
	    }
jacint@836
   371
	  }
jacint@836
   372
	}
jacint@836
   373
      }
alpar@1222
   374
      b=_node_num-2;
jacint@836
   375
jacint@836
   376
      while ( true ) {
jacint@836
   377
jacint@836
   378
	if ( b == 0 ) break;
jacint@836
   379
	if ( first[b]==INVALID ) --b;
jacint@836
   380
	else {
jacint@836
   381
	  Node w=first[b];
jacint@836
   382
	  first[b]=next[w];
jacint@836
   383
	  int newlevel=push(w,next, first);
jacint@836
   384
	  
deba@2330
   385
          if ( newlevel == _node_num) {
deba@2330
   386
            excess.set(w, 0);
deba@2330
   387
	    level.set(w,_node_num);
deba@2330
   388
          }
jacint@836
   389
	  //relabel
deba@2330
   390
	  if ( excess[w] != 0 ) {
jacint@836
   391
	    level.set(w,++newlevel);
jacint@836
   392
	    next.set(w,first[newlevel]);
jacint@836
   393
	    first[newlevel]=w;
jacint@836
   394
	    b=newlevel;
jacint@836
   395
	  }
jacint@836
   396
	} 
jacint@836
   397
      } // while(true)
jacint@836
   398
      flow_prop=GEN_FLOW;
jacint@836
   399
      status=AFTER_PREFLOW_PHASE_2;
jacint@836
   400
    }
jacint@836
   401
jacint@836
   402
    /// Returns the value of the maximum flow.
jacint@836
   403
jacint@836
   404
    /// Returns the value of the maximum flow by returning the excess
alpar@911
   405
    /// of the target node \c t. This value equals to the value of
jacint@836
   406
    /// the maximum flow already after running \ref phase1.
jacint@836
   407
    Num flowValue() const {
alpar@1222
   408
      return excess[_target];
jacint@836
   409
    }
jacint@836
   410
jacint@836
   411
jacint@836
   412
    ///Returns a minimum value cut.
jacint@836
   413
jacint@836
   414
    ///Sets \c M to the characteristic vector of a minimum value
jacint@836
   415
    ///cut. This method can be called both after running \ref
jacint@836
   416
    ///phase1 and \ref phase2. It is much faster after
marci@849
   417
    ///\ref phase1.  \pre M should be a bool-valued node-map. \pre
alpar@911
   418
    ///If \ref minCut() is called after \ref phase2() then M should
jacint@836
   419
    ///be initialized to false.
jacint@836
   420
    template<typename _CutMap>
jacint@836
   421
    void minCut(_CutMap& M) const {
jacint@836
   422
      switch ( status ) {
jacint@836
   423
	case AFTER_PREFLOW_PHASE_1:
alpar@1222
   424
	for(NodeIt v(*_g); v!=INVALID; ++v) {
alpar@1222
   425
	  if (level[v] < _node_num) {
jacint@836
   426
	    M.set(v, false);
jacint@836
   427
	  } else {
jacint@836
   428
	    M.set(v, true);
jacint@836
   429
	  }
jacint@836
   430
	}
jacint@836
   431
	break;
jacint@836
   432
	case AFTER_PREFLOW_PHASE_2:
jacint@836
   433
	minMinCut(M);
jacint@836
   434
	break;
jacint@836
   435
	case AFTER_NOTHING:
jacint@836
   436
	break;
jacint@836
   437
      }
jacint@836
   438
    }
jacint@836
   439
jacint@836
   440
    ///Returns the inclusionwise minimum of the minimum value cuts.
jacint@836
   441
jacint@836
   442
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836
   443
    ///which is inclusionwise minimum. It is computed by processing a
jacint@836
   444
    ///bfs from the source node \c s in the residual graph.  \pre M
jacint@836
   445
    ///should be a node map of bools initialized to false.  \pre \ref
jacint@836
   446
    ///phase2 should already be run.
jacint@836
   447
    template<typename _CutMap>
jacint@836
   448
    void minMinCut(_CutMap& M) const {
jacint@836
   449
jacint@836
   450
      std::queue<Node> queue;
alpar@1222
   451
      M.set(_source,true);
alpar@1227
   452
      queue.push(_source);
jacint@836
   453
      
jacint@836
   454
      while (!queue.empty()) {
jacint@836
   455
	Node w=queue.front();
jacint@836
   456
	queue.pop();
jacint@836
   457
	
alpar@1222
   458
	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   459
	  Node v=_g->target(e);
deba@2330
   460
	  if (!M[v] && _surely.positive((*_capacity)[e] -(*_flow)[e]) ) {
jacint@836
   461
	    queue.push(v);
jacint@836
   462
	    M.set(v, true);
jacint@836
   463
	  }
jacint@836
   464
	}
jacint@836
   465
	
alpar@1222
   466
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   467
	  Node v=_g->source(e);
jacint@2024
   468
	  if (!M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836
   469
	    queue.push(v);
jacint@836
   470
	    M.set(v, true);
jacint@836
   471
	  }
jacint@836
   472
	}
jacint@836
   473
      }
jacint@836
   474
    }
jacint@836
   475
    
jacint@836
   476
    ///Returns the inclusionwise maximum of the minimum value cuts.
jacint@836
   477
jacint@836
   478
    ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836
   479
    ///which is inclusionwise maximum. It is computed by processing a
jacint@836
   480
    ///backward bfs from the target node \c t in the residual graph.
alpar@911
   481
    ///\pre \ref phase2() or run() should already be run.
jacint@836
   482
    template<typename _CutMap>
jacint@836
   483
    void maxMinCut(_CutMap& M) const {
jacint@836
   484
alpar@1222
   485
      for(NodeIt v(*_g) ; v!=INVALID; ++v) M.set(v, true);
jacint@836
   486
jacint@836
   487
      std::queue<Node> queue;
jacint@836
   488
alpar@1222
   489
      M.set(_target,false);
alpar@1222
   490
      queue.push(_target);
jacint@836
   491
jacint@836
   492
      while (!queue.empty()) {
jacint@836
   493
        Node w=queue.front();
jacint@836
   494
	queue.pop();
jacint@836
   495
alpar@1222
   496
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   497
	  Node v=_g->source(e);
deba@2330
   498
	  if (M[v] && _surely.positive((*_capacity)[e] - (*_flow)[e]) ) {
jacint@836
   499
	    queue.push(v);
jacint@836
   500
	    M.set(v, false);
jacint@836
   501
	  }
jacint@836
   502
	}
jacint@836
   503
alpar@1222
   504
	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222
   505
	  Node v=_g->target(e);
jacint@2024
   506
	  if (M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836
   507
	    queue.push(v);
jacint@836
   508
	    M.set(v, false);
jacint@836
   509
	  }
jacint@836
   510
	}
jacint@836
   511
      }
jacint@836
   512
    }
jacint@836
   513
jacint@836
   514
    ///Sets the source node to \c _s.
jacint@836
   515
jacint@836
   516
    ///Sets the source node to \c _s.
jacint@836
   517
    /// 
alpar@1222
   518
    void source(Node _s) { 
alpar@1222
   519
      _source=_s; 
jacint@836
   520
      if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
jacint@836
   521
      status=AFTER_NOTHING; 
jacint@836
   522
    }
jacint@836
   523
alpar@1222
   524
    ///Returns the source node.
alpar@1222
   525
alpar@1222
   526
    ///Returns the source node.
alpar@1222
   527
    /// 
alpar@1222
   528
    Node source() const { 
alpar@1222
   529
      return _source;
alpar@1222
   530
    }
alpar@1222
   531
jacint@836
   532
    ///Sets the target node to \c _t.
jacint@836
   533
jacint@836
   534
    ///Sets the target node to \c _t.
jacint@836
   535
    ///
alpar@1222
   536
    void target(Node _t) { 
alpar@1222
   537
      _target=_t; 
jacint@836
   538
      if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
jacint@836
   539
      status=AFTER_NOTHING; 
jacint@836
   540
    }
jacint@836
   541
alpar@1222
   542
    ///Returns the target node.
alpar@1222
   543
alpar@1222
   544
    ///Returns the target node.
alpar@1222
   545
    /// 
alpar@1222
   546
    Node target() const { 
alpar@1222
   547
      return _target;
alpar@1222
   548
    }
alpar@1222
   549
jacint@836
   550
    /// Sets the edge map of the capacities to _cap.
jacint@836
   551
jacint@836
   552
    /// Sets the edge map of the capacities to _cap.
jacint@836
   553
    /// 
alpar@1222
   554
    void capacityMap(const CapacityMap& _cap) { 
alpar@1222
   555
      _capacity=&_cap; 
jacint@836
   556
      status=AFTER_NOTHING; 
jacint@836
   557
    }
zsuzska@1285
   558
    /// Returns a reference to capacity map.
alpar@1222
   559
zsuzska@1285
   560
    /// Returns a reference to capacity map.
alpar@1222
   561
    /// 
alpar@1222
   562
    const CapacityMap &capacityMap() const { 
alpar@1222
   563
      return *_capacity;
alpar@1222
   564
    }
jacint@836
   565
jacint@836
   566
    /// Sets the edge map of the flows to _flow.
jacint@836
   567
jacint@836
   568
    /// Sets the edge map of the flows to _flow.
jacint@836
   569
    /// 
alpar@1222
   570
    void flowMap(FlowMap& _f) { 
alpar@1222
   571
      _flow=&_f; 
jacint@836
   572
      flow_prop=NO_FLOW;
jacint@836
   573
      status=AFTER_NOTHING; 
jacint@836
   574
    }
alpar@1222
   575
     
zsuzska@1285
   576
    /// Returns a reference to flow map.
jacint@836
   577
zsuzska@1285
   578
    /// Returns a reference to flow map.
alpar@1222
   579
    /// 
alpar@1222
   580
    const FlowMap &flowMap() const { 
alpar@1222
   581
      return *_flow;
alpar@1222
   582
    }
jacint@836
   583
jacint@836
   584
  private:
jacint@836
   585
jacint@836
   586
    int push(Node w, NNMap& next, VecNode& first) {
jacint@836
   587
jacint@836
   588
      int lev=level[w];
jacint@836
   589
      Num exc=excess[w];
alpar@1222
   590
      int newlevel=_node_num;       //bound on the next level of w
jacint@836
   591
alpar@1222
   592
      for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
deba@2330
   593
	if ( !_surely.positive((*_capacity)[e] - (*_flow)[e])) continue;
alpar@1222
   594
	Node v=_g->target(e);
jacint@2024
   595
	
jacint@836
   596
	if( lev > level[v] ) { //Push is allowed now
jacint@836
   597
	  
deba@2330
   598
	  if ( excess[v] == 0 && v!=_target && v!=_source ) {
jacint@836
   599
	    next.set(v,first[level[v]]);
jacint@836
   600
	    first[level[v]]=v;
jacint@836
   601
	  }
jacint@836
   602
alpar@1222
   603
	  Num cap=(*_capacity)[e];
alpar@1222
   604
	  Num flo=(*_flow)[e];
jacint@836
   605
	  Num remcap=cap-flo;
jacint@836
   606
	  
jacint@2024
   607
	  if ( ! _surely.less(remcap, exc) ) { //A nonsaturating push.
jacint@836
   608
	    
alpar@1222
   609
	    _flow->set(e, flo+exc);
jacint@836
   610
	    excess.set(v, excess[v]+exc);
jacint@836
   611
	    exc=0;
jacint@836
   612
	    break;
jacint@836
   613
jacint@836
   614
	  } else { //A saturating push.
alpar@1222
   615
	    _flow->set(e, cap);
jacint@836
   616
	    excess.set(v, excess[v]+remcap);
jacint@836
   617
	    exc-=remcap;
jacint@836
   618
	  }
jacint@836
   619
	} else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836
   620
      } //for out edges wv
jacint@836
   621
deba@2330
   622
      if ( exc != 0 ) {
alpar@1222
   623
	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
jacint@836
   624
	  
jacint@2024
   625
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   626
	  Node v=_g->source(e);
jacint@2024
   627
	  
jacint@836
   628
	  if( lev > level[v] ) { //Push is allowed now
jacint@836
   629
deba@2330
   630
	    if ( excess[v] == 0 && v!=_target && v!=_source ) {
jacint@836
   631
	      next.set(v,first[level[v]]);
jacint@836
   632
	      first[level[v]]=v;
jacint@836
   633
	    }
jacint@836
   634
alpar@1222
   635
	    Num flo=(*_flow)[e];
jacint@836
   636
jacint@2024
   637
	    if ( !_surely.less(flo, exc) ) { //A nonsaturating push.
jacint@836
   638
alpar@1222
   639
	      _flow->set(e, flo-exc);
jacint@836
   640
	      excess.set(v, excess[v]+exc);
jacint@836
   641
	      exc=0;
jacint@836
   642
	      break;
jacint@836
   643
	    } else {  //A saturating push.
jacint@836
   644
jacint@836
   645
	      excess.set(v, excess[v]+flo);
jacint@836
   646
	      exc-=flo;
alpar@1222
   647
	      _flow->set(e,0);
jacint@836
   648
	    }
jacint@836
   649
	  } else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836
   650
	} //for in edges vw
jacint@836
   651
jacint@836
   652
      } // if w still has excess after the out edge for cycle
jacint@836
   653
jacint@836
   654
      excess.set(w, exc);
jacint@836
   655
      
jacint@836
   656
      return newlevel;
jacint@836
   657
    }
jacint@836
   658
    
jacint@836
   659
    
jacint@836
   660
    
jacint@836
   661
    void preflowPreproc(VecNode& first, NNMap& next, 
jacint@836
   662
			VecNode& level_list, NNMap& left, NNMap& right)
jacint@836
   663
    {
alpar@1222
   664
      for(NodeIt v(*_g); v!=INVALID; ++v) level.set(v,_node_num);
jacint@836
   665
      std::queue<Node> bfs_queue;
jacint@836
   666
      
jacint@836
   667
      if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
jacint@836
   668
	//Reverse_bfs from t in the residual graph,
jacint@836
   669
	//to find the starting level.
alpar@1222
   670
	level.set(_target,0);
alpar@1222
   671
	bfs_queue.push(_target);
jacint@836
   672
	
jacint@836
   673
	while ( !bfs_queue.empty() ) {
jacint@836
   674
	  
jacint@836
   675
	  Node v=bfs_queue.front();
jacint@836
   676
	  bfs_queue.pop();
jacint@836
   677
	  int l=level[v]+1;
jacint@836
   678
	  
alpar@1222
   679
	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
deba@2330
   680
	    if ( !_surely.positive((*_capacity)[e] - (*_flow)[e] )) continue;
alpar@1222
   681
	    Node w=_g->source(e);
alpar@1222
   682
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   683
	      bfs_queue.push(w);
jacint@836
   684
	      Node z=level_list[l];
jacint@836
   685
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   686
	      right.set(w,z);
jacint@836
   687
	      level_list[l]=w;
jacint@836
   688
	      level.set(w, l);
jacint@836
   689
	    }
jacint@836
   690
	  }
jacint@836
   691
	  
alpar@1222
   692
	  for(OutEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
jacint@2024
   693
	    if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   694
	    Node w=_g->target(e);
alpar@1222
   695
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   696
	      bfs_queue.push(w);
jacint@836
   697
	      Node z=level_list[l];
jacint@836
   698
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   699
	      right.set(w,z);
jacint@836
   700
	      level_list[l]=w;
jacint@836
   701
	      level.set(w, l);
jacint@836
   702
	    }
jacint@836
   703
	  }
jacint@836
   704
	} //while
jacint@836
   705
      } //if
jacint@836
   706
jacint@836
   707
jacint@836
   708
      switch (flow_prop) {
jacint@836
   709
	case NO_FLOW:  
alpar@1222
   710
	for(EdgeIt e(*_g); e!=INVALID; ++e) _flow->set(e,0);
jacint@836
   711
	case ZERO_FLOW:
alpar@1222
   712
	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836
   713
	
jacint@836
   714
	//Reverse_bfs from t, to find the starting level.
alpar@1222
   715
	level.set(_target,0);
alpar@1222
   716
	bfs_queue.push(_target);
jacint@836
   717
	
jacint@836
   718
	while ( !bfs_queue.empty() ) {
jacint@836
   719
	  
jacint@836
   720
	  Node v=bfs_queue.front();
jacint@836
   721
	  bfs_queue.pop();
jacint@836
   722
	  int l=level[v]+1;
jacint@836
   723
	  
alpar@1222
   724
	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
alpar@1222
   725
	    Node w=_g->source(e);
alpar@1222
   726
	    if ( level[w] == _node_num && w != _source ) {
jacint@836
   727
	      bfs_queue.push(w);
jacint@836
   728
	      Node z=level_list[l];
jacint@836
   729
	      if ( z!=INVALID ) left.set(z,w);
jacint@836
   730
	      right.set(w,z);
jacint@836
   731
	      level_list[l]=w;
jacint@836
   732
	      level.set(w, l);
jacint@836
   733
	    }
jacint@836
   734
	  }
jacint@836
   735
	}
jacint@836
   736
	
jacint@836
   737
	//the starting flow
alpar@1222
   738
	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222
   739
	  Num c=(*_capacity)[e];
jacint@2024
   740
	  if ( !_surely.positive(c) ) continue;
alpar@1222
   741
	  Node w=_g->target(e);
alpar@1222
   742
	  if ( level[w] < _node_num ) {
deba@2330
   743
	    if ( excess[w] == 0 && w!=_target ) { //putting into the stack
jacint@836
   744
	      next.set(w,first[level[w]]);
jacint@836
   745
	      first[level[w]]=w;
jacint@836
   746
	    }
alpar@1222
   747
	    _flow->set(e, c);
jacint@836
   748
	    excess.set(w, excess[w]+c);
jacint@836
   749
	  }
jacint@836
   750
	}
jacint@836
   751
	break;
jacint@836
   752
jacint@836
   753
	case GEN_FLOW:
alpar@1222
   754
	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836
   755
	{
jacint@836
   756
	  Num exc=0;
alpar@1222
   757
	  for(InEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222
   758
	  for(OutEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc-=(*_flow)[e];
deba@2330
   759
          if (!_surely.positive(exc)) {
deba@2330
   760
            exc = 0;
deba@2330
   761
          }
deba@2330
   762
          excess.set(_target,exc);
jacint@836
   763
	}
jacint@836
   764
jacint@836
   765
	//the starting flow
alpar@1222
   766
	for(OutEdgeIt e(*_g,_source); e!=INVALID; ++e)	{
alpar@1222
   767
	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024
   768
	  if ( !_surely.positive(rem) ) continue;
alpar@1222
   769
	  Node w=_g->target(e);
alpar@1222
   770
	  if ( level[w] < _node_num ) {
deba@2330
   771
	    if ( excess[w] == 0 && w!=_target ) { //putting into the stack
jacint@836
   772
	      next.set(w,first[level[w]]);
jacint@836
   773
	      first[level[w]]=w;
jacint@836
   774
	    }   
alpar@1222
   775
	    _flow->set(e, (*_capacity)[e]);
jacint@836
   776
	    excess.set(w, excess[w]+rem);
jacint@836
   777
	  }
jacint@836
   778
	}
jacint@836
   779
	
alpar@1222
   780
	for(InEdgeIt e(*_g,_source); e!=INVALID; ++e) {
jacint@2024
   781
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   782
	  Node w=_g->source(e);
alpar@1222
   783
	  if ( level[w] < _node_num ) {
deba@2330
   784
	    if ( excess[w] == 0 && w!=_target ) {
jacint@836
   785
	      next.set(w,first[level[w]]);
jacint@836
   786
	      first[level[w]]=w;
jacint@836
   787
	    }  
alpar@1222
   788
	    excess.set(w, excess[w]+(*_flow)[e]);
alpar@1222
   789
	    _flow->set(e, 0);
jacint@836
   790
	  }
jacint@836
   791
	}
jacint@836
   792
	break;
jacint@836
   793
jacint@836
   794
	case PRE_FLOW:	
jacint@836
   795
	//the starting flow
alpar@1222
   796
	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222
   797
	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024
   798
	  if ( !_surely.positive(rem) ) continue;
alpar@1222
   799
	  Node w=_g->target(e);
alpar@1222
   800
	  if ( level[w] < _node_num ) _flow->set(e, (*_capacity)[e]);
jacint@836
   801
	}
jacint@836
   802
	
alpar@1222
   803
	for(InEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
jacint@2024
   804
	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222
   805
	  Node w=_g->source(e);
alpar@1222
   806
	  if ( level[w] < _node_num ) _flow->set(e, 0);
jacint@836
   807
	}
jacint@836
   808
	
jacint@836
   809
	//computing the excess
alpar@1222
   810
	for(NodeIt w(*_g); w!=INVALID; ++w) {
jacint@836
   811
	  Num exc=0;
alpar@1222
   812
	  for(InEdgeIt e(*_g,w); e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222
   813
	  for(OutEdgeIt e(*_g,w); e!=INVALID; ++e) exc-=(*_flow)[e];
deba@2330
   814
          if (!_surely.positive(exc)) {
deba@2330
   815
            exc = 0;
deba@2330
   816
          }
jacint@836
   817
	  excess.set(w,exc);
jacint@836
   818
	  
jacint@836
   819
	  //putting the active nodes into the stack
jacint@836
   820
	  int lev=level[w];
deba@2330
   821
	    if ( exc != 0 && lev < _node_num && Node(w) != _target ) {
jacint@836
   822
	      next.set(w,first[lev]);
jacint@836
   823
	      first[lev]=w;
jacint@836
   824
	    }
jacint@836
   825
	}
jacint@836
   826
	break;
jacint@836
   827
      } //switch
jacint@836
   828
    } //preflowPreproc
jacint@836
   829
jacint@836
   830
jacint@836
   831
    void relabel(Node w, int newlevel, VecNode& first, NNMap& next, 
jacint@836
   832
		 VecNode& level_list, NNMap& left,
jacint@836
   833
		 NNMap& right, int& b, int& k, bool what_heur )
jacint@836
   834
    {
jacint@836
   835
jacint@836
   836
      int lev=level[w];
jacint@836
   837
jacint@836
   838
      Node right_n=right[w];
jacint@836
   839
      Node left_n=left[w];
jacint@836
   840
jacint@836
   841
      //unlacing starts
jacint@836
   842
      if ( right_n!=INVALID ) {
jacint@836
   843
	if ( left_n!=INVALID ) {
jacint@836
   844
	  right.set(left_n, right_n);
jacint@836
   845
	  left.set(right_n, left_n);
jacint@836
   846
	} else {
jacint@836
   847
	  level_list[lev]=right_n;
jacint@836
   848
	  left.set(right_n, INVALID);
jacint@836
   849
	}
jacint@836
   850
      } else {
jacint@836
   851
	if ( left_n!=INVALID ) {
jacint@836
   852
	  right.set(left_n, INVALID);
jacint@836
   853
	} else {
jacint@836
   854
	  level_list[lev]=INVALID;
jacint@836
   855
	}
jacint@836
   856
      }
jacint@836
   857
      //unlacing ends
jacint@836
   858
jacint@836
   859
      if ( level_list[lev]==INVALID ) {
jacint@836
   860
jacint@836
   861
	//gapping starts
jacint@836
   862
	for (int i=lev; i!=k ; ) {
jacint@836
   863
	  Node v=level_list[++i];
jacint@836
   864
	  while ( v!=INVALID ) {
alpar@1222
   865
	    level.set(v,_node_num);
jacint@836
   866
	    v=right[v];
jacint@836
   867
	  }
jacint@836
   868
	  level_list[i]=INVALID;
jacint@836
   869
	  if ( !what_heur ) first[i]=INVALID;
jacint@836
   870
	}
jacint@836
   871
alpar@1222
   872
	level.set(w,_node_num);
jacint@836
   873
	b=lev-1;
jacint@836
   874
	k=b;
jacint@836
   875
	//gapping ends
jacint@836
   876
jacint@836
   877
      } else {
jacint@836
   878
alpar@1222
   879
	if ( newlevel == _node_num ) level.set(w,_node_num);
jacint@836
   880
	else {
jacint@836
   881
	  level.set(w,++newlevel);
jacint@836
   882
	  next.set(w,first[newlevel]);
jacint@836
   883
	  first[newlevel]=w;
jacint@836
   884
	  if ( what_heur ) b=newlevel;
jacint@836
   885
	  if ( k < newlevel ) ++k;      //now k=newlevel
jacint@836
   886
	  Node z=level_list[newlevel];
jacint@836
   887
	  if ( z!=INVALID ) left.set(z,w);
jacint@836
   888
	  right.set(w,z);
jacint@836
   889
	  left.set(w,INVALID);
jacint@836
   890
	  level_list[newlevel]=w;
jacint@836
   891
	}
jacint@836
   892
      }
jacint@836
   893
    } //relabel
jacint@836
   894
jacint@836
   895
  }; 
alpar@1227
   896
deba@2376
   897
  ///\ingroup max_flow
deba@1792
   898
  ///\brief Function type interface for Preflow algorithm.
deba@1792
   899
  ///
alpar@1227
   900
  ///Function type interface for Preflow algorithm.
alpar@1227
   901
  ///\sa Preflow
alpar@1227
   902
  template<class GR, class CM, class FM>
alpar@1227
   903
  Preflow<GR,typename CM::Value,CM,FM> preflow(const GR &g,
alpar@1227
   904
			    typename GR::Node source,
alpar@1227
   905
			    typename GR::Node target,
alpar@1227
   906
			    const CM &cap,
alpar@1227
   907
			    FM &flow
alpar@1227
   908
			    )
alpar@1227
   909
  {
alpar@1227
   910
    return Preflow<GR,typename CM::Value,CM,FM>(g,source,target,cap,flow);
alpar@1227
   911
  }
alpar@1227
   912
alpar@921
   913
} //namespace lemon
jacint@836
   914
alpar@921
   915
#endif //LEMON_PREFLOW_H