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