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