src/work/marci/augmenting_flow.h
author marci
Mon, 20 Sep 2004 17:53:33 +0000
changeset 890 3a48bc350e0f
parent 863 d27bbe17b0b8
child 921 818510fa3d99
permissions -rw-r--r--
Specialized ConstMap for defining constant maps at compile time, by klao.
Time comparision of the generic and specialized maps.
marci@762
     1
// -*- C++ -*-
marci@762
     2
#ifndef HUGO_AUGMENTING_FLOW_H
marci@762
     3
#define HUGO_AUGMENTING_FLOW_H
marci@762
     4
marci@762
     5
#include <vector>
marci@762
     6
#include <iostream>
marci@762
     7
marci@762
     8
#include <hugo/graph_wrapper.h>
marci@762
     9
#include <bfs_dfs.h>
marci@762
    10
#include <hugo/invalid.h>
marci@762
    11
#include <hugo/maps.h>
marci@888
    12
#include <hugo/tight_edge_filter_map.h>
marci@762
    13
marci@762
    14
/// \file
marci@762
    15
/// \brief Maximum flow algorithms.
marci@762
    16
/// \ingroup galgs
marci@762
    17
marci@762
    18
namespace hugo {
marci@762
    19
marci@762
    20
  /// \addtogroup galgs
marci@762
    21
  /// @{                                                                                                                                        
marci@862
    22
  /// Class for augmenting path flow algorithms.
marci@762
    23
marci@862
    24
  /// This class provides various algorithms for finding a flow of
marci@862
    25
  /// maximum value in a directed graph. The \e source node, the \e
marci@862
    26
  /// target node, the \e capacity of the edges and the \e starting \e
marci@862
    27
  /// flow value of the edges should be passed to the algorithm through the
marci@862
    28
  /// constructor. 
marci@862
    29
//   /// It is possible to change these quantities using the
marci@862
    30
//   /// functions \ref resetSource, \ref resetTarget, \ref resetCap and
marci@862
    31
//   /// \ref resetFlow. Before any subsequent runs of any algorithm of
marci@862
    32
//   /// the class \ref resetFlow should be called. 
marci@762
    33
marci@862
    34
  /// After running an algorithm of the class, the actual flow value 
marci@862
    35
  /// can be obtained by calling \ref flowValue(). The minimum
marci@862
    36
  /// value cut can be written into a \c node map of \c bools by
marci@862
    37
  /// calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
marci@862
    38
  /// the inclusionwise minimum and maximum of the minimum value
marci@862
    39
  /// cuts, resp.)                                                                                                                               
marci@762
    40
  ///\param Graph The directed graph type the algorithm runs on.
marci@762
    41
  ///\param Num The number type of the capacities and the flow values.
marci@762
    42
  ///\param CapMap The capacity map type.
marci@762
    43
  ///\param FlowMap The flow map type.                                                                                                           
marci@862
    44
  ///\author Marton Makai
marci@762
    45
  template <typename Graph, typename Num,
marci@762
    46
	    typename CapMap=typename Graph::template EdgeMap<Num>,
marci@762
    47
            typename FlowMap=typename Graph::template EdgeMap<Num> >
marci@762
    48
  class AugmentingFlow {
marci@762
    49
  protected:
marci@762
    50
    typedef typename Graph::Node Node;
marci@762
    51
    typedef typename Graph::NodeIt NodeIt;
marci@762
    52
    typedef typename Graph::EdgeIt EdgeIt;
marci@762
    53
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@762
    54
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@762
    55
marci@762
    56
    const Graph* g;
marci@762
    57
    Node s;
marci@762
    58
    Node t;
marci@762
    59
    const CapMap* capacity;
marci@762
    60
    FlowMap* flow;
marci@762
    61
//    int n;      //the number of nodes of G
marci@762
    62
    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
marci@762
    63
    //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
marci@762
    64
    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
marci@762
    65
    typedef typename ResGW::Edge ResGWEdge;
marci@762
    66
    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
marci@762
    67
    typedef typename Graph::template NodeMap<int> ReachedMap;
marci@762
    68
marci@762
    69
    //level works as a bool map in augmenting path algorithms and is
marci@762
    70
    //used by bfs for storing reached information.  In preflow, it
marci@762
    71
    //shows the levels of nodes.     
marci@762
    72
    ReachedMap level;
marci@762
    73
marci@762
    74
  public:
marci@762
    75
    ///Indicates the property of the starting flow.
marci@762
    76
marci@762
    77
    ///Indicates the property of the starting flow. The meanings are as follows:
marci@762
    78
    ///- \c ZERO_FLOW: constant zero flow
marci@762
    79
    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
marci@762
    80
    ///the sum of the out-flows in every node except the \e source and
marci@762
    81
    ///the \e target.
marci@762
    82
    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
marci@762
    83
    ///least the sum of the out-flows in every node except the \e source.
marci@762
    84
    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
marci@762
    85
    ///set to the constant zero flow in the beginning of the algorithm in this case.
marci@762
    86
    enum FlowEnum{
marci@762
    87
      ZERO_FLOW,
marci@762
    88
      GEN_FLOW,
marci@762
    89
      PRE_FLOW,
marci@762
    90
      NO_FLOW
marci@762
    91
    };
marci@762
    92
marci@762
    93
    enum StatusEnum {
marci@762
    94
      AFTER_NOTHING,
marci@762
    95
      AFTER_AUGMENTING,
marci@762
    96
      AFTER_FAST_AUGMENTING, 
marci@762
    97
      AFTER_PRE_FLOW_PHASE_1,      
marci@762
    98
      AFTER_PRE_FLOW_PHASE_2
marci@762
    99
    };
marci@762
   100
marci@762
   101
    /// Don not needle this flag only if necessary.
marci@762
   102
    StatusEnum status;
marci@762
   103
    int number_of_augmentations;
marci@762
   104
marci@762
   105
marci@762
   106
    template<typename IntMap>
marci@762
   107
    class TrickyReachedMap {
marci@762
   108
    protected:
marci@762
   109
      IntMap* map;
marci@762
   110
      int* number_of_augmentations;
marci@762
   111
    public:
marci@762
   112
      TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
marci@762
   113
	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
marci@762
   114
      void set(const Node& n, bool b) {
marci@762
   115
	if (b)
marci@762
   116
	  map->set(n, *number_of_augmentations);
marci@762
   117
	else 
marci@762
   118
	  map->set(n, *number_of_augmentations-1);
marci@762
   119
      }
marci@762
   120
      bool operator[](const Node& n) const { 
marci@762
   121
	return (*map)[n]==*number_of_augmentations; 
marci@762
   122
      }
marci@762
   123
    };
marci@762
   124
    
marci@762
   125
    AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
marci@762
   126
		   FlowMap& _flow) :
marci@762
   127
      g(&_G), s(_s), t(_t), capacity(&_capacity),
marci@762
   128
      flow(&_flow), //n(_G.nodeNum()), 
marci@762
   129
      level(_G), //excess(_G,0), 
marci@762
   130
      status(AFTER_NOTHING), number_of_augmentations(0) { }
marci@762
   131
marci@762
   132
    /// Starting from a flow, this method searches for an augmenting path
marci@762
   133
    /// according to the Edmonds-Karp algorithm
marci@762
   134
    /// and augments the flow on if any.
marci@762
   135
    /// The return value shows if the augmentation was succesful.
marci@762
   136
    bool augmentOnShortestPath();
marci@762
   137
    bool augmentOnShortestPath2();
marci@762
   138
marci@762
   139
    /// Starting from a flow, this method searches for an augmenting blocking
marci@762
   140
    /// flow according to Dinits' algorithm and augments the flow on if any.
marci@762
   141
    /// The blocking flow is computed in a physically constructed
marci@762
   142
    /// residual graph of type \c Mutablegraph.
marci@762
   143
    /// The return value show sif the augmentation was succesful.
marci@762
   144
    template<typename MutableGraph> bool augmentOnBlockingFlow();
marci@762
   145
marci@762
   146
    /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
marci@762
   147
    /// residual graph is not constructed physically.
marci@762
   148
    /// The return value shows if the augmentation was succesful.
marci@762
   149
    bool augmentOnBlockingFlow2();
marci@762
   150
marci@762
   151
    template<typename _CutMap>
marci@762
   152
    void actMinCut(_CutMap& M) const {
marci@762
   153
      NodeIt v;
marci@762
   154
      switch (status) {
marci@762
   155
	case AFTER_PRE_FLOW_PHASE_1:
marci@762
   156
//	std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
marci@762
   157
// 	for(g->first(v); g->valid(v); g->next(v)) {
marci@762
   158
// 	  if (level[v] < n) {
marci@762
   159
// 	    M.set(v, false);
marci@762
   160
// 	  } else {
marci@762
   161
// 	    M.set(v, true);
marci@762
   162
// 	  }
marci@762
   163
// 	}
marci@762
   164
	break;
marci@762
   165
      case AFTER_PRE_FLOW_PHASE_2:
marci@762
   166
//	std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
marci@762
   167
	break;
marci@762
   168
      case AFTER_NOTHING:
marci@762
   169
//	std::cout << "AFTER_NOTHING" << std::endl;
marci@762
   170
	minMinCut(M);
marci@762
   171
	break;
marci@762
   172
      case AFTER_AUGMENTING:
marci@762
   173
//	std::cout << "AFTER_AUGMENTING" << std::endl;
marci@775
   174
	for(g->first(v); v!=INVALID; ++v) {
marci@762
   175
	  if (level[v]) {
marci@762
   176
	    M.set(v, true);
marci@762
   177
	  } else {
marci@762
   178
	    M.set(v, false);
marci@762
   179
	  }
marci@762
   180
	}
marci@762
   181
	break;
marci@762
   182
      case AFTER_FAST_AUGMENTING:
marci@762
   183
//	std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
marci@775
   184
	for(g->first(v); v!=INVALID; ++v) {
marci@762
   185
	  if (level[v]==number_of_augmentations) {
marci@762
   186
	    M.set(v, true);
marci@762
   187
	  } else {
marci@762
   188
	    M.set(v, false);
marci@762
   189
	  }
marci@762
   190
	}
marci@762
   191
	break;
marci@762
   192
      }
marci@762
   193
    }
marci@762
   194
marci@762
   195
    template<typename _CutMap>
marci@762
   196
    void minMinCut(_CutMap& M) const {
marci@762
   197
      std::queue<Node> queue;
marci@762
   198
marci@762
   199
      M.set(s,true);
marci@762
   200
      queue.push(s);
marci@762
   201
marci@762
   202
      while (!queue.empty()) {
marci@762
   203
        Node w=queue.front();
marci@762
   204
	queue.pop();
marci@762
   205
marci@762
   206
	OutEdgeIt e;
marci@775
   207
	for(g->first(e,w) ; e!=INVALID; ++e) {
marci@762
   208
	  Node v=g->head(e);
marci@762
   209
	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
marci@762
   210
	    queue.push(v);
marci@762
   211
	    M.set(v, true);
marci@762
   212
	  }
marci@762
   213
	}
marci@762
   214
marci@762
   215
	InEdgeIt f;
marci@775
   216
	for(g->first(f,w) ; f!=INVALID; ++f) {
marci@762
   217
	  Node v=g->tail(f);
marci@762
   218
	  if (!M[v] && (*flow)[f] > 0 ) {
marci@762
   219
	    queue.push(v);
marci@762
   220
	    M.set(v, true);
marci@762
   221
	  }
marci@762
   222
	}
marci@762
   223
      }
marci@762
   224
    }
marci@762
   225
marci@762
   226
    template<typename _CutMap>
marci@762
   227
    void minMinCut2(_CutMap& M) const {
marci@762
   228
      ResGW res_graph(*g, *capacity, *flow);
marci@762
   229
      BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
marci@762
   230
      bfs.pushAndSetReached(s);
marci@762
   231
      while (!bfs.finished()) ++bfs;
marci@762
   232
    }
marci@762
   233
marci@762
   234
    Num flowValue() const {
marci@762
   235
      Num a=0;
marci@777
   236
      for (InEdgeIt e(*g, t); e!=INVALID; ++e) a+=(*flow)[e];
marci@777
   237
      for (OutEdgeIt e(*g, t); e!=INVALID; ++e) a-=(*flow)[e];
marci@762
   238
      return a;
marci@762
   239
      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
marci@762
   240
    }
marci@762
   241
marci@762
   242
  };
marci@762
   243
marci@762
   244
marci@762
   245
marci@762
   246
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@762
   247
  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
marci@762
   248
  {
marci@762
   249
    ResGW res_graph(*g, *capacity, *flow);
marci@888
   250
    typename ResGW::ResCap res_cap(res_graph);
marci@888
   251
marci@762
   252
    bool _augment=false;
marci@762
   253
marci@762
   254
    //ReachedMap level(res_graph);
marci@777
   255
    for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
marci@762
   256
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@762
   257
    bfs.pushAndSetReached(s);
marci@762
   258
marci@762
   259
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
marci@762
   260
    pred.set(s, INVALID);
marci@762
   261
marci@762
   262
    typename ResGW::template NodeMap<Num> free(res_graph);
marci@762
   263
marci@762
   264
    //searching for augmenting path
marci@762
   265
    while ( !bfs.finished() ) {
marci@777
   266
      ResGWEdge e=bfs;
marci@775
   267
      if (e!=INVALID && bfs.isBNodeNewlyReached()) {
marci@762
   268
	Node v=res_graph.tail(e);
marci@762
   269
	Node w=res_graph.head(e);
marci@762
   270
	pred.set(w, e);
marci@775
   271
	if (pred[v]!=INVALID) {
marci@888
   272
	  free.set(w, std::min(free[v], res_cap[e]));
marci@762
   273
	} else {
marci@888
   274
	  free.set(w, res_cap[e]);
marci@762
   275
	}
marci@762
   276
	if (res_graph.head(e)==t) { _augment=true; break; }
marci@762
   277
      }
marci@762
   278
marci@762
   279
      ++bfs;
marci@762
   280
    } //end of searching augmenting path
marci@762
   281
marci@762
   282
    if (_augment) {
marci@762
   283
      Node n=t;
marci@762
   284
      Num augment_value=free[t];
marci@775
   285
      while (pred[n]!=INVALID) {
marci@762
   286
	ResGWEdge e=pred[n];
marci@762
   287
	res_graph.augment(e, augment_value);
marci@762
   288
	n=res_graph.tail(e);
marci@762
   289
      }
marci@762
   290
    }
marci@762
   291
marci@762
   292
    status=AFTER_AUGMENTING;
marci@762
   293
    return _augment;
marci@762
   294
  }
marci@762
   295
marci@762
   296
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@762
   297
  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
marci@762
   298
  {
marci@762
   299
    ResGW res_graph(*g, *capacity, *flow);
marci@888
   300
    typename ResGW::ResCap res_cap(res_graph);
marci@888
   301
marci@762
   302
    bool _augment=false;
marci@762
   303
marci@762
   304
    if (status!=AFTER_FAST_AUGMENTING) {
marci@777
   305
      for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0); 
marci@762
   306
      number_of_augmentations=1;
marci@762
   307
    } else {
marci@762
   308
      ++number_of_augmentations;
marci@762
   309
    }
marci@762
   310
    TrickyReachedMap<ReachedMap> 
marci@762
   311
      tricky_reached_map(level, number_of_augmentations);
marci@762
   312
    //ReachedMap level(res_graph);
marci@762
   313
//    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
marci@762
   314
    BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > 
marci@762
   315
      bfs(res_graph, tricky_reached_map);
marci@762
   316
    bfs.pushAndSetReached(s);
marci@762
   317
marci@762
   318
    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
marci@762
   319
    pred.set(s, INVALID);
marci@762
   320
marci@762
   321
    typename ResGW::template NodeMap<Num> free(res_graph);
marci@762
   322
marci@762
   323
    //searching for augmenting path
marci@762
   324
    while ( !bfs.finished() ) {
marci@777
   325
      ResGWEdge e=bfs;
marci@775
   326
      if (e!=INVALID && bfs.isBNodeNewlyReached()) {
marci@762
   327
	Node v=res_graph.tail(e);
marci@762
   328
	Node w=res_graph.head(e);
marci@762
   329
	pred.set(w, e);
marci@775
   330
	if (pred[v]!=INVALID) {
marci@888
   331
	  free.set(w, std::min(free[v], res_cap[e]));
marci@762
   332
	} else {
marci@888
   333
	  free.set(w, res_cap[e]);
marci@762
   334
	}
marci@762
   335
	if (res_graph.head(e)==t) { _augment=true; break; }
marci@762
   336
      }
marci@762
   337
marci@762
   338
      ++bfs;
marci@762
   339
    } //end of searching augmenting path
marci@762
   340
marci@762
   341
    if (_augment) {
marci@762
   342
      Node n=t;
marci@762
   343
      Num augment_value=free[t];
marci@775
   344
      while (pred[n]!=INVALID) {
marci@762
   345
	ResGWEdge e=pred[n];
marci@762
   346
	res_graph.augment(e, augment_value);
marci@762
   347
	n=res_graph.tail(e);
marci@762
   348
      }
marci@762
   349
    }
marci@762
   350
marci@762
   351
    status=AFTER_FAST_AUGMENTING;
marci@762
   352
    return _augment;
marci@762
   353
  }
marci@762
   354
marci@762
   355
marci@762
   356
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@762
   357
  template<typename MutableGraph>
marci@762
   358
  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
marci@762
   359
  {
marci@762
   360
    typedef MutableGraph MG;
marci@762
   361
    bool _augment=false;
marci@762
   362
marci@762
   363
    ResGW res_graph(*g, *capacity, *flow);
marci@888
   364
    typename ResGW::ResCap res_cap(res_graph);
marci@762
   365
marci@762
   366
    //bfs for distances on the residual graph
marci@762
   367
    //ReachedMap level(res_graph);
marci@777
   368
    for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
marci@762
   369
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@762
   370
    bfs.pushAndSetReached(s);
marci@762
   371
    typename ResGW::template NodeMap<int>
marci@762
   372
      dist(res_graph); //filled up with 0's
marci@762
   373
marci@762
   374
    //F will contain the physical copy of the residual graph
marci@762
   375
    //with the set of edges which are on shortest paths
marci@762
   376
    MG F;
marci@762
   377
    typename ResGW::template NodeMap<typename MG::Node>
marci@762
   378
      res_graph_to_F(res_graph);
marci@762
   379
    {
marci@762
   380
      typename ResGW::NodeIt n;
marci@862
   381
      for(res_graph.first(n); n!=INVALID; ++n) 
marci@762
   382
	res_graph_to_F.set(n, F.addNode());
marci@762
   383
    }
marci@762
   384
marci@762
   385
    typename MG::Node sF=res_graph_to_F[s];
marci@762
   386
    typename MG::Node tF=res_graph_to_F[t];
marci@762
   387
    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
marci@762
   388
    typename MG::template EdgeMap<Num> residual_capacity(F);
marci@762
   389
marci@762
   390
    while ( !bfs.finished() ) {
marci@777
   391
      ResGWEdge e=bfs;
marci@775
   392
      if (e!=INVALID) {
marci@762
   393
	if (bfs.isBNodeNewlyReached()) {
marci@762
   394
	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
marci@762
   395
	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
marci@762
   396
					res_graph_to_F[res_graph.head(e)]);
marci@854
   397
	  //original_edge.update();
marci@762
   398
	  original_edge.set(f, e);
marci@854
   399
	  //residual_capacity.update();
marci@888
   400
	  residual_capacity.set(f, res_cap[e]);
marci@762
   401
	} else {
marci@762
   402
	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
marci@762
   403
	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
marci@762
   404
					  res_graph_to_F[res_graph.head(e)]);
marci@854
   405
	    //original_edge.update();
marci@762
   406
	    original_edge.set(f, e);
marci@854
   407
	    //residual_capacity.update();
marci@888
   408
	    residual_capacity.set(f, res_cap[e]);
marci@762
   409
	  }
marci@762
   410
	}
marci@762
   411
      }
marci@762
   412
      ++bfs;
marci@762
   413
    } //computing distances from s in the residual graph
marci@762
   414
marci@762
   415
    bool __augment=true;
marci@762
   416
marci@762
   417
    while (__augment) {
marci@762
   418
      __augment=false;
marci@762
   419
      //computing blocking flow with dfs
marci@762
   420
      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
marci@762
   421
      typename MG::template NodeMap<typename MG::Edge> pred(F);
marci@762
   422
      pred.set(sF, INVALID);
marci@762
   423
      //invalid iterators for sources
marci@762
   424
marci@762
   425
      typename MG::template NodeMap<Num> free(F);
marci@762
   426
marci@762
   427
      dfs.pushAndSetReached(sF);
marci@762
   428
      while (!dfs.finished()) {
marci@762
   429
	++dfs;
marci@854
   430
	if (typename MG::Edge(dfs)!=INVALID) {
marci@762
   431
	  if (dfs.isBNodeNewlyReached()) {
marci@777
   432
	    typename MG::Node v=F.tail(dfs);
marci@777
   433
	    typename MG::Node w=F.head(dfs);
marci@762
   434
	    pred.set(w, dfs);
marci@775
   435
	    if (pred[v]!=INVALID) {
marci@762
   436
	      free.set(w, std::min(free[v], residual_capacity[dfs]));
marci@762
   437
	    } else {
marci@762
   438
	      free.set(w, residual_capacity[dfs]);
marci@762
   439
	    }
marci@762
   440
	    if (w==tF) {
marci@762
   441
	      __augment=true;
marci@762
   442
	      _augment=true;
marci@762
   443
	      break;
marci@762
   444
	    }
marci@762
   445
marci@762
   446
	  } else {
marci@854
   447
	    F.erase(typename MG::Edge(dfs));
marci@762
   448
	  }
marci@762
   449
	}
marci@762
   450
      }
marci@762
   451
marci@762
   452
      if (__augment) {
marci@762
   453
	typename MG::Node n=tF;
marci@762
   454
	Num augment_value=free[tF];
marci@775
   455
	while (pred[n]!=INVALID) {
marci@762
   456
	  typename MG::Edge e=pred[n];
marci@762
   457
	  res_graph.augment(original_edge[e], augment_value);
marci@762
   458
	  n=F.tail(e);
marci@762
   459
	  if (residual_capacity[e]==augment_value)
marci@762
   460
	    F.erase(e);
marci@762
   461
	  else
marci@762
   462
	    residual_capacity.set(e, residual_capacity[e]-augment_value);
marci@762
   463
	}
marci@762
   464
      }
marci@762
   465
marci@762
   466
    }
marci@762
   467
marci@762
   468
    status=AFTER_AUGMENTING;
marci@762
   469
    return _augment;
marci@762
   470
  }
marci@762
   471
marci@862
   472
  /// Blocking flow augmentation without constructing the layered 
marci@862
   473
  /// graph physically in which the blocking flow is computed.
marci@762
   474
  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
marci@762
   475
  bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
marci@762
   476
  {
marci@762
   477
    bool _augment=false;
marci@762
   478
marci@762
   479
    ResGW res_graph(*g, *capacity, *flow);
marci@888
   480
    typename ResGW::ResCap res_cap(res_graph);
marci@762
   481
marci@862
   482
    //Potential map, for distances from s
marci@862
   483
    typename ResGW::template NodeMap<int> potential(res_graph, 0);
marci@862
   484
    typedef ConstMap<typename ResGW::Edge, int> Const1Map; 
marci@862
   485
    Const1Map const_1_map(1);
marci@862
   486
    TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>,
marci@862
   487
      Const1Map> tight_edge_filter(res_graph, potential, const_1_map);
marci@862
   488
marci@777
   489
    for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
marci@762
   490
    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
marci@862
   491
    bfs.pushAndSetReached(s);
marci@762
   492
marci@862
   493
    //computing distances from s in the residual graph
marci@762
   494
    while ( !bfs.finished() ) {
marci@777
   495
      ResGWEdge e=bfs;
marci@862
   496
      if (e!=INVALID && bfs.isBNodeNewlyReached())
marci@862
   497
	potential.set(res_graph.head(e), potential[res_graph.tail(e)]+1);
marci@762
   498
      ++bfs;
marci@862
   499
    } 
marci@762
   500
marci@862
   501
    //Subgraph containing the edges on some shortest paths 
marci@862
   502
    //(i.e. tight edges)
marci@762
   503
    ConstMap<typename ResGW::Node, bool> true_map(true);
marci@762
   504
    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
marci@862
   505
      TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>, 
marci@862
   506
      Const1Map> > FilterResGW;
marci@862
   507
    FilterResGW filter_res_graph(res_graph, true_map, tight_edge_filter);
marci@762
   508
marci@762
   509
    //Subgraph, which is able to delete edges which are already
marci@762
   510
    //met by the dfs
marci@777
   511
    typename FilterResGW::template NodeMap<typename FilterResGW::Edge>
marci@762
   512
      first_out_edges(filter_res_graph);
marci@862
   513
    for (typename FilterResGW::NodeIt v(filter_res_graph); v!=INVALID; ++v)
marci@862
   514
      first_out_edges.set
marci@862
   515
	(v, typename FilterResGW::OutEdgeIt(filter_res_graph, v));
marci@862
   516
marci@762
   517
    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
marci@777
   518
      template NodeMap<typename FilterResGW::Edge> > ErasingResGW;
marci@762
   519
    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
marci@762
   520
marci@762
   521
    bool __augment=true;
marci@762
   522
marci@762
   523
    while (__augment) {
marci@762
   524
marci@762
   525
      __augment=false;
marci@762
   526
      //computing blocking flow with dfs
marci@762
   527
      DfsIterator< ErasingResGW,
marci@762
   528
	typename ErasingResGW::template NodeMap<bool> >
marci@762
   529
	dfs(erasing_res_graph);
marci@762
   530
      typename ErasingResGW::
marci@777
   531
	template NodeMap<typename ErasingResGW::Edge> pred(erasing_res_graph);
marci@762
   532
      pred.set(s, INVALID);
marci@762
   533
      //invalid iterators for sources
marci@762
   534
marci@762
   535
      typename ErasingResGW::template NodeMap<Num>
marci@762
   536
	free1(erasing_res_graph);
marci@762
   537
marci@762
   538
      dfs.pushAndSetReached
marci@777
   539
	/// \bug hugo 0.2
marci@762
   540
	(typename ErasingResGW::Node
marci@762
   541
	 (typename FilterResGW::Node
marci@762
   542
	  (typename ResGW::Node(s)
marci@762
   543
	   )
marci@762
   544
	  )
marci@762
   545
	 );
marci@777
   546
	
marci@762
   547
      while (!dfs.finished()) {
marci@762
   548
	++dfs;
marci@862
   549
	if (typename ErasingResGW::Edge(dfs)!=INVALID) {
marci@862
   550
	  if (dfs.isBNodeNewlyReached()) {
marci@862
   551
	    
marci@862
   552
	    typename ErasingResGW::Node v=erasing_res_graph.tail(dfs);
marci@862
   553
	    typename ErasingResGW::Node w=erasing_res_graph.head(dfs);
marci@762
   554
marci@862
   555
	    pred.set(w, typename ErasingResGW::Edge(dfs));
marci@862
   556
	    if (pred[v]!=INVALID) {
marci@862
   557
	      free1.set
marci@888
   558
		(w, std::min(free1[v], res_cap
marci@888
   559
			     [typename ErasingResGW::Edge(dfs)]));
marci@862
   560
	    } else {
marci@862
   561
	      free1.set
marci@888
   562
		(w, res_cap
marci@888
   563
		 [typename ErasingResGW::Edge(dfs)]);
marci@862
   564
	    }
marci@762
   565
marci@862
   566
	    if (w==t) {
marci@862
   567
	      __augment=true;
marci@862
   568
	      _augment=true;
marci@862
   569
	      break;
marci@762
   570
	    }
marci@862
   571
	  } else {
marci@862
   572
	    erasing_res_graph.erase(dfs);
marci@762
   573
	  }
marci@862
   574
	}
marci@762
   575
      }
marci@762
   576
marci@762
   577
      if (__augment) {
marci@762
   578
	typename ErasingResGW::Node
marci@762
   579
	  n=typename FilterResGW::Node(typename ResGW::Node(t));
marci@762
   580
	Num augment_value=free1[n];
marci@777
   581
	while (pred[n]!=INVALID) {
marci@777
   582
	  typename ErasingResGW::Edge e=pred[n];
marci@762
   583
	  res_graph.augment(e, augment_value);
marci@762
   584
	  n=erasing_res_graph.tail(e);
marci@888
   585
	  if (res_cap[e]==0)
marci@762
   586
	    erasing_res_graph.erase(e);
marci@762
   587
	}
marci@762
   588
      }
marci@762
   589
marci@762
   590
    } //while (__augment)
marci@762
   591
marci@762
   592
    status=AFTER_AUGMENTING;
marci@762
   593
    return _augment;
marci@762
   594
  }
marci@762
   595
marci@762
   596
marci@762
   597
} //namespace hugo
marci@762
   598
marci@762
   599
#endif //HUGO_AUGMENTING_FLOW_H
marci@762
   600
marci@762
   601