src/work/edmonds_karp.hh
author alpar
Wed, 10 Mar 2004 17:47:54 +0000
changeset 164 970b265696b0
parent 148 004fdf703abb
child 168 27fbd1559fb7
permissions -rw-r--r--
New graph interface
marci@59
     1
#ifndef EDMONDS_KARP_HH
marci@59
     2
#define EDMONDS_KARP_HH
marci@43
     3
marci@43
     4
#include <algorithm>
marci@75
     5
#include <list>
marci@75
     6
#include <iterator>
marci@43
     7
marci@43
     8
#include <bfs_iterator.hh>
marci@133
     9
//#include <time_measure.h>
marci@43
    10
alpar@107
    11
namespace hugo {
marci@43
    12
marci@75
    13
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@43
    14
  class ResGraph {
klao@127
    15
  public:
marci@43
    16
    typedef typename Graph::NodeIt NodeIt;
marci@43
    17
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
    18
  private:
marci@43
    19
    typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@43
    20
    const Graph& G;
marci@59
    21
    FlowMap& flow;
marci@59
    22
    const CapacityMap& capacity;
marci@43
    23
  public:
marci@59
    24
    ResGraph(const Graph& _G, FlowMap& _flow, 
marci@59
    25
	     const CapacityMap& _capacity) : 
marci@43
    26
      G(_G), flow(_flow), capacity(_capacity) { }
marci@43
    27
marci@43
    28
    class EdgeIt; 
marci@43
    29
    class OutEdgeIt; 
marci@43
    30
    friend class EdgeIt; 
marci@43
    31
    friend class OutEdgeIt; 
marci@43
    32
marci@43
    33
    class EdgeIt {
marci@75
    34
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@43
    35
    protected:
marci@75
    36
      const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
marci@43
    37
      OldSymEdgeIt sym;
marci@43
    38
    public:
marci@43
    39
      EdgeIt() { } 
marci@43
    40
      //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
marci@75
    41
      Number free() const { 
marci@43
    42
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@43
    43
	  return (resG->capacity.get(sym)-resG->flow.get(sym)); 
marci@43
    44
	} else { 
marci@43
    45
	  return (resG->flow.get(sym)); 
marci@43
    46
	}
marci@43
    47
      }
marci@43
    48
      bool valid() const { return sym.valid(); }
marci@75
    49
      void augment(Number a) const {
marci@43
    50
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@43
    51
	  resG->flow.set(sym, resG->flow.get(sym)+a);
marci@75
    52
	  //resG->flow[sym]+=a;
marci@43
    53
	} else { 
marci@43
    54
	  resG->flow.set(sym, resG->flow.get(sym)-a);
marci@75
    55
	  //resG->flow[sym]-=a;
marci@43
    56
	}
marci@43
    57
      }
marci@43
    58
    };
marci@43
    59
marci@43
    60
    class OutEdgeIt : public EdgeIt {
marci@75
    61
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@43
    62
    public:
marci@43
    63
      OutEdgeIt() { }
marci@43
    64
      //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
marci@43
    65
    private:
marci@75
    66
      OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) { 
marci@43
    67
      	resG=&_resG;
marci@43
    68
	sym=resG->G.template first<OldSymEdgeIt>(v);
marci@43
    69
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@43
    70
      }
marci@43
    71
    public:
marci@43
    72
      OutEdgeIt& operator++() { 
marci@43
    73
	++sym; 
marci@43
    74
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@43
    75
	return *this; 
marci@43
    76
      }
marci@43
    77
    };
marci@43
    78
marci@64
    79
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@43
    80
      e=OutEdgeIt(*this, v); 
marci@43
    81
    }
marci@43
    82
    void getFirst(EachNodeIt& v) const { G.getFirst(v); }
marci@43
    83
    
marci@43
    84
    template< typename It >
marci@43
    85
    It first() const { 
marci@75
    86
      It e;      
marci@75
    87
      getFirst(e);
marci@75
    88
      return e; 
marci@75
    89
    }
marci@75
    90
marci@75
    91
    template< typename It >
marci@75
    92
    It first(NodeIt v) const { 
marci@75
    93
      It e;
marci@75
    94
      getFirst(e, v);
marci@75
    95
      return e; 
marci@75
    96
    }
marci@75
    97
marci@75
    98
    NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
marci@75
    99
    NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
marci@75
   100
marci@75
   101
    NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
marci@75
   102
    NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
marci@75
   103
marci@75
   104
    int id(NodeIt v) const { return G.id(v); }
marci@75
   105
marci@75
   106
    template <typename S>
marci@75
   107
    class NodeMap {
marci@75
   108
      typename Graph::NodeMap<S> node_map; 
marci@75
   109
    public:
marci@75
   110
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@75
   111
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@75
   112
      void set(NodeIt nit, S a) { node_map.set(nit, a); }
marci@75
   113
      S get(NodeIt nit) const { return node_map.get(nit); }
marci@75
   114
      S& operator[](NodeIt nit) { return node_map[nit]; } 
marci@75
   115
      const S& operator[](NodeIt nit) const { return node_map[nit]; } 
marci@75
   116
    };
marci@75
   117
marci@75
   118
  };
marci@75
   119
marci@75
   120
marci@75
   121
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@75
   122
  class ResGraph2 {
klao@127
   123
  public:
marci@75
   124
    typedef typename Graph::NodeIt NodeIt;
marci@75
   125
    typedef typename Graph::EachNodeIt EachNodeIt;
klao@127
   126
  private:
marci@75
   127
    //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@75
   128
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@75
   129
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@75
   130
    
marci@75
   131
    const Graph& G;
marci@75
   132
    FlowMap& flow;
marci@75
   133
    const CapacityMap& capacity;
marci@75
   134
  public:
marci@75
   135
    ResGraph2(const Graph& _G, FlowMap& _flow, 
marci@75
   136
	     const CapacityMap& _capacity) : 
marci@75
   137
      G(_G), flow(_flow), capacity(_capacity) { }
marci@75
   138
marci@75
   139
    class EdgeIt; 
marci@75
   140
    class OutEdgeIt; 
marci@75
   141
    friend class EdgeIt; 
marci@75
   142
    friend class OutEdgeIt; 
marci@75
   143
marci@75
   144
    class EdgeIt {
marci@75
   145
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@75
   146
    protected:
marci@75
   147
      const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
marci@75
   148
      //OldSymEdgeIt sym;
marci@75
   149
      OldOutEdgeIt out;
marci@75
   150
      OldInEdgeIt in;
marci@148
   151
      bool out_or_in; //true, iff out
marci@75
   152
    public:
marci@148
   153
      EdgeIt() : out_or_in(true) { } 
marci@75
   154
      Number free() const { 
marci@75
   155
	if (out_or_in) { 
marci@75
   156
	  return (resG->capacity.get(out)-resG->flow.get(out)); 
marci@75
   157
	} else { 
marci@75
   158
	  return (resG->flow.get(in)); 
marci@75
   159
	}
marci@75
   160
      }
marci@75
   161
      bool valid() const { 
marci@75
   162
	return out_or_in && out.valid() || in.valid(); }
marci@75
   163
      void augment(Number a) const {
marci@75
   164
	if (out_or_in) { 
marci@75
   165
	  resG->flow.set(out, resG->flow.get(out)+a);
marci@75
   166
	} else { 
marci@75
   167
	  resG->flow.set(in, resG->flow.get(in)-a);
marci@75
   168
	}
marci@75
   169
      }
marci@75
   170
    };
marci@75
   171
marci@75
   172
    class OutEdgeIt : public EdgeIt {
marci@75
   173
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@75
   174
    public:
marci@75
   175
      OutEdgeIt() { }
marci@75
   176
    private:
marci@75
   177
      OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) { 
marci@75
   178
      	resG=&_resG;
marci@75
   179
	out=resG->G.template first<OldOutEdgeIt>(v);
marci@75
   180
	while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   181
	if (!out.valid()) {
marci@75
   182
	  out_or_in=0;
marci@75
   183
	  in=resG->G.template first<OldInEdgeIt>(v);
marci@75
   184
	  while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   185
	}
marci@75
   186
      }
marci@75
   187
    public:
marci@75
   188
      OutEdgeIt& operator++() { 
marci@75
   189
	if (out_or_in) {
marci@75
   190
	  NodeIt v=resG->G.aNode(out);
marci@75
   191
	  ++out;
marci@75
   192
	  while( out.valid() && !(free()>0) ) { ++out; }
marci@75
   193
	  if (!out.valid()) {
marci@75
   194
	    out_or_in=0;
marci@75
   195
	    in=resG->G.template first<OldInEdgeIt>(v);
marci@75
   196
	    while( in.valid() && !(free()>0) ) { ++in; }
marci@75
   197
	  }
marci@75
   198
	} else {
marci@75
   199
	  ++in;
marci@75
   200
	  while( in.valid() && !(free()>0) ) { ++in; } 
marci@75
   201
	}
marci@75
   202
	return *this; 
marci@75
   203
      }
marci@75
   204
    };
marci@75
   205
marci@75
   206
    void getFirst(OutEdgeIt& e, NodeIt v) const { 
marci@75
   207
      e=OutEdgeIt(*this, v); 
marci@75
   208
    }
marci@75
   209
    void getFirst(EachNodeIt& v) const { G.getFirst(v); }
marci@75
   210
    
marci@75
   211
    template< typename It >
marci@75
   212
    It first() const { 
marci@43
   213
      It e;
marci@43
   214
      getFirst(e);
marci@43
   215
      return e; 
marci@43
   216
    }
marci@43
   217
marci@43
   218
    template< typename It >
marci@64
   219
    It first(NodeIt v) const { 
marci@43
   220
      It e;
marci@43
   221
      getFirst(e, v);
marci@43
   222
      return e; 
marci@43
   223
    }
marci@43
   224
marci@75
   225
    NodeIt tail(EdgeIt e) const { 
marci@75
   226
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   227
    NodeIt head(EdgeIt e) const { 
marci@75
   228
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@43
   229
marci@75
   230
    NodeIt aNode(OutEdgeIt e) const { 
marci@75
   231
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@75
   232
    NodeIt bNode(OutEdgeIt e) const { 
marci@75
   233
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@43
   234
marci@64
   235
    int id(NodeIt v) const { return G.id(v); }
marci@43
   236
marci@69
   237
    template <typename S>
marci@43
   238
    class NodeMap {
marci@69
   239
      typename Graph::NodeMap<S> node_map; 
marci@43
   240
    public:
marci@75
   241
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@75
   242
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@75
   243
      void set(NodeIt nit, S a) { node_map.set(nit, a); }
marci@75
   244
      S get(NodeIt nit) const { return node_map.get(nit); }
marci@75
   245
    };
marci@75
   246
  };
marci@75
   247
marci@43
   248
marci@75
   249
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@59
   250
  class MaxFlow {
klao@127
   251
  public:
marci@43
   252
    typedef typename Graph::NodeIt NodeIt;
marci@43
   253
    typedef typename Graph::EdgeIt EdgeIt;
marci@43
   254
    typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@43
   255
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@43
   256
    typedef typename Graph::InEdgeIt InEdgeIt;
klao@127
   257
klao@127
   258
  private:
marci@148
   259
    const Graph* G;
marci@64
   260
    NodeIt s;
marci@64
   261
    NodeIt t;
marci@148
   262
    FlowMap* flow;
marci@148
   263
    const CapacityMap* capacity;
marci@148
   264
    typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@59
   265
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@59
   266
    typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@75
   267
marci@75
   268
    //AugGraph res_graph;    
marci@75
   269
    //typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@75
   270
    //typename AugGraph::NodeMap<AugEdgeIt> pred; 
marci@133
   271
    //typename AugGraph::NodeMap<Number> free;
marci@59
   272
  public:
marci@75
   273
    MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@148
   274
      G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,  
marci@75
   275
      //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) 
marci@75
   276
      { }
marci@133
   277
    bool augmentOnShortestPath() {
marci@148
   278
      AugGraph res_graph(*G, *flow, *capacity);
marci@59
   279
      bool _augment=false;
marci@59
   280
      
marci@59
   281
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@148
   282
      BfsIterator5< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@59
   283
      res_bfs.pushAndSetReached(s);
marci@59
   284
	
marci@59
   285
      typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@75
   286
      //filled up with invalid iterators
marci@75
   287
      //pred.set(s, AugEdgeIt());
marci@59
   288
      
marci@133
   289
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@59
   290
	
marci@59
   291
      //searching for augmenting path
marci@59
   292
      while ( !res_bfs.finished() ) { 
marci@141
   293
	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@148
   294
	if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
marci@59
   295
	  NodeIt v=res_graph.tail(e);
marci@59
   296
	  NodeIt w=res_graph.head(e);
marci@59
   297
	  pred.set(w, e);
marci@148
   298
	  if (res_graph.valid(pred.get(v))) {
marci@59
   299
	    free.set(w, std::min(free.get(v), e.free()));
marci@59
   300
	  } else {
marci@59
   301
	    free.set(w, e.free()); 
marci@59
   302
	  }
marci@59
   303
	  if (res_graph.head(e)==t) { _augment=true; break; }
marci@59
   304
	}
marci@59
   305
	
marci@59
   306
	++res_bfs;
marci@59
   307
      } //end of searching augmenting path
marci@43
   308
marci@59
   309
      if (_augment) {
marci@59
   310
	NodeIt n=t;
marci@75
   311
	Number augment_value=free.get(t);
marci@148
   312
	while (res_graph.valid(pred.get(n))) { 
marci@59
   313
	  AugEdgeIt e=pred.get(n);
marci@59
   314
	  e.augment(augment_value); 
marci@59
   315
	  n=res_graph.tail(e);
marci@59
   316
	}
marci@59
   317
      }
marci@59
   318
marci@59
   319
      return _augment;
marci@59
   320
    }
marci@141
   321
marci@133
   322
    template<typename MutableGraph> bool augmentOnBlockingFlow() {
marci@133
   323
      bool _augment=false;
marci@133
   324
marci@148
   325
      AugGraph res_graph(*G, *flow, *capacity);
marci@133
   326
marci@133
   327
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@133
   328
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
marci@133
   329
marci@100
   330
      bfs.pushAndSetReached(s);
marci@133
   331
      typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@100
   332
      while ( !bfs.finished() ) { 
marci@141
   333
	AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
marci@148
   334
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@133
   335
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@100
   336
	}
marci@100
   337
	
marci@100
   338
	++bfs;
marci@133
   339
      } //computing distances from s in the residual graph
marci@100
   340
marci@100
   341
      MutableGraph F;
marci@133
   342
      typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
marci@148
   343
      for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@133
   344
	res_graph_to_F.set(n, F.addNode());
marci@100
   345
      }
marci@133
   346
      
marci@133
   347
      typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
marci@133
   348
      typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
marci@133
   349
marci@133
   350
      typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
marci@148
   351
      typename MutableGraph::EdgeMap<Number> residual_capacity(F);
marci@133
   352
marci@133
   353
      //Making F to the graph containing the edges of the residual graph 
marci@133
   354
      //which are in some shortest paths
marci@148
   355
      for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
marci@133
   356
	if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
marci@133
   357
	  typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
marci@133
   358
	  original_edge.update();
marci@133
   359
	  original_edge.set(f, e);
marci@148
   360
	  residual_capacity.update();
marci@148
   361
	  residual_capacity.set(f, e.free());
marci@133
   362
	} 
marci@133
   363
      }
marci@133
   364
marci@133
   365
      bool __augment=true;
marci@133
   366
marci@133
   367
      while (__augment) {
marci@133
   368
	__augment=false;
marci@133
   369
	//computing blocking flow with dfs
marci@133
   370
	typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
marci@133
   371
	DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
marci@133
   372
	typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
marci@133
   373
	typename MutableGraph::NodeMap<Number> free(F);
marci@133
   374
marci@133
   375
	dfs.pushAndSetReached(sF);      
marci@133
   376
	while (!dfs.finished()) {
marci@133
   377
	  ++dfs;
marci@148
   378
	  if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
marci@133
   379
	    //std::cout << "OutEdgeIt: " << dfs; 
marci@133
   380
	    //std::cout << " aNode: " << F.aNode(dfs); 
marci@133
   381
	    //std::cout << " bNode: " << F.bNode(dfs) << " ";
marci@133
   382
	  
marci@133
   383
	    typename MutableGraph::NodeIt v=F.aNode(dfs);
marci@133
   384
	    typename MutableGraph::NodeIt w=F.bNode(dfs);
marci@133
   385
	    pred.set(w, dfs);
marci@148
   386
	    if (F.valid(pred.get(v))) {
marci@148
   387
	      free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
marci@133
   388
	    } else {
marci@148
   389
	      free.set(w, residual_capacity.get(dfs)/*original_edge.get(dfs).free()*/); 
marci@133
   390
	    }
marci@133
   391
	    if (w==tF) { 
marci@133
   392
	      //std::cout << "AUGMENTATION"<<std::endl;
marci@133
   393
	      __augment=true; 
marci@133
   394
	      _augment=true;
marci@133
   395
	      break; 
marci@133
   396
	    }
marci@133
   397
	  } else { 
marci@133
   398
	    //std::cout << "OutEdgeIt: " << "invalid"; 
marci@133
   399
	    //std::cout << " aNode: " << dfs.aNode(); 
marci@133
   400
	    //std::cout << " bNode: " << "invalid" << " ";
marci@133
   401
	  }
marci@133
   402
	  if (dfs.isBNodeNewlyReached()) { 
marci@133
   403
	    //std::cout << "bNodeIsNewlyReached ";
marci@133
   404
	  } else { 
marci@133
   405
	    //std::cout << "bNodeIsNotNewlyReached ";
marci@133
   406
	    if (typename MutableGraph::OutEdgeIt(dfs).valid()) {
marci@133
   407
	      //std::cout << "DELETE ";
marci@133
   408
	      F.erase(typename MutableGraph::OutEdgeIt(dfs));
marci@133
   409
	    }
marci@133
   410
	  } 
marci@133
   411
	  //if (dfs.isANodeExamined()) { 
marci@133
   412
	    //std::cout << "aNodeIsExamined ";
marci@133
   413
	    //} else { 
marci@133
   414
	    //std::cout << "aNodeIsNotExamined ";
marci@133
   415
	    //} 
marci@133
   416
	  //std::cout<<std::endl;
marci@100
   417
	}
marci@133
   418
marci@133
   419
	if (__augment) {
marci@133
   420
	  typename MutableGraph::NodeIt n=tF;
marci@133
   421
	  Number augment_value=free.get(tF);
marci@148
   422
	  while (F.valid(pred.get(n))) { 
marci@133
   423
	    typename MutableGraph::EdgeIt e=pred.get(n);
marci@133
   424
	    original_edge.get(e).augment(augment_value); 
marci@133
   425
	    n=F.tail(e);
marci@148
   426
	    if (residual_capacity.get(e)==augment_value) 
marci@135
   427
	      F.erase(e); 
marci@135
   428
	    else 
marci@148
   429
	      residual_capacity.set(e, residual_capacity.get(e)-augment_value);
marci@133
   430
	  }
marci@133
   431
	}
marci@135
   432
      
marci@100
   433
      }
marci@133
   434
            
marci@133
   435
      return _augment;
marci@100
   436
    }
marci@43
   437
    void run() {
marci@100
   438
      //int num_of_augmentations=0;
marci@133
   439
      while (augmentOnShortestPath()) { 
marci@133
   440
	//while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@133
   441
	//std::cout << ++num_of_augmentations << " ";
marci@133
   442
	//std::cout<<std::endl;
marci@133
   443
      } 
marci@133
   444
    }
marci@133
   445
    template<typename MutableGraph> void run() {
marci@133
   446
      //int num_of_augmentations=0;
marci@133
   447
      //while (augmentOnShortestPath()) { 
marci@133
   448
	while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@100
   449
	//std::cout << ++num_of_augmentations << " ";
marci@100
   450
	//std::cout<<std::endl;
marci@100
   451
      } 
marci@43
   452
    }
marci@75
   453
    Number flowValue() { 
marci@75
   454
      Number a=0;
marci@148
   455
      OutEdgeIt e;
marci@148
   456
      for(G->getFirst(e, s); G->valid(e); G->next(e)) {
marci@148
   457
	a+=flow->get(e);
marci@69
   458
      }
marci@69
   459
      return a;
marci@69
   460
    }
marci@43
   461
  };
marci@43
   462
marci@75
   463
  
marci@155
   464
//   template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@155
   465
//   class MaxFlow2 {
marci@155
   466
//   public:
marci@155
   467
//     typedef typename Graph::NodeIt NodeIt;
marci@155
   468
//     typedef typename Graph::EdgeIt EdgeIt;
marci@155
   469
//     typedef typename Graph::EachEdgeIt EachEdgeIt;
marci@155
   470
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@155
   471
//     typedef typename Graph::InEdgeIt InEdgeIt;
marci@155
   472
//   private:
marci@155
   473
//     const Graph& G;
marci@155
   474
//     std::list<NodeIt>& S;
marci@155
   475
//     std::list<NodeIt>& T;
marci@155
   476
//     FlowMap& flow;
marci@155
   477
//     const CapacityMap& capacity;
marci@155
   478
//     typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@155
   479
//     typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@155
   480
//     typedef typename AugGraph::EdgeIt AugEdgeIt;
marci@155
   481
//     typename Graph::NodeMap<bool> SMap;
marci@155
   482
//     typename Graph::NodeMap<bool> TMap;
marci@155
   483
//   public:
marci@155
   484
//     MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { 
marci@155
   485
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   486
// 	  i!=S.end(); ++i) { 
marci@155
   487
// 	SMap.set(*i, true); 
marci@155
   488
//       }
marci@155
   489
//       for (typename std::list<NodeIt>::const_iterator i=T.begin(); 
marci@155
   490
// 	   i!=T.end(); ++i) { 
marci@155
   491
// 	TMap.set(*i, true); 
marci@155
   492
//       }
marci@155
   493
//     }
marci@155
   494
//     bool augment() {
marci@155
   495
//       AugGraph res_graph(G, flow, capacity);
marci@155
   496
//       bool _augment=false;
marci@155
   497
//       NodeIt reached_t_node;
marci@75
   498
      
marci@155
   499
//       typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@155
   500
//       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@155
   501
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   502
// 	  i!=S.end(); ++i) {
marci@155
   503
// 	res_bfs.pushAndSetReached(*i);
marci@155
   504
//       }
marci@155
   505
//       //res_bfs.pushAndSetReached(s);
marci@75
   506
	
marci@155
   507
//       typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph); 
marci@155
   508
//       //filled up with invalid iterators
marci@75
   509
      
marci@155
   510
//       typename AugGraph::NodeMap<Number> free(res_graph);
marci@75
   511
	
marci@155
   512
//       //searching for augmenting path
marci@155
   513
//       while ( !res_bfs.finished() ) { 
marci@155
   514
// 	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@155
   515
// 	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@155
   516
// 	  NodeIt v=res_graph.tail(e);
marci@155
   517
// 	  NodeIt w=res_graph.head(e);
marci@155
   518
// 	  pred.set(w, e);
marci@155
   519
// 	  if (pred.get(v).valid()) {
marci@155
   520
// 	    free.set(w, std::min(free.get(v), e.free()));
marci@155
   521
// 	  } else {
marci@155
   522
// 	    free.set(w, e.free()); 
marci@155
   523
// 	  }
marci@155
   524
// 	  if (TMap.get(res_graph.head(e))) { 
marci@155
   525
// 	    _augment=true; 
marci@155
   526
// 	    reached_t_node=res_graph.head(e);
marci@155
   527
// 	    break; 
marci@155
   528
// 	  }
marci@155
   529
// 	}
marci@75
   530
	
marci@155
   531
// 	++res_bfs;
marci@155
   532
//       } //end of searching augmenting path
marci@75
   533
marci@155
   534
//       if (_augment) {
marci@155
   535
// 	NodeIt n=reached_t_node;
marci@155
   536
// 	Number augment_value=free.get(reached_t_node);
marci@155
   537
// 	while (pred.get(n).valid()) { 
marci@155
   538
// 	  AugEdgeIt e=pred.get(n);
marci@155
   539
// 	  e.augment(augment_value); 
marci@155
   540
// 	  n=res_graph.tail(e);
marci@155
   541
// 	}
marci@155
   542
//       }
marci@75
   543
marci@155
   544
//       return _augment;
marci@155
   545
//     }
marci@155
   546
//     void run() {
marci@155
   547
//       while (augment()) { } 
marci@155
   548
//     }
marci@155
   549
//     Number flowValue() { 
marci@155
   550
//       Number a=0;
marci@155
   551
//       for(typename std::list<NodeIt>::const_iterator i=S.begin(); 
marci@155
   552
// 	  i!=S.end(); ++i) { 
marci@155
   553
// 	for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
marci@155
   554
// 	  a+=flow.get(e);
marci@155
   555
// 	}
marci@155
   556
// 	for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
marci@155
   557
// 	  a-=flow.get(e);
marci@155
   558
// 	}
marci@155
   559
//       }
marci@155
   560
//       return a;
marci@155
   561
//     }
marci@155
   562
//   };
marci@75
   563
marci@75
   564
marci@75
   565
alpar@107
   566
} // namespace hugo
marci@43
   567
marci@59
   568
#endif //EDMONDS_KARP_HH