src/work/edmonds_karp.h
author alpar
Fri, 19 Mar 2004 07:39:09 +0000
changeset 202 0bd4fe53b1d0
parent 197 fff43d9c7110
child 206 47f62d789fe7
permissions -rw-r--r--
Documentation page for maps.
marci@174
     1
// -*- c++ -*-
marci@174
     2
#ifndef EDMONDS_KARP_H
marci@174
     3
#define EDMONDS_KARP_H
marci@174
     4
marci@174
     5
#include <algorithm>
marci@174
     6
#include <list>
marci@174
     7
#include <iterator>
marci@174
     8
marci@174
     9
#include <bfs_iterator.h>
marci@174
    10
#include <invalid.h>
marci@174
    11
marci@174
    12
namespace hugo {
marci@174
    13
marci@174
    14
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@174
    15
  class ResGraph {
marci@174
    16
  public:
marci@174
    17
    typedef typename Graph::Node Node;
marci@174
    18
    typedef typename Graph::NodeIt NodeIt;
marci@174
    19
  private:
marci@174
    20
    typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@174
    21
    const Graph& G;
marci@174
    22
    FlowMap& flow;
marci@174
    23
    const CapacityMap& capacity;
marci@174
    24
  public:
marci@174
    25
    ResGraph(const Graph& _G, FlowMap& _flow, 
marci@174
    26
	     const CapacityMap& _capacity) : 
marci@174
    27
      G(_G), flow(_flow), capacity(_capacity) { }
marci@174
    28
marci@174
    29
    class Edge; 
marci@174
    30
    class OutEdgeIt; 
marci@174
    31
    friend class Edge; 
marci@174
    32
    friend class OutEdgeIt; 
marci@174
    33
marci@174
    34
    class Edge {
marci@174
    35
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@174
    36
    protected:
marci@174
    37
      const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
marci@174
    38
      OldSymEdgeIt sym;
marci@174
    39
    public:
marci@174
    40
      Edge() { } 
marci@174
    41
      //Edge(const Edge& e) : resG(e.resG), sym(e.sym) { }
marci@174
    42
      Number free() const { 
marci@174
    43
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@174
    44
	  return (resG->capacity.get(sym)-resG->flow.get(sym)); 
marci@174
    45
	} else { 
marci@174
    46
	  return (resG->flow.get(sym)); 
marci@174
    47
	}
marci@174
    48
      }
marci@174
    49
      bool valid() const { return sym.valid(); }
marci@174
    50
      void augment(Number a) const {
marci@174
    51
	if (resG->G.aNode(sym)==resG->G.tail(sym)) { 
marci@174
    52
	  resG->flow.set(sym, resG->flow.get(sym)+a);
marci@174
    53
	  //resG->flow[sym]+=a;
marci@174
    54
	} else { 
marci@174
    55
	  resG->flow.set(sym, resG->flow.get(sym)-a);
marci@174
    56
	  //resG->flow[sym]-=a;
marci@174
    57
	}
marci@174
    58
      }
marci@174
    59
    };
marci@174
    60
marci@174
    61
    class OutEdgeIt : public Edge {
marci@174
    62
      friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
marci@174
    63
    public:
marci@174
    64
      OutEdgeIt() { }
marci@174
    65
      //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
marci@174
    66
    private:
marci@174
    67
      OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) { 
marci@174
    68
      	resG=&_resG;
marci@174
    69
	sym=resG->G.template first<OldSymEdgeIt>(v);
marci@174
    70
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@174
    71
      }
marci@174
    72
    public:
marci@174
    73
      OutEdgeIt& operator++() { 
marci@174
    74
	++sym; 
marci@174
    75
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@174
    76
	return *this; 
marci@174
    77
      }
marci@174
    78
    };
marci@174
    79
marci@174
    80
    void /*getF*/first(OutEdgeIt& e, Node v) const { 
marci@174
    81
      e=OutEdgeIt(*this, v); 
marci@174
    82
    }
marci@174
    83
    void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
marci@174
    84
    
marci@174
    85
    template< typename It >
marci@174
    86
    It first() const { 
marci@174
    87
      It e;      
marci@174
    88
      /*getF*/first(e);
marci@174
    89
      return e; 
marci@174
    90
    }
marci@174
    91
marci@174
    92
    template< typename It >
marci@174
    93
    It first(Node v) const { 
marci@174
    94
      It e;
marci@174
    95
      /*getF*/first(e, v);
marci@174
    96
      return e; 
marci@174
    97
    }
marci@174
    98
marci@174
    99
    Node tail(Edge e) const { return G.aNode(e.sym); }
marci@174
   100
    Node head(Edge e) const { return G.bNode(e.sym); }
marci@174
   101
marci@174
   102
    Node aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
marci@174
   103
    Node bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
marci@174
   104
marci@174
   105
    int id(Node v) const { return G.id(v); }
marci@174
   106
marci@174
   107
    template <typename S>
marci@174
   108
    class NodeMap {
marci@174
   109
      typename Graph::NodeMap<S> node_map; 
marci@174
   110
    public:
marci@174
   111
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@174
   112
      NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@174
   113
      void set(Node nit, S a) { node_map.set(nit, a); }
marci@174
   114
      S get(Node nit) const { return node_map.get(nit); }
marci@174
   115
      S& operator[](Node nit) { return node_map[nit]; } 
marci@174
   116
      const S& operator[](Node nit) const { return node_map[nit]; } 
marci@174
   117
    };
marci@174
   118
marci@174
   119
  };
marci@174
   120
marci@174
   121
marci@174
   122
  template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@174
   123
  class ResGraph2 {
marci@174
   124
  public:
marci@174
   125
    typedef typename Graph::Node Node;
marci@174
   126
    typedef typename Graph::NodeIt NodeIt;
marci@174
   127
  private:
marci@174
   128
    //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
marci@174
   129
    typedef typename Graph::OutEdgeIt OldOutEdgeIt;
marci@174
   130
    typedef typename Graph::InEdgeIt OldInEdgeIt;
marci@174
   131
    
marci@174
   132
    const Graph& G;
marci@174
   133
    FlowMap& flow;
marci@174
   134
    const CapacityMap& capacity;
marci@174
   135
  public:
marci@174
   136
    ResGraph2(const Graph& _G, FlowMap& _flow, 
marci@174
   137
	     const CapacityMap& _capacity) : 
marci@174
   138
      G(_G), flow(_flow), capacity(_capacity) { }
marci@174
   139
marci@174
   140
    class Edge; 
marci@174
   141
    class OutEdgeIt; 
marci@174
   142
    friend class Edge; 
marci@174
   143
    friend class OutEdgeIt; 
marci@174
   144
marci@174
   145
    class Edge {
marci@174
   146
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@174
   147
    protected:
marci@174
   148
      const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
marci@174
   149
      //OldSymEdgeIt sym;
marci@174
   150
      OldOutEdgeIt out;
marci@174
   151
      OldInEdgeIt in;
marci@174
   152
      bool out_or_in; //true, iff out
marci@174
   153
    public:
marci@174
   154
      Edge() : out_or_in(true) { } 
marci@174
   155
      Number free() const { 
marci@174
   156
	if (out_or_in) { 
marci@174
   157
	  return (resG->capacity.get(out)-resG->flow.get(out)); 
marci@174
   158
	} else { 
marci@174
   159
	  return (resG->flow.get(in)); 
marci@174
   160
	}
marci@174
   161
      }
marci@174
   162
      bool valid() const { 
marci@174
   163
	return out_or_in && out.valid() || in.valid(); }
marci@174
   164
      void augment(Number a) const {
marci@174
   165
	if (out_or_in) { 
marci@174
   166
	  resG->flow.set(out, resG->flow.get(out)+a);
marci@174
   167
	} else { 
marci@174
   168
	  resG->flow.set(in, resG->flow.get(in)-a);
marci@174
   169
	}
marci@174
   170
      }
marci@174
   171
    };
marci@174
   172
marci@174
   173
    class OutEdgeIt : public Edge {
marci@174
   174
      friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
marci@174
   175
    public:
marci@174
   176
      OutEdgeIt() { }
marci@174
   177
    private:
marci@174
   178
      OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, Node v) { 
marci@174
   179
      	resG=&_resG;
marci@174
   180
	out=resG->G.template first<OldOutEdgeIt>(v);
marci@174
   181
	while( out.valid() && !(free()>0) ) { ++out; }
marci@174
   182
	if (!out.valid()) {
marci@174
   183
	  out_or_in=0;
marci@174
   184
	  in=resG->G.template first<OldInEdgeIt>(v);
marci@174
   185
	  while( in.valid() && !(free()>0) ) { ++in; }
marci@174
   186
	}
marci@174
   187
      }
marci@174
   188
    public:
marci@174
   189
      OutEdgeIt& operator++() { 
marci@174
   190
	if (out_or_in) {
marci@174
   191
	  Node v=resG->G.aNode(out);
marci@174
   192
	  ++out;
marci@174
   193
	  while( out.valid() && !(free()>0) ) { ++out; }
marci@174
   194
	  if (!out.valid()) {
marci@174
   195
	    out_or_in=0;
marci@174
   196
	    in=resG->G.template first<OldInEdgeIt>(v);
marci@174
   197
	    while( in.valid() && !(free()>0) ) { ++in; }
marci@174
   198
	  }
marci@174
   199
	} else {
marci@174
   200
	  ++in;
marci@174
   201
	  while( in.valid() && !(free()>0) ) { ++in; } 
marci@174
   202
	}
marci@174
   203
	return *this; 
marci@174
   204
      }
marci@174
   205
    };
marci@174
   206
marci@174
   207
    void /*getF*/first(OutEdgeIt& e, Node v) const { 
marci@174
   208
      e=OutEdgeIt(*this, v); 
marci@174
   209
    }
marci@174
   210
    void /*getF*/first(NodeIt& v) const { G./*getF*/first(v); }
marci@174
   211
    
marci@174
   212
    template< typename It >
marci@174
   213
    It first() const { 
marci@174
   214
      It e;
marci@174
   215
      /*getF*/first(e);
marci@174
   216
      return e; 
marci@174
   217
    }
marci@174
   218
marci@174
   219
    template< typename It >
marci@174
   220
    It first(Node v) const { 
marci@174
   221
      It e;
marci@174
   222
      /*getF*/first(e, v);
marci@174
   223
      return e; 
marci@174
   224
    }
marci@174
   225
marci@174
   226
    Node tail(Edge e) const { 
marci@174
   227
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@174
   228
    Node head(Edge e) const { 
marci@174
   229
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@174
   230
marci@174
   231
    Node aNode(OutEdgeIt e) const { 
marci@174
   232
      return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
marci@174
   233
    Node bNode(OutEdgeIt e) const { 
marci@174
   234
      return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
marci@174
   235
marci@174
   236
    int id(Node v) const { return G.id(v); }
marci@174
   237
marci@174
   238
    template <typename S>
marci@174
   239
    class NodeMap {
marci@174
   240
      typename Graph::NodeMap<S> node_map; 
marci@174
   241
    public:
marci@174
   242
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
marci@174
   243
      NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
marci@174
   244
      void set(Node nit, S a) { node_map.set(nit, a); }
marci@174
   245
      S get(Node nit) const { return node_map.get(nit); }
marci@174
   246
    };
marci@174
   247
  };
marci@174
   248
marci@174
   249
marci@174
   250
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@174
   251
  class MaxFlow {
marci@174
   252
  public:
marci@174
   253
    typedef typename Graph::Node Node;
marci@174
   254
    typedef typename Graph::Edge Edge;
marci@174
   255
    typedef typename Graph::EdgeIt EdgeIt;
marci@174
   256
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
   257
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@174
   258
marci@174
   259
  private:
marci@174
   260
    const Graph* G;
marci@174
   261
    Node s;
marci@174
   262
    Node t;
marci@174
   263
    FlowMap* flow;
marci@174
   264
    const CapacityMap* capacity;
marci@174
   265
    typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@174
   266
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@174
   267
    typedef typename AugGraph::Edge AugEdge;
marci@174
   268
marci@174
   269
  public:
marci@174
   270
    MaxFlow(const Graph& _G, Node _s, Node _t, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@191
   271
      G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) { }
marci@174
   272
    bool augmentOnShortestPath() {
marci@174
   273
      AugGraph res_graph(*G, *flow, *capacity);
marci@174
   274
      bool _augment=false;
marci@174
   275
      
marci@174
   276
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@174
   277
      BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
marci@174
   278
      res_bfs.pushAndSetReached(s);
marci@174
   279
	
marci@174
   280
      typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@174
   281
      pred.set(s, AugEdge(INVALID));
marci@174
   282
      
marci@174
   283
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@174
   284
	
marci@174
   285
      //searching for augmenting path
marci@174
   286
      while ( !res_bfs.finished() ) { 
marci@191
   287
	AugOutEdgeIt e=res_bfs;
marci@174
   288
	if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
marci@174
   289
	  Node v=res_graph.tail(e);
marci@174
   290
	  Node w=res_graph.head(e);
marci@174
   291
	  pred.set(w, e);
marci@174
   292
	  if (res_graph.valid(pred.get(v))) {
marci@174
   293
	    free.set(w, std::min(free.get(v), res_graph.free(e)));
marci@174
   294
	  } else {
marci@174
   295
	    free.set(w, res_graph.free(e)); 
marci@174
   296
	  }
marci@174
   297
	  if (res_graph.head(e)==t) { _augment=true; break; }
marci@174
   298
	}
marci@174
   299
	
marci@174
   300
	++res_bfs;
marci@174
   301
      } //end of searching augmenting path
marci@174
   302
marci@174
   303
      if (_augment) {
marci@174
   304
	Node n=t;
marci@174
   305
	Number augment_value=free.get(t);
marci@174
   306
	while (res_graph.valid(pred.get(n))) { 
marci@174
   307
	  AugEdge e=pred.get(n);
marci@174
   308
	  res_graph.augment(e, augment_value); 
marci@174
   309
	  n=res_graph.tail(e);
marci@174
   310
	}
marci@174
   311
      }
marci@174
   312
marci@174
   313
      return _augment;
marci@174
   314
    }
marci@174
   315
marci@191
   316
    template<typename MutableGraph> bool augmentOnBlockingFlow() {      
marci@174
   317
      bool _augment=false;
marci@174
   318
marci@174
   319
      AugGraph res_graph(*G, *flow, *capacity);
marci@174
   320
marci@174
   321
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@174
   322
      BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
marci@174
   323
marci@174
   324
      bfs.pushAndSetReached(s);
marci@174
   325
      typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@174
   326
      while ( !bfs.finished() ) { 
marci@191
   327
	AugOutEdgeIt e=bfs;
marci@174
   328
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@174
   329
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@174
   330
	}
marci@174
   331
	
marci@174
   332
	++bfs;
marci@174
   333
      } //computing distances from s in the residual graph
marci@174
   334
marci@174
   335
      MutableGraph F;
marci@174
   336
      typename AugGraph::NodeMap<typename MutableGraph::Node> 
marci@174
   337
	res_graph_to_F(res_graph);
marci@174
   338
      for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@174
   339
	res_graph_to_F.set(n, F.addNode());
marci@174
   340
      }
marci@174
   341
      
marci@174
   342
      typename MutableGraph::Node sF=res_graph_to_F.get(s);
marci@174
   343
      typename MutableGraph::Node tF=res_graph_to_F.get(t);
marci@174
   344
marci@174
   345
      typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
marci@174
   346
      typename MutableGraph::EdgeMap<Number> residual_capacity(F);
marci@174
   347
marci@174
   348
      //Making F to the graph containing the edges of the residual graph 
marci@174
   349
      //which are in some shortest paths
marci@174
   350
      for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
marci@174
   351
	if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
marci@174
   352
	  typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
marci@174
   353
	  original_edge.update();
marci@174
   354
	  original_edge.set(f, e);
marci@174
   355
	  residual_capacity.update();
marci@174
   356
	  residual_capacity.set(f, res_graph.free(e));
marci@174
   357
	} 
marci@174
   358
      }
marci@174
   359
marci@174
   360
      bool __augment=true;
marci@174
   361
marci@174
   362
      while (__augment) {
marci@174
   363
	__augment=false;
marci@174
   364
	//computing blocking flow with dfs
marci@174
   365
	typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
marci@174
   366
	DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
marci@174
   367
	typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
marci@174
   368
	pred.set(sF, typename MutableGraph::Edge(INVALID));
marci@174
   369
	//invalid iterators for sources
marci@174
   370
marci@174
   371
	typename MutableGraph::NodeMap<Number> free(F);
marci@174
   372
marci@174
   373
	dfs.pushAndSetReached(sF);      
marci@174
   374
	while (!dfs.finished()) {
marci@174
   375
	  ++dfs;
marci@174
   376
	  if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
marci@174
   377
	    if (dfs.isBNodeNewlyReached()) {
marci@174
   378
	      typename MutableGraph::Node v=F.aNode(dfs);
marci@174
   379
	      typename MutableGraph::Node w=F.bNode(dfs);
marci@174
   380
	      pred.set(w, dfs);
marci@174
   381
	      if (F.valid(pred.get(v))) {
marci@174
   382
		free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
marci@174
   383
	      } else {
marci@174
   384
		free.set(w, residual_capacity.get(dfs)); 
marci@174
   385
	      }
marci@174
   386
	      if (w==tF) { 
marci@174
   387
		__augment=true; 
marci@174
   388
		_augment=true;
marci@174
   389
		break; 
marci@174
   390
	      }
marci@174
   391
	      
marci@174
   392
	    } else {
marci@174
   393
	      F.erase(typename MutableGraph::OutEdgeIt(dfs));
marci@174
   394
	    }
marci@174
   395
	  } 
marci@174
   396
	}
marci@174
   397
marci@174
   398
	if (__augment) {
marci@174
   399
	  typename MutableGraph::Node n=tF;
marci@174
   400
	  Number augment_value=free.get(tF);
marci@174
   401
	  while (F.valid(pred.get(n))) { 
marci@174
   402
	    typename MutableGraph::Edge e=pred.get(n);
marci@174
   403
	    res_graph.augment(original_edge.get(e), augment_value); 
marci@174
   404
	    n=F.tail(e);
marci@174
   405
	    if (residual_capacity.get(e)==augment_value) 
marci@174
   406
	      F.erase(e); 
marci@174
   407
	    else 
marci@174
   408
	      residual_capacity.set(e, residual_capacity.get(e)-augment_value);
marci@174
   409
	  }
marci@174
   410
	}
marci@174
   411
	
marci@174
   412
      }
marci@174
   413
            
marci@174
   414
      return _augment;
marci@174
   415
    }
marci@174
   416
    bool augmentOnBlockingFlow2() {
marci@174
   417
      bool _augment=false;
marci@174
   418
marci@174
   419
      //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
marci@174
   420
      typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
marci@174
   421
      typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
marci@174
   422
      typedef typename EAugGraph::Edge EAugEdge;
marci@174
   423
marci@174
   424
      EAugGraph res_graph(*G, *flow, *capacity);
marci@174
   425
marci@174
   426
      //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
marci@174
   427
      BfsIterator4< 
marci@174
   428
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>, 
marci@175
   429
	typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt, 
marci@174
   430
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
marci@174
   431
      
marci@191
   432
      bfs.pushAndSetReached(s);
marci@174
   433
marci@174
   434
      typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
marci@174
   435
	NodeMap<int>& dist=res_graph.dist;
marci@174
   436
marci@174
   437
      while ( !bfs.finished() ) {
marci@175
   438
	typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
marci@174
   439
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@174
   440
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@174
   441
	}
marci@174
   442
	++bfs;	
marci@174
   443
      } //computing distances from s in the residual graph
marci@174
   444
marci@174
   445
      bool __augment=true;
marci@174
   446
marci@174
   447
      while (__augment) {
marci@174
   448
marci@174
   449
	__augment=false;
marci@174
   450
	//computing blocking flow with dfs
marci@174
   451
	typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
marci@174
   452
	DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > 
marci@174
   453
	  dfs(res_graph);
marci@174
   454
	typename EAugGraph::NodeMap<EAugEdge> pred(res_graph); 
marci@174
   455
	pred.set(s, EAugEdge(INVALID));
marci@174
   456
	//invalid iterators for sources
marci@174
   457
marci@174
   458
	typename EAugGraph::NodeMap<Number> free(res_graph);
marci@174
   459
marci@174
   460
	dfs.pushAndSetReached(s);
marci@174
   461
	while (!dfs.finished()) {
marci@174
   462
	  ++dfs;
marci@174
   463
	  if (res_graph.valid(EAugOutEdgeIt(dfs))) { 
marci@174
   464
	    if (dfs.isBNodeNewlyReached()) {
marci@174
   465
	  
marci@174
   466
	      typename EAugGraph::Node v=res_graph.aNode(dfs);
marci@174
   467
	      typename EAugGraph::Node w=res_graph.bNode(dfs);
marci@174
   468
marci@174
   469
	      pred.set(w, EAugOutEdgeIt(dfs));
marci@174
   470
	      if (res_graph.valid(pred.get(v))) {
marci@191
   471
		free.set(w, std::min(free.get(v), res_graph.free(dfs)));
marci@174
   472
	      } else {
marci@191
   473
		free.set(w, res_graph.free(dfs)); 
marci@174
   474
	      }
marci@174
   475
	      
marci@174
   476
	      if (w==t) { 
marci@174
   477
		__augment=true; 
marci@174
   478
		_augment=true;
marci@174
   479
		break; 
marci@174
   480
	      }
marci@174
   481
	    } else {
marci@174
   482
	      res_graph.erase(dfs);
marci@174
   483
	    }
marci@174
   484
	  } 
marci@174
   485
marci@174
   486
	}
marci@174
   487
marci@174
   488
	if (__augment) {
marci@174
   489
	  typename EAugGraph::Node n=t;
marci@174
   490
	  Number augment_value=free.get(t);
marci@174
   491
	  while (res_graph.valid(pred.get(n))) { 
marci@174
   492
	    EAugEdge e=pred.get(n);
marci@174
   493
	    res_graph.augment(e, augment_value);
marci@174
   494
	    n=res_graph.tail(e);
marci@174
   495
	    if (res_graph.free(e)==0)
marci@174
   496
	      res_graph.erase(e);
marci@174
   497
	  }
marci@174
   498
	}
marci@174
   499
      
marci@174
   500
      }
marci@174
   501
            
marci@174
   502
      return _augment;
marci@174
   503
    }
marci@174
   504
    void run() {
marci@174
   505
      //int num_of_augmentations=0;
marci@174
   506
      while (augmentOnShortestPath()) { 
marci@174
   507
	//while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@174
   508
	//std::cout << ++num_of_augmentations << " ";
marci@174
   509
	//std::cout<<std::endl;
marci@174
   510
      } 
marci@174
   511
    }
marci@174
   512
    template<typename MutableGraph> void run() {
marci@174
   513
      //int num_of_augmentations=0;
marci@174
   514
      //while (augmentOnShortestPath()) { 
marci@174
   515
	while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@174
   516
	//std::cout << ++num_of_augmentations << " ";
marci@174
   517
	//std::cout<<std::endl;
marci@174
   518
      } 
marci@174
   519
    }
marci@174
   520
    Number flowValue() { 
marci@174
   521
      Number a=0;
marci@174
   522
      OutEdgeIt e;
marci@174
   523
      for(G->/*getF*/first(e, s); G->valid(e); G->next(e)) {
marci@174
   524
	a+=flow->get(e);
marci@174
   525
      }
marci@174
   526
      return a;
marci@174
   527
    }
marci@174
   528
  };
marci@174
   529
marci@193
   530
marci@193
   531
  template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@193
   532
  class MaxMatching {
marci@193
   533
  public:
marci@193
   534
    typedef typename Graph::Node Node;
marci@194
   535
    typedef typename Graph::NodeIt NodeIt;
marci@193
   536
    typedef typename Graph::Edge Edge;
marci@193
   537
    typedef typename Graph::EdgeIt EdgeIt;
marci@193
   538
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@193
   539
    typedef typename Graph::InEdgeIt InEdgeIt;
marci@193
   540
marci@193
   541
    typedef typename Graph::NodeMap<bool> SMap;
marci@193
   542
    typedef typename Graph::NodeMap<bool> TMap;
marci@193
   543
  private:
marci@193
   544
    const Graph* G;
marci@193
   545
    SMap* S;
marci@193
   546
    TMap* T;
marci@193
   547
    //Node s;
marci@193
   548
    //Node t;
marci@193
   549
    FlowMap* flow;
marci@193
   550
    const CapacityMap* capacity;
marci@193
   551
    typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@193
   552
    typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@193
   553
    typedef typename AugGraph::Edge AugEdge;
marci@198
   554
    typename Graph::NodeMap<int> used; //0
marci@193
   555
marci@193
   556
  public:
marci@193
   557
    MaxMatching(const Graph& _G, SMap& _S, TMap& _T, FlowMap& _flow, const CapacityMap& _capacity) : 
marci@198
   558
      G(&_G), S(&_S), T(&_T), flow(&_flow), capacity(&_capacity), used(_G) { }
marci@193
   559
    bool augmentOnShortestPath() {
marci@193
   560
      AugGraph res_graph(*G, *flow, *capacity);
marci@193
   561
      bool _augment=false;
marci@193
   562
      
marci@193
   563
      typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@193
   564
      BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
marci@193
   565
      typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@193
   566
      for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
marci@198
   567
	if ((S->get(s)) && (used.get(s)<1) ) {
marci@198
   568
	  //Number u=0;
marci@198
   569
	  //for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
marci@198
   570
	  //u+=flow->get(e);
marci@198
   571
	  //if (u<1) {
marci@196
   572
	    res_bfs.pushAndSetReached(s);
marci@196
   573
	    pred.set(s, AugEdge(INVALID));
marci@198
   574
	    //}
marci@193
   575
	}
marci@193
   576
      }
marci@193
   577
      
marci@193
   578
      typename AugGraph::NodeMap<Number> free(res_graph);
marci@193
   579
	
marci@193
   580
      Node n;
marci@193
   581
      //searching for augmenting path
marci@193
   582
      while ( !res_bfs.finished() ) { 
marci@193
   583
	AugOutEdgeIt e=res_bfs;
marci@193
   584
	if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
marci@193
   585
	  Node v=res_graph.tail(e);
marci@193
   586
	  Node w=res_graph.head(e);
marci@193
   587
	  pred.set(w, e);
marci@193
   588
	  if (res_graph.valid(pred.get(v))) {
marci@193
   589
	    free.set(w, std::min(free.get(v), res_graph.free(e)));
marci@193
   590
	  } else {
marci@193
   591
	    free.set(w, res_graph.free(e)); 
marci@193
   592
	  }
marci@197
   593
	  n=res_graph.head(e);
marci@198
   594
	  if (T->get(n) && (used.get(n)<1) ) { 
marci@198
   595
	    //Number u=0;
marci@198
   596
	    //for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
marci@198
   597
	    //u+=flow->get(f);
marci@198
   598
	    //if (u<1) {
marci@197
   599
	      _augment=true; 
marci@197
   600
	      break; 
marci@198
   601
	      //}
marci@193
   602
	  }
marci@193
   603
	}
marci@193
   604
	
marci@193
   605
	++res_bfs;
marci@193
   606
      } //end of searching augmenting path
marci@193
   607
marci@193
   608
      if (_augment) {
marci@193
   609
	//Node n=t;
marci@198
   610
	used.set(n, 1); //mind2 vegen jav
marci@194
   611
	Number augment_value=free.get(n);
marci@193
   612
	while (res_graph.valid(pred.get(n))) { 
marci@193
   613
	  AugEdge e=pred.get(n);
marci@193
   614
	  res_graph.augment(e, augment_value); 
marci@193
   615
	  n=res_graph.tail(e);
marci@193
   616
	}
marci@198
   617
	used.set(n, 1); //mind2 vegen jav
marci@193
   618
      }
marci@193
   619
marci@193
   620
      return _augment;
marci@193
   621
    }
marci@193
   622
marci@193
   623
//     template<typename MutableGraph> bool augmentOnBlockingFlow() {      
marci@193
   624
//       bool _augment=false;
marci@193
   625
marci@193
   626
//       AugGraph res_graph(*G, *flow, *capacity);
marci@193
   627
marci@193
   628
//       typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@193
   629
//       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
marci@193
   630
marci@197
   631
marci@197
   632
marci@197
   633
marci@197
   634
marci@197
   635
//       //typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@197
   636
//       for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
marci@197
   637
// 	if (S->get(s)) {
marci@197
   638
// 	  Number u=0;
marci@197
   639
// 	  for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
marci@197
   640
// 	    u+=flow->get(e);
marci@197
   641
// 	  if (u<1) {
marci@197
   642
// 	    res_bfs.pushAndSetReached(s);
marci@197
   643
// 	    //pred.set(s, AugEdge(INVALID));
marci@197
   644
// 	  }
marci@197
   645
// 	}
marci@197
   646
//       }
marci@197
   647
marci@197
   648
marci@197
   649
marci@197
   650
marci@197
   651
//       //bfs.pushAndSetReached(s);
marci@193
   652
//       typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
marci@193
   653
//       while ( !bfs.finished() ) { 
marci@193
   654
// 	AugOutEdgeIt e=bfs;
marci@193
   655
// 	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@193
   656
// 	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@193
   657
// 	}
marci@193
   658
	
marci@193
   659
// 	++bfs;
marci@193
   660
//       } //computing distances from s in the residual graph
marci@193
   661
marci@193
   662
//       MutableGraph F;
marci@193
   663
//       typename AugGraph::NodeMap<typename MutableGraph::Node> 
marci@193
   664
// 	res_graph_to_F(res_graph);
marci@193
   665
//       for(typename AugGraph::NodeIt n=res_graph.template first<typename AugGraph::NodeIt>(); res_graph.valid(n); res_graph.next(n)) {
marci@193
   666
// 	res_graph_to_F.set(n, F.addNode());
marci@193
   667
//       }
marci@193
   668
      
marci@193
   669
//       typename MutableGraph::Node sF=res_graph_to_F.get(s);
marci@193
   670
//       typename MutableGraph::Node tF=res_graph_to_F.get(t);
marci@193
   671
marci@193
   672
//       typename MutableGraph::EdgeMap<AugEdge> original_edge(F);
marci@193
   673
//       typename MutableGraph::EdgeMap<Number> residual_capacity(F);
marci@193
   674
marci@193
   675
//       //Making F to the graph containing the edges of the residual graph 
marci@193
   676
//       //which are in some shortest paths
marci@193
   677
//       for(typename AugGraph::EdgeIt e=res_graph.template first<typename AugGraph::EdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
marci@193
   678
// 	if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
marci@193
   679
// 	  typename MutableGraph::Edge f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
marci@193
   680
// 	  original_edge.update();
marci@193
   681
// 	  original_edge.set(f, e);
marci@193
   682
// 	  residual_capacity.update();
marci@193
   683
// 	  residual_capacity.set(f, res_graph.free(e));
marci@193
   684
// 	} 
marci@193
   685
//       }
marci@193
   686
marci@193
   687
//       bool __augment=true;
marci@193
   688
marci@193
   689
//       while (__augment) {
marci@193
   690
// 	__augment=false;
marci@193
   691
// 	//computing blocking flow with dfs
marci@193
   692
// 	typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
marci@193
   693
// 	DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
marci@193
   694
// 	typename MutableGraph::NodeMap<typename MutableGraph::Edge> pred(F);
marci@193
   695
// 	pred.set(sF, typename MutableGraph::Edge(INVALID));
marci@193
   696
// 	//invalid iterators for sources
marci@193
   697
marci@193
   698
// 	typename MutableGraph::NodeMap<Number> free(F);
marci@193
   699
marci@193
   700
// 	dfs.pushAndSetReached(sF);      
marci@193
   701
// 	while (!dfs.finished()) {
marci@193
   702
// 	  ++dfs;
marci@193
   703
// 	  if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
marci@193
   704
// 	    if (dfs.isBNodeNewlyReached()) {
marci@193
   705
// 	      typename MutableGraph::Node v=F.aNode(dfs);
marci@193
   706
// 	      typename MutableGraph::Node w=F.bNode(dfs);
marci@193
   707
// 	      pred.set(w, dfs);
marci@193
   708
// 	      if (F.valid(pred.get(v))) {
marci@193
   709
// 		free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
marci@193
   710
// 	      } else {
marci@193
   711
// 		free.set(w, residual_capacity.get(dfs)); 
marci@193
   712
// 	      }
marci@193
   713
// 	      if (w==tF) { 
marci@193
   714
// 		__augment=true; 
marci@193
   715
// 		_augment=true;
marci@193
   716
// 		break; 
marci@193
   717
// 	      }
marci@193
   718
	      
marci@193
   719
// 	    } else {
marci@193
   720
// 	      F.erase(typename MutableGraph::OutEdgeIt(dfs));
marci@193
   721
// 	    }
marci@193
   722
// 	  } 
marci@193
   723
// 	}
marci@193
   724
marci@193
   725
// 	if (__augment) {
marci@193
   726
// 	  typename MutableGraph::Node n=tF;
marci@193
   727
// 	  Number augment_value=free.get(tF);
marci@193
   728
// 	  while (F.valid(pred.get(n))) { 
marci@193
   729
// 	    typename MutableGraph::Edge e=pred.get(n);
marci@193
   730
// 	    res_graph.augment(original_edge.get(e), augment_value); 
marci@193
   731
// 	    n=F.tail(e);
marci@193
   732
// 	    if (residual_capacity.get(e)==augment_value) 
marci@193
   733
// 	      F.erase(e); 
marci@193
   734
// 	    else 
marci@193
   735
// 	      residual_capacity.set(e, residual_capacity.get(e)-augment_value);
marci@193
   736
// 	  }
marci@193
   737
// 	}
marci@193
   738
	
marci@193
   739
//       }
marci@193
   740
            
marci@193
   741
//       return _augment;
marci@193
   742
//     }
marci@197
   743
    bool augmentOnBlockingFlow2() {
marci@197
   744
      bool _augment=false;
marci@193
   745
marci@197
   746
      //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
marci@197
   747
      typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
marci@197
   748
      typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
marci@197
   749
      typedef typename EAugGraph::Edge EAugEdge;
marci@193
   750
marci@197
   751
      EAugGraph res_graph(*G, *flow, *capacity);
marci@193
   752
marci@197
   753
      //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
marci@197
   754
      BfsIterator4< 
marci@197
   755
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>, 
marci@197
   756
	typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt, 
marci@197
   757
	ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
marci@197
   758
marci@197
   759
marci@197
   760
      //typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@197
   761
      for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
marci@197
   762
	if (S->get(s)) {
marci@197
   763
	  Number u=0;
marci@197
   764
	  for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
marci@197
   765
	    u+=flow->get(e);
marci@197
   766
	  if (u<1) {
marci@197
   767
	    bfs.pushAndSetReached(s);
marci@197
   768
	    //pred.set(s, AugEdge(INVALID));
marci@197
   769
	  }
marci@197
   770
	}
marci@197
   771
      }
marci@197
   772
marci@193
   773
      
marci@197
   774
      //bfs.pushAndSetReached(s);
marci@193
   775
marci@197
   776
      typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
marci@197
   777
	NodeMap<int>& dist=res_graph.dist;
marci@193
   778
marci@197
   779
      while ( !bfs.finished() ) {
marci@197
   780
	typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
marci@197
   781
	if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
marci@197
   782
	  dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
marci@197
   783
	}
marci@197
   784
	++bfs;	
marci@197
   785
      } //computing distances from s in the residual graph
marci@193
   786
marci@197
   787
      bool __augment=true;
marci@193
   788
marci@197
   789
      while (__augment) {
marci@193
   790
marci@197
   791
	__augment=false;
marci@197
   792
	//computing blocking flow with dfs
marci@197
   793
	typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
marci@197
   794
	DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > 
marci@197
   795
	  dfs(res_graph);
marci@197
   796
	typename EAugGraph::NodeMap<EAugEdge> pred(res_graph, INVALID); 
marci@197
   797
	//pred.set(s, EAugEdge(INVALID));
marci@197
   798
	//invalid iterators for sources
marci@193
   799
marci@197
   800
	typename EAugGraph::NodeMap<Number> free(res_graph);
marci@193
   801
marci@197
   802
marci@197
   803
	//typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@197
   804
      for(NodeIt s=G->template first<NodeIt>(); G->valid(s); G->next(s)) {
marci@197
   805
	if (S->get(s)) {
marci@197
   806
	  Number u=0;
marci@197
   807
	  for(OutEdgeIt e=G->template first<OutEdgeIt>(s); G->valid(e); G->next(e))
marci@197
   808
	    u+=flow->get(e);
marci@197
   809
	  if (u<1) {
marci@197
   810
	    dfs.pushAndSetReached(s);
marci@197
   811
	    //pred.set(s, AugEdge(INVALID));
marci@197
   812
	  }
marci@197
   813
	}
marci@197
   814
      }
marci@197
   815
marci@197
   816
marci@197
   817
marci@197
   818
      //dfs.pushAndSetReached(s);
marci@197
   819
      typename EAugGraph::Node n;
marci@197
   820
	while (!dfs.finished()) {
marci@197
   821
	  ++dfs;
marci@197
   822
	  if (res_graph.valid(EAugOutEdgeIt(dfs))) { 
marci@197
   823
	    if (dfs.isBNodeNewlyReached()) {
marci@193
   824
	  
marci@197
   825
	      typename EAugGraph::Node v=res_graph.aNode(dfs);
marci@197
   826
	      typename EAugGraph::Node w=res_graph.bNode(dfs);
marci@193
   827
marci@197
   828
	      pred.set(w, EAugOutEdgeIt(dfs));
marci@197
   829
	      if (res_graph.valid(pred.get(v))) {
marci@197
   830
		free.set(w, std::min(free.get(v), res_graph.free(dfs)));
marci@197
   831
	      } else {
marci@197
   832
		free.set(w, res_graph.free(dfs)); 
marci@197
   833
	      }
marci@197
   834
	     
marci@197
   835
	      n=w;
marci@197
   836
	      if (T->get(w)) {
marci@197
   837
		Number u=0;
marci@197
   838
		for(InEdgeIt f=G->template first<InEdgeIt>(n); G->valid(f); G->next(f))
marci@197
   839
		  u+=flow->get(f);
marci@197
   840
		if (u<1) {
marci@197
   841
		  __augment=true; 
marci@197
   842
		  _augment=true;
marci@197
   843
		  break; 
marci@197
   844
		}
marci@197
   845
	      }
marci@197
   846
	    } else {
marci@197
   847
	      res_graph.erase(dfs);
marci@197
   848
	    }
marci@197
   849
	  } 
marci@193
   850
marci@197
   851
	}
marci@193
   852
marci@197
   853
	if (__augment) {
marci@197
   854
	  // typename EAugGraph::Node n=t;
marci@197
   855
	  Number augment_value=free.get(n);
marci@197
   856
	  while (res_graph.valid(pred.get(n))) { 
marci@197
   857
	    EAugEdge e=pred.get(n);
marci@197
   858
	    res_graph.augment(e, augment_value);
marci@197
   859
	    n=res_graph.tail(e);
marci@197
   860
	    if (res_graph.free(e)==0)
marci@197
   861
	      res_graph.erase(e);
marci@197
   862
	  }
marci@197
   863
	}
marci@193
   864
      
marci@197
   865
      }
marci@193
   866
            
marci@197
   867
      return _augment;
marci@197
   868
    }
marci@193
   869
    void run() {
marci@193
   870
      //int num_of_augmentations=0;
marci@193
   871
      while (augmentOnShortestPath()) { 
marci@193
   872
	//while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@193
   873
	//std::cout << ++num_of_augmentations << " ";
marci@193
   874
	//std::cout<<std::endl;
marci@193
   875
      } 
marci@193
   876
    }
marci@194
   877
//     template<typename MutableGraph> void run() {
marci@194
   878
//       //int num_of_augmentations=0;
marci@194
   879
//       //while (augmentOnShortestPath()) { 
marci@194
   880
// 	while (augmentOnBlockingFlow<MutableGraph>()) { 
marci@194
   881
// 	//std::cout << ++num_of_augmentations << " ";
marci@194
   882
// 	//std::cout<<std::endl;
marci@194
   883
//       } 
marci@194
   884
//     } 
marci@193
   885
    Number flowValue() { 
marci@193
   886
      Number a=0;
marci@194
   887
      EdgeIt e;
marci@194
   888
      for(G->/*getF*/first(e); G->valid(e); G->next(e)) {
marci@193
   889
	a+=flow->get(e);
marci@193
   890
      }
marci@193
   891
      return a;
marci@193
   892
    }
marci@193
   893
  };
marci@193
   894
marci@193
   895
marci@193
   896
marci@193
   897
marci@193
   898
marci@174
   899
  
marci@174
   900
//   template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
marci@174
   901
//   class MaxFlow2 {
marci@174
   902
//   public:
marci@174
   903
//     typedef typename Graph::Node Node;
marci@174
   904
//     typedef typename Graph::Edge Edge;
marci@174
   905
//     typedef typename Graph::EdgeIt EdgeIt;
marci@174
   906
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
   907
//     typedef typename Graph::InEdgeIt InEdgeIt;
marci@174
   908
//   private:
marci@174
   909
//     const Graph& G;
marci@174
   910
//     std::list<Node>& S;
marci@174
   911
//     std::list<Node>& T;
marci@174
   912
//     FlowMap& flow;
marci@174
   913
//     const CapacityMap& capacity;
marci@174
   914
//     typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
marci@174
   915
//     typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
marci@174
   916
//     typedef typename AugGraph::Edge AugEdge;
marci@174
   917
//     typename Graph::NodeMap<bool> SMap;
marci@174
   918
//     typename Graph::NodeMap<bool> TMap;
marci@174
   919
//   public:
marci@174
   920
//     MaxFlow2(const Graph& _G, std::list<Node>& _S, std::list<Node>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { 
marci@174
   921
//       for(typename std::list<Node>::const_iterator i=S.begin(); 
marci@174
   922
// 	  i!=S.end(); ++i) { 
marci@174
   923
// 	SMap.set(*i, true); 
marci@174
   924
//       }
marci@174
   925
//       for (typename std::list<Node>::const_iterator i=T.begin(); 
marci@174
   926
// 	   i!=T.end(); ++i) { 
marci@174
   927
// 	TMap.set(*i, true); 
marci@174
   928
//       }
marci@174
   929
//     }
marci@174
   930
//     bool augment() {
marci@174
   931
//       AugGraph res_graph(G, flow, capacity);
marci@174
   932
//       bool _augment=false;
marci@174
   933
//       Node reached_t_node;
marci@174
   934
      
marci@174
   935
//       typedef typename AugGraph::NodeMap<bool> ReachedMap;
marci@174
   936
//       BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
marci@174
   937
//       for(typename std::list<Node>::const_iterator i=S.begin(); 
marci@174
   938
// 	  i!=S.end(); ++i) {
marci@174
   939
// 	res_bfs.pushAndSetReached(*i);
marci@174
   940
//       }
marci@174
   941
//       //res_bfs.pushAndSetReached(s);
marci@174
   942
	
marci@174
   943
//       typename AugGraph::NodeMap<AugEdge> pred(res_graph); 
marci@174
   944
//       //filled up with invalid iterators
marci@174
   945
      
marci@174
   946
//       typename AugGraph::NodeMap<Number> free(res_graph);
marci@174
   947
	
marci@174
   948
//       //searching for augmenting path
marci@174
   949
//       while ( !res_bfs.finished() ) { 
marci@174
   950
// 	AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
marci@174
   951
// 	if (e.valid() && res_bfs.isBNodeNewlyReached()) {
marci@174
   952
// 	  Node v=res_graph.tail(e);
marci@174
   953
// 	  Node w=res_graph.head(e);
marci@174
   954
// 	  pred.set(w, e);
marci@174
   955
// 	  if (pred.get(v).valid()) {
marci@174
   956
// 	    free.set(w, std::min(free.get(v), e.free()));
marci@174
   957
// 	  } else {
marci@174
   958
// 	    free.set(w, e.free()); 
marci@174
   959
// 	  }
marci@174
   960
// 	  if (TMap.get(res_graph.head(e))) { 
marci@174
   961
// 	    _augment=true; 
marci@174
   962
// 	    reached_t_node=res_graph.head(e);
marci@174
   963
// 	    break; 
marci@174
   964
// 	  }
marci@174
   965
// 	}
marci@174
   966
	
marci@174
   967
// 	++res_bfs;
marci@174
   968
//       } //end of searching augmenting path
marci@174
   969
marci@174
   970
//       if (_augment) {
marci@174
   971
// 	Node n=reached_t_node;
marci@174
   972
// 	Number augment_value=free.get(reached_t_node);
marci@174
   973
// 	while (pred.get(n).valid()) { 
marci@174
   974
// 	  AugEdge e=pred.get(n);
marci@174
   975
// 	  e.augment(augment_value); 
marci@174
   976
// 	  n=res_graph.tail(e);
marci@174
   977
// 	}
marci@174
   978
//       }
marci@174
   979
marci@174
   980
//       return _augment;
marci@174
   981
//     }
marci@174
   982
//     void run() {
marci@174
   983
//       while (augment()) { } 
marci@174
   984
//     }
marci@174
   985
//     Number flowValue() { 
marci@174
   986
//       Number a=0;
marci@174
   987
//       for(typename std::list<Node>::const_iterator i=S.begin(); 
marci@174
   988
// 	  i!=S.end(); ++i) { 
marci@174
   989
// 	for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
marci@174
   990
// 	  a+=flow.get(e);
marci@174
   991
// 	}
marci@174
   992
// 	for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
marci@174
   993
// 	  a-=flow.get(e);
marci@174
   994
// 	}
marci@174
   995
//       }
marci@174
   996
//       return a;
marci@174
   997
//     }
marci@174
   998
//   };
marci@174
   999
marci@174
  1000
marci@174
  1001
marci@174
  1002
} // namespace hugo
marci@174
  1003
marci@174
  1004
#endif //EDMONDS_KARP_H